api.xpack.watcher.put_watch.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. "strconv"
  25. "strings"
  26. )
  27. func newWatcherPutWatchFunc(t Transport) WatcherPutWatch {
  28. return func(id string, o ...func(*WatcherPutWatchRequest)) (*Response, error) {
  29. var r = WatcherPutWatchRequest{WatchID: id}
  30. for _, f := range o {
  31. f(&r)
  32. }
  33. return r.Do(r.ctx, t)
  34. }
  35. }
  36. // ----- API Definition -------------------------------------------------------
  37. // WatcherPutWatch - Creates a new watch, or updates an existing one.
  38. //
  39. // See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/current/watcher-api-put-watch.html.
  40. type WatcherPutWatch func(id string, o ...func(*WatcherPutWatchRequest)) (*Response, error)
  41. // WatcherPutWatchRequest configures the Watcher Put Watch API request.
  42. type WatcherPutWatchRequest struct {
  43. WatchID string
  44. Body io.Reader
  45. Active *bool
  46. IfPrimaryTerm *int
  47. IfSeqNo *int
  48. Version *int
  49. Pretty bool
  50. Human bool
  51. ErrorTrace bool
  52. FilterPath []string
  53. Header http.Header
  54. ctx context.Context
  55. }
  56. // Do executes the request and returns response or error.
  57. func (r WatcherPutWatchRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
  58. var (
  59. method string
  60. path strings.Builder
  61. params map[string]string
  62. )
  63. method = "PUT"
  64. path.Grow(1 + len("_watcher") + 1 + len("watch") + 1 + len(r.WatchID))
  65. path.WriteString("/")
  66. path.WriteString("_watcher")
  67. path.WriteString("/")
  68. path.WriteString("watch")
  69. path.WriteString("/")
  70. path.WriteString(r.WatchID)
  71. params = make(map[string]string)
  72. if r.Active != nil {
  73. params["active"] = strconv.FormatBool(*r.Active)
  74. }
  75. if r.IfPrimaryTerm != nil {
  76. params["if_primary_term"] = strconv.FormatInt(int64(*r.IfPrimaryTerm), 10)
  77. }
  78. if r.IfSeqNo != nil {
  79. params["if_seq_no"] = strconv.FormatInt(int64(*r.IfSeqNo), 10)
  80. }
  81. if r.Version != nil {
  82. params["version"] = strconv.FormatInt(int64(*r.Version), 10)
  83. }
  84. if r.Pretty {
  85. params["pretty"] = "true"
  86. }
  87. if r.Human {
  88. params["human"] = "true"
  89. }
  90. if r.ErrorTrace {
  91. params["error_trace"] = "true"
  92. }
  93. if len(r.FilterPath) > 0 {
  94. params["filter_path"] = strings.Join(r.FilterPath, ",")
  95. }
  96. req, err := newRequest(method, path.String(), r.Body)
  97. if err != nil {
  98. return nil, err
  99. }
  100. if len(params) > 0 {
  101. q := req.URL.Query()
  102. for k, v := range params {
  103. q.Set(k, v)
  104. }
  105. req.URL.RawQuery = q.Encode()
  106. }
  107. if len(r.Header) > 0 {
  108. if len(req.Header) == 0 {
  109. req.Header = r.Header
  110. } else {
  111. for k, vv := range r.Header {
  112. for _, v := range vv {
  113. req.Header.Add(k, v)
  114. }
  115. }
  116. }
  117. }
  118. if r.Body != nil && req.Header.Get(headerContentType) == "" {
  119. req.Header[headerContentType] = headerContentTypeJSON
  120. }
  121. if ctx != nil {
  122. req = req.WithContext(ctx)
  123. }
  124. res, err := transport.Perform(req)
  125. if err != nil {
  126. return nil, err
  127. }
  128. response := Response{
  129. StatusCode: res.StatusCode,
  130. Body: res.Body,
  131. Header: res.Header,
  132. }
  133. return &response, nil
  134. }
  135. // WithContext sets the request context.
  136. func (f WatcherPutWatch) WithContext(v context.Context) func(*WatcherPutWatchRequest) {
  137. return func(r *WatcherPutWatchRequest) {
  138. r.ctx = v
  139. }
  140. }
  141. // WithBody - The watch.
  142. func (f WatcherPutWatch) WithBody(v io.Reader) func(*WatcherPutWatchRequest) {
  143. return func(r *WatcherPutWatchRequest) {
  144. r.Body = v
  145. }
  146. }
  147. // WithActive - specify whether the watch is in/active by default.
  148. func (f WatcherPutWatch) WithActive(v bool) func(*WatcherPutWatchRequest) {
  149. return func(r *WatcherPutWatchRequest) {
  150. r.Active = &v
  151. }
  152. }
  153. // WithIfPrimaryTerm - only update the watch if the last operation that has changed the watch has the specified primary term.
  154. func (f WatcherPutWatch) WithIfPrimaryTerm(v int) func(*WatcherPutWatchRequest) {
  155. return func(r *WatcherPutWatchRequest) {
  156. r.IfPrimaryTerm = &v
  157. }
  158. }
  159. // WithIfSeqNo - only update the watch if the last operation that has changed the watch has the specified sequence number.
  160. func (f WatcherPutWatch) WithIfSeqNo(v int) func(*WatcherPutWatchRequest) {
  161. return func(r *WatcherPutWatchRequest) {
  162. r.IfSeqNo = &v
  163. }
  164. }
  165. // WithVersion - explicit version number for concurrency control.
  166. func (f WatcherPutWatch) WithVersion(v int) func(*WatcherPutWatchRequest) {
  167. return func(r *WatcherPutWatchRequest) {
  168. r.Version = &v
  169. }
  170. }
  171. // WithPretty makes the response body pretty-printed.
  172. func (f WatcherPutWatch) WithPretty() func(*WatcherPutWatchRequest) {
  173. return func(r *WatcherPutWatchRequest) {
  174. r.Pretty = true
  175. }
  176. }
  177. // WithHuman makes statistical values human-readable.
  178. func (f WatcherPutWatch) WithHuman() func(*WatcherPutWatchRequest) {
  179. return func(r *WatcherPutWatchRequest) {
  180. r.Human = true
  181. }
  182. }
  183. // WithErrorTrace includes the stack trace for errors in the response body.
  184. func (f WatcherPutWatch) WithErrorTrace() func(*WatcherPutWatchRequest) {
  185. return func(r *WatcherPutWatchRequest) {
  186. r.ErrorTrace = true
  187. }
  188. }
  189. // WithFilterPath filters the properties of the response body.
  190. func (f WatcherPutWatch) WithFilterPath(v ...string) func(*WatcherPutWatchRequest) {
  191. return func(r *WatcherPutWatchRequest) {
  192. r.FilterPath = v
  193. }
  194. }
  195. // WithHeader adds the headers to the HTTP request.
  196. func (f WatcherPutWatch) WithHeader(h map[string]string) func(*WatcherPutWatchRequest) {
  197. return func(r *WatcherPutWatchRequest) {
  198. if r.Header == nil {
  199. r.Header = make(http.Header)
  200. }
  201. for k, v := range h {
  202. r.Header.Add(k, v)
  203. }
  204. }
  205. }
  206. // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
  207. func (f WatcherPutWatch) WithOpaqueID(s string) func(*WatcherPutWatchRequest) {
  208. return func(r *WatcherPutWatchRequest) {
  209. if r.Header == nil {
  210. r.Header = make(http.Header)
  211. }
  212. r.Header.Set("X-Opaque-Id", s)
  213. }
  214. }