api.cat.templates.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. "net/http"
  23. "strconv"
  24. "strings"
  25. "time"
  26. )
  27. func newCatTemplatesFunc(t Transport) CatTemplates {
  28. return func(o ...func(*CatTemplatesRequest)) (*Response, error) {
  29. var r = CatTemplatesRequest{}
  30. for _, f := range o {
  31. f(&r)
  32. }
  33. return r.Do(r.ctx, t)
  34. }
  35. }
  36. // ----- API Definition -------------------------------------------------------
  37. // CatTemplates returns information about existing templates.
  38. //
  39. // See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/master/cat-templates.html.
  40. type CatTemplates func(o ...func(*CatTemplatesRequest)) (*Response, error)
  41. // CatTemplatesRequest configures the Cat Templates API request.
  42. type CatTemplatesRequest struct {
  43. Name string
  44. Format string
  45. H []string
  46. Help *bool
  47. Local *bool
  48. MasterTimeout time.Duration
  49. S []string
  50. V *bool
  51. Pretty bool
  52. Human bool
  53. ErrorTrace bool
  54. FilterPath []string
  55. Header http.Header
  56. ctx context.Context
  57. }
  58. // Do executes the request and returns response or error.
  59. func (r CatTemplatesRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
  60. var (
  61. method string
  62. path strings.Builder
  63. params map[string]string
  64. )
  65. method = "GET"
  66. path.Grow(1 + len("_cat") + 1 + len("templates") + 1 + len(r.Name))
  67. path.WriteString("/")
  68. path.WriteString("_cat")
  69. path.WriteString("/")
  70. path.WriteString("templates")
  71. if r.Name != "" {
  72. path.WriteString("/")
  73. path.WriteString(r.Name)
  74. }
  75. params = make(map[string]string)
  76. if r.Format != "" {
  77. params["format"] = r.Format
  78. }
  79. if len(r.H) > 0 {
  80. params["h"] = strings.Join(r.H, ",")
  81. }
  82. if r.Help != nil {
  83. params["help"] = strconv.FormatBool(*r.Help)
  84. }
  85. if r.Local != nil {
  86. params["local"] = strconv.FormatBool(*r.Local)
  87. }
  88. if r.MasterTimeout != 0 {
  89. params["master_timeout"] = formatDuration(r.MasterTimeout)
  90. }
  91. if len(r.S) > 0 {
  92. params["s"] = strings.Join(r.S, ",")
  93. }
  94. if r.V != nil {
  95. params["v"] = strconv.FormatBool(*r.V)
  96. }
  97. if r.Pretty {
  98. params["pretty"] = "true"
  99. }
  100. if r.Human {
  101. params["human"] = "true"
  102. }
  103. if r.ErrorTrace {
  104. params["error_trace"] = "true"
  105. }
  106. if len(r.FilterPath) > 0 {
  107. params["filter_path"] = strings.Join(r.FilterPath, ",")
  108. }
  109. req, err := newRequest(method, path.String(), nil)
  110. if err != nil {
  111. return nil, err
  112. }
  113. if len(params) > 0 {
  114. q := req.URL.Query()
  115. for k, v := range params {
  116. q.Set(k, v)
  117. }
  118. req.URL.RawQuery = q.Encode()
  119. }
  120. if len(r.Header) > 0 {
  121. if len(req.Header) == 0 {
  122. req.Header = r.Header
  123. } else {
  124. for k, vv := range r.Header {
  125. for _, v := range vv {
  126. req.Header.Add(k, v)
  127. }
  128. }
  129. }
  130. }
  131. if ctx != nil {
  132. req = req.WithContext(ctx)
  133. }
  134. res, err := transport.Perform(req)
  135. if err != nil {
  136. return nil, err
  137. }
  138. response := Response{
  139. StatusCode: res.StatusCode,
  140. Body: res.Body,
  141. Header: res.Header,
  142. }
  143. return &response, nil
  144. }
  145. // WithContext sets the request context.
  146. func (f CatTemplates) WithContext(v context.Context) func(*CatTemplatesRequest) {
  147. return func(r *CatTemplatesRequest) {
  148. r.ctx = v
  149. }
  150. }
  151. // WithName - a pattern that returned template names must match.
  152. func (f CatTemplates) WithName(v string) func(*CatTemplatesRequest) {
  153. return func(r *CatTemplatesRequest) {
  154. r.Name = v
  155. }
  156. }
  157. // WithFormat - a short version of the accept header, e.g. json, yaml.
  158. func (f CatTemplates) WithFormat(v string) func(*CatTemplatesRequest) {
  159. return func(r *CatTemplatesRequest) {
  160. r.Format = v
  161. }
  162. }
  163. // WithH - comma-separated list of column names to display.
  164. func (f CatTemplates) WithH(v ...string) func(*CatTemplatesRequest) {
  165. return func(r *CatTemplatesRequest) {
  166. r.H = v
  167. }
  168. }
  169. // WithHelp - return help information.
  170. func (f CatTemplates) WithHelp(v bool) func(*CatTemplatesRequest) {
  171. return func(r *CatTemplatesRequest) {
  172. r.Help = &v
  173. }
  174. }
  175. // WithLocal - return local information, do not retrieve the state from master node (default: false).
  176. func (f CatTemplates) WithLocal(v bool) func(*CatTemplatesRequest) {
  177. return func(r *CatTemplatesRequest) {
  178. r.Local = &v
  179. }
  180. }
  181. // WithMasterTimeout - explicit operation timeout for connection to master node.
  182. func (f CatTemplates) WithMasterTimeout(v time.Duration) func(*CatTemplatesRequest) {
  183. return func(r *CatTemplatesRequest) {
  184. r.MasterTimeout = v
  185. }
  186. }
  187. // WithS - comma-separated list of column names or column aliases to sort by.
  188. func (f CatTemplates) WithS(v ...string) func(*CatTemplatesRequest) {
  189. return func(r *CatTemplatesRequest) {
  190. r.S = v
  191. }
  192. }
  193. // WithV - verbose mode. display column headers.
  194. func (f CatTemplates) WithV(v bool) func(*CatTemplatesRequest) {
  195. return func(r *CatTemplatesRequest) {
  196. r.V = &v
  197. }
  198. }
  199. // WithPretty makes the response body pretty-printed.
  200. func (f CatTemplates) WithPretty() func(*CatTemplatesRequest) {
  201. return func(r *CatTemplatesRequest) {
  202. r.Pretty = true
  203. }
  204. }
  205. // WithHuman makes statistical values human-readable.
  206. func (f CatTemplates) WithHuman() func(*CatTemplatesRequest) {
  207. return func(r *CatTemplatesRequest) {
  208. r.Human = true
  209. }
  210. }
  211. // WithErrorTrace includes the stack trace for errors in the response body.
  212. func (f CatTemplates) WithErrorTrace() func(*CatTemplatesRequest) {
  213. return func(r *CatTemplatesRequest) {
  214. r.ErrorTrace = true
  215. }
  216. }
  217. // WithFilterPath filters the properties of the response body.
  218. func (f CatTemplates) WithFilterPath(v ...string) func(*CatTemplatesRequest) {
  219. return func(r *CatTemplatesRequest) {
  220. r.FilterPath = v
  221. }
  222. }
  223. // WithHeader adds the headers to the HTTP request.
  224. func (f CatTemplates) WithHeader(h map[string]string) func(*CatTemplatesRequest) {
  225. return func(r *CatTemplatesRequest) {
  226. if r.Header == nil {
  227. r.Header = make(http.Header)
  228. }
  229. for k, v := range h {
  230. r.Header.Add(k, v)
  231. }
  232. }
  233. }
  234. // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
  235. func (f CatTemplates) WithOpaqueID(s string) func(*CatTemplatesRequest) {
  236. return func(r *CatTemplatesRequest) {
  237. if r.Header == nil {
  238. r.Header = make(http.Header)
  239. }
  240. r.Header.Set("X-Opaque-Id", s)
  241. }
  242. }