api.xpack.ml.start_datafeed.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 newMLStartDatafeedFunc(t Transport) MLStartDatafeed {
  28. return func(datafeed_id string, o ...func(*MLStartDatafeedRequest)) (*Response, error) {
  29. var r = MLStartDatafeedRequest{DatafeedID: datafeed_id}
  30. for _, f := range o {
  31. f(&r)
  32. }
  33. return r.Do(r.ctx, t)
  34. }
  35. }
  36. // ----- API Definition -------------------------------------------------------
  37. // MLStartDatafeed - Starts one or more datafeeds.
  38. //
  39. // See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/current/ml-start-datafeed.html.
  40. type MLStartDatafeed func(datafeed_id string, o ...func(*MLStartDatafeedRequest)) (*Response, error)
  41. // MLStartDatafeedRequest configures the ML Start Datafeed API request.
  42. type MLStartDatafeedRequest struct {
  43. Body io.Reader
  44. DatafeedID string
  45. End string
  46. Start string
  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 MLStartDatafeedRequest) 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 = "POST"
  63. path.Grow(1 + len("_ml") + 1 + len("datafeeds") + 1 + len(r.DatafeedID) + 1 + len("_start"))
  64. path.WriteString("/")
  65. path.WriteString("_ml")
  66. path.WriteString("/")
  67. path.WriteString("datafeeds")
  68. path.WriteString("/")
  69. path.WriteString(r.DatafeedID)
  70. path.WriteString("/")
  71. path.WriteString("_start")
  72. params = make(map[string]string)
  73. if r.End != "" {
  74. params["end"] = r.End
  75. }
  76. if r.Start != "" {
  77. params["start"] = r.Start
  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 MLStartDatafeed) WithContext(v context.Context) func(*MLStartDatafeedRequest) {
  135. return func(r *MLStartDatafeedRequest) {
  136. r.ctx = v
  137. }
  138. }
  139. // WithBody - The start datafeed parameters.
  140. func (f MLStartDatafeed) WithBody(v io.Reader) func(*MLStartDatafeedRequest) {
  141. return func(r *MLStartDatafeedRequest) {
  142. r.Body = v
  143. }
  144. }
  145. // WithEnd - the end time when the datafeed should stop. when not set, the datafeed continues in real time.
  146. func (f MLStartDatafeed) WithEnd(v string) func(*MLStartDatafeedRequest) {
  147. return func(r *MLStartDatafeedRequest) {
  148. r.End = v
  149. }
  150. }
  151. // WithStart - the start time from where the datafeed should begin.
  152. func (f MLStartDatafeed) WithStart(v string) func(*MLStartDatafeedRequest) {
  153. return func(r *MLStartDatafeedRequest) {
  154. r.Start = v
  155. }
  156. }
  157. // WithTimeout - controls the time to wait until a datafeed has started. default to 20 seconds.
  158. func (f MLStartDatafeed) WithTimeout(v time.Duration) func(*MLStartDatafeedRequest) {
  159. return func(r *MLStartDatafeedRequest) {
  160. r.Timeout = v
  161. }
  162. }
  163. // WithPretty makes the response body pretty-printed.
  164. func (f MLStartDatafeed) WithPretty() func(*MLStartDatafeedRequest) {
  165. return func(r *MLStartDatafeedRequest) {
  166. r.Pretty = true
  167. }
  168. }
  169. // WithHuman makes statistical values human-readable.
  170. func (f MLStartDatafeed) WithHuman() func(*MLStartDatafeedRequest) {
  171. return func(r *MLStartDatafeedRequest) {
  172. r.Human = true
  173. }
  174. }
  175. // WithErrorTrace includes the stack trace for errors in the response body.
  176. func (f MLStartDatafeed) WithErrorTrace() func(*MLStartDatafeedRequest) {
  177. return func(r *MLStartDatafeedRequest) {
  178. r.ErrorTrace = true
  179. }
  180. }
  181. // WithFilterPath filters the properties of the response body.
  182. func (f MLStartDatafeed) WithFilterPath(v ...string) func(*MLStartDatafeedRequest) {
  183. return func(r *MLStartDatafeedRequest) {
  184. r.FilterPath = v
  185. }
  186. }
  187. // WithHeader adds the headers to the HTTP request.
  188. func (f MLStartDatafeed) WithHeader(h map[string]string) func(*MLStartDatafeedRequest) {
  189. return func(r *MLStartDatafeedRequest) {
  190. if r.Header == nil {
  191. r.Header = make(http.Header)
  192. }
  193. for k, v := range h {
  194. r.Header.Add(k, v)
  195. }
  196. }
  197. }
  198. // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
  199. func (f MLStartDatafeed) WithOpaqueID(s string) func(*MLStartDatafeedRequest) {
  200. return func(r *MLStartDatafeedRequest) {
  201. if r.Header == nil {
  202. r.Header = make(http.Header)
  203. }
  204. r.Header.Set("X-Opaque-Id", s)
  205. }
  206. }