github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.cat.help.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // 3 // The OpenSearch Contributors require contributions made to 4 // this file be licensed under the Apache-2.0 license or a 5 // compatible open source license. 6 // 7 // Modifications Copyright OpenSearch Contributors. See 8 // GitHub history for details. 9 10 // Licensed to Elasticsearch B.V. under one or more contributor 11 // license agreements. See the NOTICE file distributed with 12 // this work for additional information regarding copyright 13 // ownership. Elasticsearch B.V. licenses this file to you under 14 // the Apache License, Version 2.0 (the "License"); you may 15 // not use this file except in compliance with the License. 16 // You may obtain a copy of the License at 17 // 18 // http://www.apache.org/licenses/LICENSE-2.0 19 // 20 // Unless required by applicable law or agreed to in writing, 21 // software distributed under the License is distributed on an 22 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 23 // KIND, either express or implied. See the License for the 24 // specific language governing permissions and limitations 25 // under the License. 26 27 package opensearchapi 28 29 import ( 30 "context" 31 "net/http" 32 "strconv" 33 "strings" 34 ) 35 36 func newCatHelpFunc(t Transport) CatHelp { 37 return func(o ...func(*CatHelpRequest)) (*Response, error) { 38 var r = CatHelpRequest{} 39 for _, f := range o { 40 f(&r) 41 } 42 return r.Do(r.ctx, t) 43 } 44 } 45 46 // ----- API Definition ------------------------------------------------------- 47 48 // CatHelp returns help for the Cat APIs. 49 // 50 // 51 type CatHelp func(o ...func(*CatHelpRequest)) (*Response, error) 52 53 // CatHelpRequest configures the Cat Help API request. 54 // 55 type CatHelpRequest struct { 56 Help *bool 57 S []string 58 59 Pretty bool 60 Human bool 61 ErrorTrace bool 62 FilterPath []string 63 64 Header http.Header 65 66 ctx context.Context 67 } 68 69 // Do executes the request and returns response or error. 70 // 71 func (r CatHelpRequest) Do(ctx context.Context, transport Transport) (*Response, error) { 72 var ( 73 method string 74 path strings.Builder 75 params map[string]string 76 ) 77 78 method = "GET" 79 80 path.Grow(len("/_cat")) 81 path.WriteString("/_cat") 82 83 params = make(map[string]string) 84 85 if r.Help != nil { 86 params["help"] = strconv.FormatBool(*r.Help) 87 } 88 89 if len(r.S) > 0 { 90 params["s"] = strings.Join(r.S, ",") 91 } 92 93 if r.Pretty { 94 params["pretty"] = "true" 95 } 96 97 if r.Human { 98 params["human"] = "true" 99 } 100 101 if r.ErrorTrace { 102 params["error_trace"] = "true" 103 } 104 105 if len(r.FilterPath) > 0 { 106 params["filter_path"] = strings.Join(r.FilterPath, ",") 107 } 108 109 req, err := newRequest(method, path.String(), nil) 110 if err != nil { 111 return nil, err 112 } 113 114 if len(params) > 0 { 115 q := req.URL.Query() 116 for k, v := range params { 117 q.Set(k, v) 118 } 119 req.URL.RawQuery = q.Encode() 120 } 121 122 if len(r.Header) > 0 { 123 if len(req.Header) == 0 { 124 req.Header = r.Header 125 } else { 126 for k, vv := range r.Header { 127 for _, v := range vv { 128 req.Header.Add(k, v) 129 } 130 } 131 } 132 } 133 134 if ctx != nil { 135 req = req.WithContext(ctx) 136 } 137 138 res, err := transport.Perform(req) 139 if err != nil { 140 return nil, err 141 } 142 143 response := Response{ 144 StatusCode: res.StatusCode, 145 Body: res.Body, 146 Header: res.Header, 147 } 148 149 return &response, nil 150 } 151 152 // WithContext sets the request context. 153 // 154 func (f CatHelp) WithContext(v context.Context) func(*CatHelpRequest) { 155 return func(r *CatHelpRequest) { 156 r.ctx = v 157 } 158 } 159 160 // WithHelp - return help information. 161 // 162 func (f CatHelp) WithHelp(v bool) func(*CatHelpRequest) { 163 return func(r *CatHelpRequest) { 164 r.Help = &v 165 } 166 } 167 168 // WithS - comma-separated list of column names or column aliases to sort by. 169 // 170 func (f CatHelp) WithS(v ...string) func(*CatHelpRequest) { 171 return func(r *CatHelpRequest) { 172 r.S = v 173 } 174 } 175 176 // WithPretty makes the response body pretty-printed. 177 // 178 func (f CatHelp) WithPretty() func(*CatHelpRequest) { 179 return func(r *CatHelpRequest) { 180 r.Pretty = true 181 } 182 } 183 184 // WithHuman makes statistical values human-readable. 185 // 186 func (f CatHelp) WithHuman() func(*CatHelpRequest) { 187 return func(r *CatHelpRequest) { 188 r.Human = true 189 } 190 } 191 192 // WithErrorTrace includes the stack trace for errors in the response body. 193 // 194 func (f CatHelp) WithErrorTrace() func(*CatHelpRequest) { 195 return func(r *CatHelpRequest) { 196 r.ErrorTrace = true 197 } 198 } 199 200 // WithFilterPath filters the properties of the response body. 201 // 202 func (f CatHelp) WithFilterPath(v ...string) func(*CatHelpRequest) { 203 return func(r *CatHelpRequest) { 204 r.FilterPath = v 205 } 206 } 207 208 // WithHeader adds the headers to the HTTP request. 209 // 210 func (f CatHelp) WithHeader(h map[string]string) func(*CatHelpRequest) { 211 return func(r *CatHelpRequest) { 212 if r.Header == nil { 213 r.Header = make(http.Header) 214 } 215 for k, v := range h { 216 r.Header.Add(k, v) 217 } 218 } 219 } 220 221 // WithOpaqueID adds the X-Opaque-Id header to the HTTP request. 222 // 223 func (f CatHelp) WithOpaqueID(s string) func(*CatHelpRequest) { 224 return func(r *CatHelpRequest) { 225 if r.Header == nil { 226 r.Header = make(http.Header) 227 } 228 r.Header.Set("X-Opaque-Id", s) 229 } 230 }