api.xpack.sql.get_async.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 newSQLGetAsyncFunc(t Transport) SQLGetAsync {
  27. return func(id string, o ...func(*SQLGetAsyncRequest)) (*Response, error) {
  28. var r = SQLGetAsyncRequest{DocumentID: id}
  29. for _, f := range o {
  30. f(&r)
  31. }
  32. return r.Do(r.ctx, t)
  33. }
  34. }
  35. // ----- API Definition -------------------------------------------------------
  36. // SQLGetAsync - Returns the current status and available results for an async SQL search or stored synchronous SQL search
  37. //
  38. // See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/master/get-async-sql-search-api.html.
  39. type SQLGetAsync func(id string, o ...func(*SQLGetAsyncRequest)) (*Response, error)
  40. // SQLGetAsyncRequest configures the SQL Get Async API request.
  41. type SQLGetAsyncRequest struct {
  42. DocumentID string
  43. Delimiter string
  44. Format string
  45. KeepAlive time.Duration
  46. WaitForCompletionTimeout time.Duration
  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 SQLGetAsyncRequest) 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 = "GET"
  62. path.Grow(1 + len("_sql") + 1 + len("async") + 1 + len(r.DocumentID))
  63. path.WriteString("/")
  64. path.WriteString("_sql")
  65. path.WriteString("/")
  66. path.WriteString("async")
  67. path.WriteString("/")
  68. path.WriteString(r.DocumentID)
  69. params = make(map[string]string)
  70. if r.Delimiter != "" {
  71. params["delimiter"] = r.Delimiter
  72. }
  73. if r.Format != "" {
  74. params["format"] = r.Format
  75. }
  76. if r.KeepAlive != 0 {
  77. params["keep_alive"] = formatDuration(r.KeepAlive)
  78. }
  79. if r.WaitForCompletionTimeout != 0 {
  80. params["wait_for_completion_timeout"] = formatDuration(r.WaitForCompletionTimeout)
  81. }
  82. if r.Pretty {
  83. params["pretty"] = "true"
  84. }
  85. if r.Human {
  86. params["human"] = "true"
  87. }
  88. if r.ErrorTrace {
  89. params["error_trace"] = "true"
  90. }
  91. if len(r.FilterPath) > 0 {
  92. params["filter_path"] = strings.Join(r.FilterPath, ",")
  93. }
  94. req, err := newRequest(method, path.String(), nil)
  95. if err != nil {
  96. return nil, err
  97. }
  98. if len(params) > 0 {
  99. q := req.URL.Query()
  100. for k, v := range params {
  101. q.Set(k, v)
  102. }
  103. req.URL.RawQuery = q.Encode()
  104. }
  105. if len(r.Header) > 0 {
  106. if len(req.Header) == 0 {
  107. req.Header = r.Header
  108. } else {
  109. for k, vv := range r.Header {
  110. for _, v := range vv {
  111. req.Header.Add(k, v)
  112. }
  113. }
  114. }
  115. }
  116. if ctx != nil {
  117. req = req.WithContext(ctx)
  118. }
  119. res, err := transport.Perform(req)
  120. if err != nil {
  121. return nil, err
  122. }
  123. response := Response{
  124. StatusCode: res.StatusCode,
  125. Body: res.Body,
  126. Header: res.Header,
  127. }
  128. return &response, nil
  129. }
  130. // WithContext sets the request context.
  131. func (f SQLGetAsync) WithContext(v context.Context) func(*SQLGetAsyncRequest) {
  132. return func(r *SQLGetAsyncRequest) {
  133. r.ctx = v
  134. }
  135. }
  136. // WithDelimiter - separator for csv results.
  137. func (f SQLGetAsync) WithDelimiter(v string) func(*SQLGetAsyncRequest) {
  138. return func(r *SQLGetAsyncRequest) {
  139. r.Delimiter = v
  140. }
  141. }
  142. // WithFormat - short version of the accept header, e.g. json, yaml.
  143. func (f SQLGetAsync) WithFormat(v string) func(*SQLGetAsyncRequest) {
  144. return func(r *SQLGetAsyncRequest) {
  145. r.Format = v
  146. }
  147. }
  148. // WithKeepAlive - retention period for the search and its results.
  149. func (f SQLGetAsync) WithKeepAlive(v time.Duration) func(*SQLGetAsyncRequest) {
  150. return func(r *SQLGetAsyncRequest) {
  151. r.KeepAlive = v
  152. }
  153. }
  154. // WithWaitForCompletionTimeout - duration to wait for complete results.
  155. func (f SQLGetAsync) WithWaitForCompletionTimeout(v time.Duration) func(*SQLGetAsyncRequest) {
  156. return func(r *SQLGetAsyncRequest) {
  157. r.WaitForCompletionTimeout = v
  158. }
  159. }
  160. // WithPretty makes the response body pretty-printed.
  161. func (f SQLGetAsync) WithPretty() func(*SQLGetAsyncRequest) {
  162. return func(r *SQLGetAsyncRequest) {
  163. r.Pretty = true
  164. }
  165. }
  166. // WithHuman makes statistical values human-readable.
  167. func (f SQLGetAsync) WithHuman() func(*SQLGetAsyncRequest) {
  168. return func(r *SQLGetAsyncRequest) {
  169. r.Human = true
  170. }
  171. }
  172. // WithErrorTrace includes the stack trace for errors in the response body.
  173. func (f SQLGetAsync) WithErrorTrace() func(*SQLGetAsyncRequest) {
  174. return func(r *SQLGetAsyncRequest) {
  175. r.ErrorTrace = true
  176. }
  177. }
  178. // WithFilterPath filters the properties of the response body.
  179. func (f SQLGetAsync) WithFilterPath(v ...string) func(*SQLGetAsyncRequest) {
  180. return func(r *SQLGetAsyncRequest) {
  181. r.FilterPath = v
  182. }
  183. }
  184. // WithHeader adds the headers to the HTTP request.
  185. func (f SQLGetAsync) WithHeader(h map[string]string) func(*SQLGetAsyncRequest) {
  186. return func(r *SQLGetAsyncRequest) {
  187. if r.Header == nil {
  188. r.Header = make(http.Header)
  189. }
  190. for k, v := range h {
  191. r.Header.Add(k, v)
  192. }
  193. }
  194. }
  195. // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
  196. func (f SQLGetAsync) WithOpaqueID(s string) func(*SQLGetAsyncRequest) {
  197. return func(r *SQLGetAsyncRequest) {
  198. if r.Header == nil {
  199. r.Header = make(http.Header)
  200. }
  201. r.Header.Set("X-Opaque-Id", s)
  202. }
  203. }