api.xpack.ml.get_calendars.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 newMLGetCalendarsFunc(t Transport) MLGetCalendars {
  28. return func(o ...func(*MLGetCalendarsRequest)) (*Response, error) {
  29. var r = MLGetCalendarsRequest{}
  30. for _, f := range o {
  31. f(&r)
  32. }
  33. return r.Do(r.ctx, t)
  34. }
  35. }
  36. // ----- API Definition -------------------------------------------------------
  37. // MLGetCalendars - Retrieves configuration information for calendars.
  38. //
  39. // See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/current/ml-get-calendar.html.
  40. type MLGetCalendars func(o ...func(*MLGetCalendarsRequest)) (*Response, error)
  41. // MLGetCalendarsRequest configures the ML Get Calendars API request.
  42. type MLGetCalendarsRequest struct {
  43. Body io.Reader
  44. CalendarID string
  45. From *int
  46. Size *int
  47. Pretty bool
  48. Human bool
  49. ErrorTrace bool
  50. FilterPath []string
  51. Header http.Header
  52. ctx context.Context
  53. }
  54. // Do executes the request and returns response or error.
  55. func (r MLGetCalendarsRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
  56. var (
  57. method string
  58. path strings.Builder
  59. params map[string]string
  60. )
  61. method = "POST"
  62. path.Grow(1 + len("_ml") + 1 + len("calendars") + 1 + len(r.CalendarID))
  63. path.WriteString("/")
  64. path.WriteString("_ml")
  65. path.WriteString("/")
  66. path.WriteString("calendars")
  67. if r.CalendarID != "" {
  68. path.WriteString("/")
  69. path.WriteString(r.CalendarID)
  70. }
  71. params = make(map[string]string)
  72. if r.From != nil {
  73. params["from"] = strconv.FormatInt(int64(*r.From), 10)
  74. }
  75. if r.Size != nil {
  76. params["size"] = strconv.FormatInt(int64(*r.Size), 10)
  77. }
  78. if r.Pretty {
  79. params["pretty"] = "true"
  80. }
  81. if r.Human {
  82. params["human"] = "true"
  83. }
  84. if r.ErrorTrace {
  85. params["error_trace"] = "true"
  86. }
  87. if len(r.FilterPath) > 0 {
  88. params["filter_path"] = strings.Join(r.FilterPath, ",")
  89. }
  90. req, err := newRequest(method, path.String(), r.Body)
  91. if err != nil {
  92. return nil, err
  93. }
  94. if len(params) > 0 {
  95. q := req.URL.Query()
  96. for k, v := range params {
  97. q.Set(k, v)
  98. }
  99. req.URL.RawQuery = q.Encode()
  100. }
  101. if len(r.Header) > 0 {
  102. if len(req.Header) == 0 {
  103. req.Header = r.Header
  104. } else {
  105. for k, vv := range r.Header {
  106. for _, v := range vv {
  107. req.Header.Add(k, v)
  108. }
  109. }
  110. }
  111. }
  112. if r.Body != nil && req.Header.Get(headerContentType) == "" {
  113. req.Header[headerContentType] = headerContentTypeJSON
  114. }
  115. if ctx != nil {
  116. req = req.WithContext(ctx)
  117. }
  118. res, err := transport.Perform(req)
  119. if err != nil {
  120. return nil, err
  121. }
  122. response := Response{
  123. StatusCode: res.StatusCode,
  124. Body: res.Body,
  125. Header: res.Header,
  126. }
  127. return &response, nil
  128. }
  129. // WithContext sets the request context.
  130. func (f MLGetCalendars) WithContext(v context.Context) func(*MLGetCalendarsRequest) {
  131. return func(r *MLGetCalendarsRequest) {
  132. r.ctx = v
  133. }
  134. }
  135. // WithBody - The from and size parameters optionally sent in the body.
  136. func (f MLGetCalendars) WithBody(v io.Reader) func(*MLGetCalendarsRequest) {
  137. return func(r *MLGetCalendarsRequest) {
  138. r.Body = v
  139. }
  140. }
  141. // WithCalendarID - the ID of the calendar to fetch.
  142. func (f MLGetCalendars) WithCalendarID(v string) func(*MLGetCalendarsRequest) {
  143. return func(r *MLGetCalendarsRequest) {
  144. r.CalendarID = v
  145. }
  146. }
  147. // WithFrom - skips a number of calendars.
  148. func (f MLGetCalendars) WithFrom(v int) func(*MLGetCalendarsRequest) {
  149. return func(r *MLGetCalendarsRequest) {
  150. r.From = &v
  151. }
  152. }
  153. // WithSize - specifies a max number of calendars to get.
  154. func (f MLGetCalendars) WithSize(v int) func(*MLGetCalendarsRequest) {
  155. return func(r *MLGetCalendarsRequest) {
  156. r.Size = &v
  157. }
  158. }
  159. // WithPretty makes the response body pretty-printed.
  160. func (f MLGetCalendars) WithPretty() func(*MLGetCalendarsRequest) {
  161. return func(r *MLGetCalendarsRequest) {
  162. r.Pretty = true
  163. }
  164. }
  165. // WithHuman makes statistical values human-readable.
  166. func (f MLGetCalendars) WithHuman() func(*MLGetCalendarsRequest) {
  167. return func(r *MLGetCalendarsRequest) {
  168. r.Human = true
  169. }
  170. }
  171. // WithErrorTrace includes the stack trace for errors in the response body.
  172. func (f MLGetCalendars) WithErrorTrace() func(*MLGetCalendarsRequest) {
  173. return func(r *MLGetCalendarsRequest) {
  174. r.ErrorTrace = true
  175. }
  176. }
  177. // WithFilterPath filters the properties of the response body.
  178. func (f MLGetCalendars) WithFilterPath(v ...string) func(*MLGetCalendarsRequest) {
  179. return func(r *MLGetCalendarsRequest) {
  180. r.FilterPath = v
  181. }
  182. }
  183. // WithHeader adds the headers to the HTTP request.
  184. func (f MLGetCalendars) WithHeader(h map[string]string) func(*MLGetCalendarsRequest) {
  185. return func(r *MLGetCalendarsRequest) {
  186. if r.Header == nil {
  187. r.Header = make(http.Header)
  188. }
  189. for k, v := range h {
  190. r.Header.Add(k, v)
  191. }
  192. }
  193. }
  194. // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
  195. func (f MLGetCalendars) WithOpaqueID(s string) func(*MLGetCalendarsRequest) {
  196. return func(r *MLGetCalendarsRequest) {
  197. if r.Header == nil {
  198. r.Header = make(http.Header)
  199. }
  200. r.Header.Set("X-Opaque-Id", s)
  201. }
  202. }