api.xpack.sql.query.go 4.7 KB

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