api.xpack.sql.translate.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 newSQLTranslateFunc(t Transport) SQLTranslate {
  27. return func(body io.Reader, o ...func(*SQLTranslateRequest)) (*Response, error) {
  28. var r = SQLTranslateRequest{Body: body}
  29. for _, f := range o {
  30. f(&r)
  31. }
  32. return r.Do(r.ctx, t)
  33. }
  34. }
  35. // ----- API Definition -------------------------------------------------------
  36. // SQLTranslate - Translates SQL into Elasticsearch queries
  37. //
  38. // See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/current/sql-translate-api.html.
  39. type SQLTranslate func(body io.Reader, o ...func(*SQLTranslateRequest)) (*Response, error)
  40. // SQLTranslateRequest configures the SQL Translate API request.
  41. type SQLTranslateRequest struct {
  42. Body io.Reader
  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 SQLTranslateRequest) 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 = "POST"
  58. path.Grow(len("/_sql/translate"))
  59. path.WriteString("/_sql/translate")
  60. params = make(map[string]string)
  61. if r.Pretty {
  62. params["pretty"] = "true"
  63. }
  64. if r.Human {
  65. params["human"] = "true"
  66. }
  67. if r.ErrorTrace {
  68. params["error_trace"] = "true"
  69. }
  70. if len(r.FilterPath) > 0 {
  71. params["filter_path"] = strings.Join(r.FilterPath, ",")
  72. }
  73. req, err := newRequest(method, path.String(), r.Body)
  74. if err != nil {
  75. return nil, err
  76. }
  77. if len(params) > 0 {
  78. q := req.URL.Query()
  79. for k, v := range params {
  80. q.Set(k, v)
  81. }
  82. req.URL.RawQuery = q.Encode()
  83. }
  84. if len(r.Header) > 0 {
  85. if len(req.Header) == 0 {
  86. req.Header = r.Header
  87. } else {
  88. for k, vv := range r.Header {
  89. for _, v := range vv {
  90. req.Header.Add(k, v)
  91. }
  92. }
  93. }
  94. }
  95. if r.Body != nil && req.Header.Get(headerContentType) == "" {
  96. req.Header[headerContentType] = headerContentTypeJSON
  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 SQLTranslate) WithContext(v context.Context) func(*SQLTranslateRequest) {
  114. return func(r *SQLTranslateRequest) {
  115. r.ctx = v
  116. }
  117. }
  118. // WithPretty makes the response body pretty-printed.
  119. func (f SQLTranslate) WithPretty() func(*SQLTranslateRequest) {
  120. return func(r *SQLTranslateRequest) {
  121. r.Pretty = true
  122. }
  123. }
  124. // WithHuman makes statistical values human-readable.
  125. func (f SQLTranslate) WithHuman() func(*SQLTranslateRequest) {
  126. return func(r *SQLTranslateRequest) {
  127. r.Human = true
  128. }
  129. }
  130. // WithErrorTrace includes the stack trace for errors in the response body.
  131. func (f SQLTranslate) WithErrorTrace() func(*SQLTranslateRequest) {
  132. return func(r *SQLTranslateRequest) {
  133. r.ErrorTrace = true
  134. }
  135. }
  136. // WithFilterPath filters the properties of the response body.
  137. func (f SQLTranslate) WithFilterPath(v ...string) func(*SQLTranslateRequest) {
  138. return func(r *SQLTranslateRequest) {
  139. r.FilterPath = v
  140. }
  141. }
  142. // WithHeader adds the headers to the HTTP request.
  143. func (f SQLTranslate) WithHeader(h map[string]string) func(*SQLTranslateRequest) {
  144. return func(r *SQLTranslateRequest) {
  145. if r.Header == nil {
  146. r.Header = make(http.Header)
  147. }
  148. for k, v := range h {
  149. r.Header.Add(k, v)
  150. }
  151. }
  152. }
  153. // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
  154. func (f SQLTranslate) WithOpaqueID(s string) func(*SQLTranslateRequest) {
  155. return func(r *SQLTranslateRequest) {
  156. if r.Header == nil {
  157. r.Header = make(http.Header)
  158. }
  159. r.Header.Set("X-Opaque-Id", s)
  160. }
  161. }