github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.cat.templates.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 "time" 35 ) 36 37 func newCatTemplatesFunc(t Transport) CatTemplates { 38 return func(o ...func(*CatTemplatesRequest)) (*Response, error) { 39 var r = CatTemplatesRequest{} 40 for _, f := range o { 41 f(&r) 42 } 43 return r.Do(r.ctx, t) 44 } 45 } 46 47 // ----- API Definition ------------------------------------------------------- 48 49 // CatTemplates returns information about existing templates. 50 // 51 // 52 type CatTemplates func(o ...func(*CatTemplatesRequest)) (*Response, error) 53 54 // CatTemplatesRequest configures the Cat Templates API request. 55 // 56 type CatTemplatesRequest struct { 57 Name string 58 59 Format string 60 H []string 61 Help *bool 62 Local *bool 63 MasterTimeout time.Duration 64 ClusterManagerTimeout time.Duration 65 S []string 66 V *bool 67 68 Pretty bool 69 Human bool 70 ErrorTrace bool 71 FilterPath []string 72 73 Header http.Header 74 75 ctx context.Context 76 } 77 78 // Do executes the request and returns response or error. 79 // 80 func (r CatTemplatesRequest) Do(ctx context.Context, transport Transport) (*Response, error) { 81 var ( 82 method string 83 path strings.Builder 84 params map[string]string 85 ) 86 87 method = "GET" 88 89 path.Grow(1 + len("_cat") + 1 + len("templates") + 1 + len(r.Name)) 90 path.WriteString("/") 91 path.WriteString("_cat") 92 path.WriteString("/") 93 path.WriteString("templates") 94 if r.Name != "" { 95 path.WriteString("/") 96 path.WriteString(r.Name) 97 } 98 99 params = make(map[string]string) 100 101 if r.Format != "" { 102 params["format"] = r.Format 103 } 104 105 if len(r.H) > 0 { 106 params["h"] = strings.Join(r.H, ",") 107 } 108 109 if r.Help != nil { 110 params["help"] = strconv.FormatBool(*r.Help) 111 } 112 113 if r.Local != nil { 114 params["local"] = strconv.FormatBool(*r.Local) 115 } 116 117 if r.MasterTimeout != 0 { 118 params["master_timeout"] = formatDuration(r.MasterTimeout) 119 } 120 121 if r.ClusterManagerTimeout != 0 { 122 params["cluster_manager_timeout"] = formatDuration(r.ClusterManagerTimeout) 123 } 124 125 if len(r.S) > 0 { 126 params["s"] = strings.Join(r.S, ",") 127 } 128 129 if r.V != nil { 130 params["v"] = strconv.FormatBool(*r.V) 131 } 132 133 if r.Pretty { 134 params["pretty"] = "true" 135 } 136 137 if r.Human { 138 params["human"] = "true" 139 } 140 141 if r.ErrorTrace { 142 params["error_trace"] = "true" 143 } 144 145 if len(r.FilterPath) > 0 { 146 params["filter_path"] = strings.Join(r.FilterPath, ",") 147 } 148 149 req, err := newRequest(method, path.String(), nil) 150 if err != nil { 151 return nil, err 152 } 153 154 if len(params) > 0 { 155 q := req.URL.Query() 156 for k, v := range params { 157 q.Set(k, v) 158 } 159 req.URL.RawQuery = q.Encode() 160 } 161 162 if len(r.Header) > 0 { 163 if len(req.Header) == 0 { 164 req.Header = r.Header 165 } else { 166 for k, vv := range r.Header { 167 for _, v := range vv { 168 req.Header.Add(k, v) 169 } 170 } 171 } 172 } 173 174 if ctx != nil { 175 req = req.WithContext(ctx) 176 } 177 178 res, err := transport.Perform(req) 179 if err != nil { 180 return nil, err 181 } 182 183 response := Response{ 184 StatusCode: res.StatusCode, 185 Body: res.Body, 186 Header: res.Header, 187 } 188 189 return &response, nil 190 } 191 192 // WithContext sets the request context. 193 // 194 func (f CatTemplates) WithContext(v context.Context) func(*CatTemplatesRequest) { 195 return func(r *CatTemplatesRequest) { 196 r.ctx = v 197 } 198 } 199 200 // WithName - a pattern that returned template names must match. 201 // 202 func (f CatTemplates) WithName(v string) func(*CatTemplatesRequest) { 203 return func(r *CatTemplatesRequest) { 204 r.Name = v 205 } 206 } 207 208 // WithFormat - a short version of the accept header, e.g. json, yaml. 209 // 210 func (f CatTemplates) WithFormat(v string) func(*CatTemplatesRequest) { 211 return func(r *CatTemplatesRequest) { 212 r.Format = v 213 } 214 } 215 216 // WithH - comma-separated list of column names to display. 217 // 218 func (f CatTemplates) WithH(v ...string) func(*CatTemplatesRequest) { 219 return func(r *CatTemplatesRequest) { 220 r.H = v 221 } 222 } 223 224 // WithHelp - return help information. 225 // 226 func (f CatTemplates) WithHelp(v bool) func(*CatTemplatesRequest) { 227 return func(r *CatTemplatesRequest) { 228 r.Help = &v 229 } 230 } 231 232 // WithLocal - return local information, do not retrieve the state from cluster-manager node (default: false). 233 // 234 func (f CatTemplates) WithLocal(v bool) func(*CatTemplatesRequest) { 235 return func(r *CatTemplatesRequest) { 236 r.Local = &v 237 } 238 } 239 240 // WithMasterTimeout - explicit operation timeout for connection to cluster-manager node. 241 // 242 // Deprecated: To promote inclusive language, use WithClusterManagerTimeout instead. 243 // 244 func (f CatTemplates) WithMasterTimeout(v time.Duration) func(*CatTemplatesRequest) { 245 return func(r *CatTemplatesRequest) { 246 r.MasterTimeout = v 247 } 248 } 249 250 // WithClusterManagerTimeout - explicit operation timeout for connection to cluster-manager node. 251 // 252 func (f CatTemplates) WithClusterManagerTimeout(v time.Duration) func(*CatTemplatesRequest) { 253 return func(r *CatTemplatesRequest) { 254 r.ClusterManagerTimeout = v 255 } 256 } 257 258 // WithS - comma-separated list of column names or column aliases to sort by. 259 // 260 func (f CatTemplates) WithS(v ...string) func(*CatTemplatesRequest) { 261 return func(r *CatTemplatesRequest) { 262 r.S = v 263 } 264 } 265 266 // WithV - verbose mode. display column headers. 267 // 268 func (f CatTemplates) WithV(v bool) func(*CatTemplatesRequest) { 269 return func(r *CatTemplatesRequest) { 270 r.V = &v 271 } 272 } 273 274 // WithPretty makes the response body pretty-printed. 275 // 276 func (f CatTemplates) WithPretty() func(*CatTemplatesRequest) { 277 return func(r *CatTemplatesRequest) { 278 r.Pretty = true 279 } 280 } 281 282 // WithHuman makes statistical values human-readable. 283 // 284 func (f CatTemplates) WithHuman() func(*CatTemplatesRequest) { 285 return func(r *CatTemplatesRequest) { 286 r.Human = true 287 } 288 } 289 290 // WithErrorTrace includes the stack trace for errors in the response body. 291 // 292 func (f CatTemplates) WithErrorTrace() func(*CatTemplatesRequest) { 293 return func(r *CatTemplatesRequest) { 294 r.ErrorTrace = true 295 } 296 } 297 298 // WithFilterPath filters the properties of the response body. 299 // 300 func (f CatTemplates) WithFilterPath(v ...string) func(*CatTemplatesRequest) { 301 return func(r *CatTemplatesRequest) { 302 r.FilterPath = v 303 } 304 } 305 306 // WithHeader adds the headers to the HTTP request. 307 // 308 func (f CatTemplates) WithHeader(h map[string]string) func(*CatTemplatesRequest) { 309 return func(r *CatTemplatesRequest) { 310 if r.Header == nil { 311 r.Header = make(http.Header) 312 } 313 for k, v := range h { 314 r.Header.Add(k, v) 315 } 316 } 317 } 318 319 // WithOpaqueID adds the X-Opaque-Id header to the HTTP request. 320 // 321 func (f CatTemplates) WithOpaqueID(s string) func(*CatTemplatesRequest) { 322 return func(r *CatTemplatesRequest) { 323 if r.Header == nil { 324 r.Header = make(http.Header) 325 } 326 r.Header.Set("X-Opaque-Id", s) 327 } 328 }