api.xpack.xpack.usage.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. "strings"
  24. "time"
  25. )
  26. func newXPackUsageFunc(t Transport) XPackUsage {
  27. return func(o ...func(*XPackUsageRequest)) (*Response, error) {
  28. var r = XPackUsageRequest{}
  29. for _, f := range o {
  30. f(&r)
  31. }
  32. return r.Do(r.ctx, t)
  33. }
  34. }
  35. // ----- API Definition -------------------------------------------------------
  36. // XPackUsage - Retrieves usage information about the installed X-Pack features.
  37. //
  38. // See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/current/usage-api.html.
  39. type XPackUsage func(o ...func(*XPackUsageRequest)) (*Response, error)
  40. // XPackUsageRequest configures the X Pack Usage API request.
  41. type XPackUsageRequest struct {
  42. MasterTimeout time.Duration
  43. Pretty bool
  44. Human bool
  45. ErrorTrace bool
  46. FilterPath []string
  47. Header http.Header
  48. ctx context.Context
  49. }
  50. // Do executes the request and returns response or error.
  51. func (r XPackUsageRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
  52. var (
  53. method string
  54. path strings.Builder
  55. params map[string]string
  56. )
  57. method = "GET"
  58. path.Grow(len("/_xpack/usage"))
  59. path.WriteString("/_xpack/usage")
  60. params = make(map[string]string)
  61. if r.MasterTimeout != 0 {
  62. params["master_timeout"] = formatDuration(r.MasterTimeout)
  63. }
  64. if r.Pretty {
  65. params["pretty"] = "true"
  66. }
  67. if r.Human {
  68. params["human"] = "true"
  69. }
  70. if r.ErrorTrace {
  71. params["error_trace"] = "true"
  72. }
  73. if len(r.FilterPath) > 0 {
  74. params["filter_path"] = strings.Join(r.FilterPath, ",")
  75. }
  76. req, err := newRequest(method, path.String(), nil)
  77. if err != nil {
  78. return nil, err
  79. }
  80. if len(params) > 0 {
  81. q := req.URL.Query()
  82. for k, v := range params {
  83. q.Set(k, v)
  84. }
  85. req.URL.RawQuery = q.Encode()
  86. }
  87. if len(r.Header) > 0 {
  88. if len(req.Header) == 0 {
  89. req.Header = r.Header
  90. } else {
  91. for k, vv := range r.Header {
  92. for _, v := range vv {
  93. req.Header.Add(k, v)
  94. }
  95. }
  96. }
  97. }
  98. if ctx != nil {
  99. req = req.WithContext(ctx)
  100. }
  101. res, err := transport.Perform(req)
  102. if err != nil {
  103. return nil, err
  104. }
  105. response := Response{
  106. StatusCode: res.StatusCode,
  107. Body: res.Body,
  108. Header: res.Header,
  109. }
  110. return &response, nil
  111. }
  112. // WithContext sets the request context.
  113. func (f XPackUsage) WithContext(v context.Context) func(*XPackUsageRequest) {
  114. return func(r *XPackUsageRequest) {
  115. r.ctx = v
  116. }
  117. }
  118. // WithMasterTimeout - specify timeout for watch write operation.
  119. func (f XPackUsage) WithMasterTimeout(v time.Duration) func(*XPackUsageRequest) {
  120. return func(r *XPackUsageRequest) {
  121. r.MasterTimeout = v
  122. }
  123. }
  124. // WithPretty makes the response body pretty-printed.
  125. func (f XPackUsage) WithPretty() func(*XPackUsageRequest) {
  126. return func(r *XPackUsageRequest) {
  127. r.Pretty = true
  128. }
  129. }
  130. // WithHuman makes statistical values human-readable.
  131. func (f XPackUsage) WithHuman() func(*XPackUsageRequest) {
  132. return func(r *XPackUsageRequest) {
  133. r.Human = true
  134. }
  135. }
  136. // WithErrorTrace includes the stack trace for errors in the response body.
  137. func (f XPackUsage) WithErrorTrace() func(*XPackUsageRequest) {
  138. return func(r *XPackUsageRequest) {
  139. r.ErrorTrace = true
  140. }
  141. }
  142. // WithFilterPath filters the properties of the response body.
  143. func (f XPackUsage) WithFilterPath(v ...string) func(*XPackUsageRequest) {
  144. return func(r *XPackUsageRequest) {
  145. r.FilterPath = v
  146. }
  147. }
  148. // WithHeader adds the headers to the HTTP request.
  149. func (f XPackUsage) WithHeader(h map[string]string) func(*XPackUsageRequest) {
  150. return func(r *XPackUsageRequest) {
  151. if r.Header == nil {
  152. r.Header = make(http.Header)
  153. }
  154. for k, v := range h {
  155. r.Header.Add(k, v)
  156. }
  157. }
  158. }
  159. // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
  160. func (f XPackUsage) WithOpaqueID(s string) func(*XPackUsageRequest) {
  161. return func(r *XPackUsageRequest) {
  162. if r.Header == nil {
  163. r.Header = make(http.Header)
  164. }
  165. r.Header.Set("X-Opaque-Id", s)
  166. }
  167. }