github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.cat.count.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 28 package opensearchapi 29 30 import ( 31 "context" 32 "net/http" 33 "strconv" 34 "strings" 35 ) 36 37 func newCatCountFunc(t Transport) CatCount { 38 return func(o ...func(*CatCountRequest)) (*Response, error) { 39 var r = CatCountRequest{} 40 for _, f := range o { 41 f(&r) 42 } 43 return r.Do(r.ctx, t) 44 } 45 } 46 47 // ----- API Definition ------------------------------------------------------- 48 49 // CatCount provides quick access to the document count of the entire cluster, or individual indices. 50 // 51 // 52 type CatCount func(o ...func(*CatCountRequest)) (*Response, error) 53 54 // CatCountRequest configures the Cat Count API request. 55 // 56 type CatCountRequest struct { 57 Index []string 58 59 Format string 60 H []string 61 Help *bool 62 S []string 63 V *bool 64 65 Pretty bool 66 Human bool 67 ErrorTrace bool 68 FilterPath []string 69 70 Header http.Header 71 72 ctx context.Context 73 } 74 75 // Do executes the request and returns response or error. 76 // 77 func (r CatCountRequest) Do(ctx context.Context, transport Transport) (*Response, error) { 78 var ( 79 method string 80 path strings.Builder 81 params map[string]string 82 ) 83 84 method = "GET" 85 86 path.Grow(1 + len("_cat") + 1 + len("count") + 1 + len(strings.Join(r.Index, ","))) 87 path.WriteString("/") 88 path.WriteString("_cat") 89 path.WriteString("/") 90 path.WriteString("count") 91 if len(r.Index) > 0 { 92 path.WriteString("/") 93 path.WriteString(strings.Join(r.Index, ",")) 94 } 95 96 params = make(map[string]string) 97 98 if r.Format != "" { 99 params["format"] = r.Format 100 } 101 102 if len(r.H) > 0 { 103 params["h"] = strings.Join(r.H, ",") 104 } 105 106 if r.Help != nil { 107 params["help"] = strconv.FormatBool(*r.Help) 108 } 109 110 if len(r.S) > 0 { 111 params["s"] = strings.Join(r.S, ",") 112 } 113 114 if r.V != nil { 115 params["v"] = strconv.FormatBool(*r.V) 116 } 117 118 if r.Pretty { 119 params["pretty"] = "true" 120 } 121 122 if r.Human { 123 params["human"] = "true" 124 } 125 126 if r.ErrorTrace { 127 params["error_trace"] = "true" 128 } 129 130 if len(r.FilterPath) > 0 { 131 params["filter_path"] = strings.Join(r.FilterPath, ",") 132 } 133 134 req, err := newRequest(method, path.String(), nil) 135 if err != nil { 136 return nil, err 137 } 138 139 if len(params) > 0 { 140 q := req.URL.Query() 141 for k, v := range params { 142 q.Set(k, v) 143 } 144 req.URL.RawQuery = q.Encode() 145 } 146 147 if len(r.Header) > 0 { 148 if len(req.Header) == 0 { 149 req.Header = r.Header 150 } else { 151 for k, vv := range r.Header { 152 for _, v := range vv { 153 req.Header.Add(k, v) 154 } 155 } 156 } 157 } 158 159 if ctx != nil { 160 req = req.WithContext(ctx) 161 } 162 163 res, err := transport.Perform(req) 164 if err != nil { 165 return nil, err 166 } 167 168 response := Response{ 169 StatusCode: res.StatusCode, 170 Body: res.Body, 171 Header: res.Header, 172 } 173 174 return &response, nil 175 } 176 177 // WithContext sets the request context. 178 // 179 func (f CatCount) WithContext(v context.Context) func(*CatCountRequest) { 180 return func(r *CatCountRequest) { 181 r.ctx = v 182 } 183 } 184 185 // WithIndex - a list of index names to limit the returned information. 186 // 187 func (f CatCount) WithIndex(v ...string) func(*CatCountRequest) { 188 return func(r *CatCountRequest) { 189 r.Index = v 190 } 191 } 192 193 // WithFormat - a short version of the accept header, e.g. json, yaml. 194 // 195 func (f CatCount) WithFormat(v string) func(*CatCountRequest) { 196 return func(r *CatCountRequest) { 197 r.Format = v 198 } 199 } 200 201 // WithH - comma-separated list of column names to display. 202 // 203 func (f CatCount) WithH(v ...string) func(*CatCountRequest) { 204 return func(r *CatCountRequest) { 205 r.H = v 206 } 207 } 208 209 // WithHelp - return help information. 210 // 211 func (f CatCount) WithHelp(v bool) func(*CatCountRequest) { 212 return func(r *CatCountRequest) { 213 r.Help = &v 214 } 215 } 216 217 // WithS - comma-separated list of column names or column aliases to sort by. 218 // 219 func (f CatCount) WithS(v ...string) func(*CatCountRequest) { 220 return func(r *CatCountRequest) { 221 r.S = v 222 } 223 } 224 225 // WithV - verbose mode. display column headers. 226 // 227 func (f CatCount) WithV(v bool) func(*CatCountRequest) { 228 return func(r *CatCountRequest) { 229 r.V = &v 230 } 231 } 232 233 // WithPretty makes the response body pretty-printed. 234 // 235 func (f CatCount) WithPretty() func(*CatCountRequest) { 236 return func(r *CatCountRequest) { 237 r.Pretty = true 238 } 239 } 240 241 // WithHuman makes statistical values human-readable. 242 // 243 func (f CatCount) WithHuman() func(*CatCountRequest) { 244 return func(r *CatCountRequest) { 245 r.Human = true 246 } 247 } 248 249 // WithErrorTrace includes the stack trace for errors in the response body. 250 // 251 func (f CatCount) WithErrorTrace() func(*CatCountRequest) { 252 return func(r *CatCountRequest) { 253 r.ErrorTrace = true 254 } 255 } 256 257 // WithFilterPath filters the properties of the response body. 258 // 259 func (f CatCount) WithFilterPath(v ...string) func(*CatCountRequest) { 260 return func(r *CatCountRequest) { 261 r.FilterPath = v 262 } 263 } 264 265 // WithHeader adds the headers to the HTTP request. 266 // 267 func (f CatCount) WithHeader(h map[string]string) func(*CatCountRequest) { 268 return func(r *CatCountRequest) { 269 if r.Header == nil { 270 r.Header = make(http.Header) 271 } 272 for k, v := range h { 273 r.Header.Add(k, v) 274 } 275 } 276 } 277 278 // WithOpaqueID adds the X-Opaque-Id header to the HTTP request. 279 // 280 func (f CatCount) WithOpaqueID(s string) func(*CatCountRequest) { 281 return func(r *CatCountRequest) { 282 if r.Header == nil { 283 r.Header = make(http.Header) 284 } 285 r.Header.Set("X-Opaque-Id", s) 286 } 287 }