api.cat.count.go 5.9 KB

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