api.put_script.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // Licensed to Elasticsearch B.V. under one or more contributor
  2. // license agreements. See the NOTICE file distributed with
  3. // this work for additional information regarding copyright
  4. // ownership. Elasticsearch B.V. licenses this file to you under
  5. // the Apache License, Version 2.0 (the "License"); you may
  6. // not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. //
  18. // Code generated from specification version 7.17.10: DO NOT EDIT
  19. package esapi
  20. import (
  21. "context"
  22. "io"
  23. "net/http"
  24. "strings"
  25. "time"
  26. )
  27. func newPutScriptFunc(t Transport) PutScript {
  28. return func(id string, body io.Reader, o ...func(*PutScriptRequest)) (*Response, error) {
  29. var r = PutScriptRequest{ScriptID: id, Body: body}
  30. for _, f := range o {
  31. f(&r)
  32. }
  33. return r.Do(r.ctx, t)
  34. }
  35. }
  36. // ----- API Definition -------------------------------------------------------
  37. // PutScript creates or updates a script.
  38. //
  39. // See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/master/modules-scripting.html.
  40. type PutScript func(id string, body io.Reader, o ...func(*PutScriptRequest)) (*Response, error)
  41. // PutScriptRequest configures the Put Script API request.
  42. type PutScriptRequest struct {
  43. ScriptID string
  44. Body io.Reader
  45. ScriptContext string
  46. MasterTimeout time.Duration
  47. Timeout time.Duration
  48. Pretty bool
  49. Human bool
  50. ErrorTrace bool
  51. FilterPath []string
  52. Header http.Header
  53. ctx context.Context
  54. }
  55. // Do executes the request and returns response or error.
  56. func (r PutScriptRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
  57. var (
  58. method string
  59. path strings.Builder
  60. params map[string]string
  61. )
  62. method = "PUT"
  63. path.Grow(1 + len("_scripts") + 1 + len(r.ScriptID) + 1 + len(r.ScriptContext))
  64. path.WriteString("/")
  65. path.WriteString("_scripts")
  66. path.WriteString("/")
  67. path.WriteString(r.ScriptID)
  68. if r.ScriptContext != "" {
  69. path.WriteString("/")
  70. path.WriteString(r.ScriptContext)
  71. }
  72. params = make(map[string]string)
  73. if r.ScriptContext != "" {
  74. params["context"] = r.ScriptContext
  75. }
  76. if r.MasterTimeout != 0 {
  77. params["master_timeout"] = formatDuration(r.MasterTimeout)
  78. }
  79. if r.Timeout != 0 {
  80. params["timeout"] = formatDuration(r.Timeout)
  81. }
  82. if r.Pretty {
  83. params["pretty"] = "true"
  84. }
  85. if r.Human {
  86. params["human"] = "true"
  87. }
  88. if r.ErrorTrace {
  89. params["error_trace"] = "true"
  90. }
  91. if len(r.FilterPath) > 0 {
  92. params["filter_path"] = strings.Join(r.FilterPath, ",")
  93. }
  94. req, err := newRequest(method, path.String(), r.Body)
  95. if err != nil {
  96. return nil, err
  97. }
  98. if len(params) > 0 {
  99. q := req.URL.Query()
  100. for k, v := range params {
  101. q.Set(k, v)
  102. }
  103. req.URL.RawQuery = q.Encode()
  104. }
  105. if len(r.Header) > 0 {
  106. if len(req.Header) == 0 {
  107. req.Header = r.Header
  108. } else {
  109. for k, vv := range r.Header {
  110. for _, v := range vv {
  111. req.Header.Add(k, v)
  112. }
  113. }
  114. }
  115. }
  116. if r.Body != nil && req.Header.Get(headerContentType) == "" {
  117. req.Header[headerContentType] = headerContentTypeJSON
  118. }
  119. if ctx != nil {
  120. req = req.WithContext(ctx)
  121. }
  122. res, err := transport.Perform(req)
  123. if err != nil {
  124. return nil, err
  125. }
  126. response := Response{
  127. StatusCode: res.StatusCode,
  128. Body: res.Body,
  129. Header: res.Header,
  130. }
  131. return &response, nil
  132. }
  133. // WithContext sets the request context.
  134. func (f PutScript) WithContext(v context.Context) func(*PutScriptRequest) {
  135. return func(r *PutScriptRequest) {
  136. r.ctx = v
  137. }
  138. }
  139. // WithScriptContext - script context.
  140. func (f PutScript) WithScriptContext(v string) func(*PutScriptRequest) {
  141. return func(r *PutScriptRequest) {
  142. r.ScriptContext = v
  143. }
  144. }
  145. // WithMasterTimeout - specify timeout for connection to master.
  146. func (f PutScript) WithMasterTimeout(v time.Duration) func(*PutScriptRequest) {
  147. return func(r *PutScriptRequest) {
  148. r.MasterTimeout = v
  149. }
  150. }
  151. // WithTimeout - explicit operation timeout.
  152. func (f PutScript) WithTimeout(v time.Duration) func(*PutScriptRequest) {
  153. return func(r *PutScriptRequest) {
  154. r.Timeout = v
  155. }
  156. }
  157. // WithPretty makes the response body pretty-printed.
  158. func (f PutScript) WithPretty() func(*PutScriptRequest) {
  159. return func(r *PutScriptRequest) {
  160. r.Pretty = true
  161. }
  162. }
  163. // WithHuman makes statistical values human-readable.
  164. func (f PutScript) WithHuman() func(*PutScriptRequest) {
  165. return func(r *PutScriptRequest) {
  166. r.Human = true
  167. }
  168. }
  169. // WithErrorTrace includes the stack trace for errors in the response body.
  170. func (f PutScript) WithErrorTrace() func(*PutScriptRequest) {
  171. return func(r *PutScriptRequest) {
  172. r.ErrorTrace = true
  173. }
  174. }
  175. // WithFilterPath filters the properties of the response body.
  176. func (f PutScript) WithFilterPath(v ...string) func(*PutScriptRequest) {
  177. return func(r *PutScriptRequest) {
  178. r.FilterPath = v
  179. }
  180. }
  181. // WithHeader adds the headers to the HTTP request.
  182. func (f PutScript) WithHeader(h map[string]string) func(*PutScriptRequest) {
  183. return func(r *PutScriptRequest) {
  184. if r.Header == nil {
  185. r.Header = make(http.Header)
  186. }
  187. for k, v := range h {
  188. r.Header.Add(k, v)
  189. }
  190. }
  191. }
  192. // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
  193. func (f PutScript) WithOpaqueID(s string) func(*PutScriptRequest) {
  194. return func(r *PutScriptRequest) {
  195. if r.Header == nil {
  196. r.Header = make(http.Header)
  197. }
  198. r.Header.Set("X-Opaque-Id", s)
  199. }
  200. }