api.xpack.security.authenticate.go 4.7 KB

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