api.cat.segments.go 6.3 KB

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