github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.update_by_query_rethrottle.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 newUpdateByQueryRethrottleFunc(t Transport) UpdateByQueryRethrottle { 37 return func(task_id string, requests_per_second *int, o ...func(*UpdateByQueryRethrottleRequest)) (*Response, error) { 38 var r = UpdateByQueryRethrottleRequest{TaskID: task_id, RequestsPerSecond: requests_per_second} 39 for _, f := range o { 40 f(&r) 41 } 42 return r.Do(r.ctx, t) 43 } 44 } 45 46 // ----- API Definition ------------------------------------------------------- 47 48 // UpdateByQueryRethrottle changes the number of requests per second for a particular Update By Query operation. 49 // 50 // 51 type UpdateByQueryRethrottle func(task_id string, requests_per_second *int, o ...func(*UpdateByQueryRethrottleRequest)) (*Response, error) 52 53 // UpdateByQueryRethrottleRequest configures the Update By Query Rethrottle API request. 54 // 55 type UpdateByQueryRethrottleRequest struct { 56 TaskID string 57 58 RequestsPerSecond *int 59 60 Pretty bool 61 Human bool 62 ErrorTrace bool 63 FilterPath []string 64 65 Header http.Header 66 67 ctx context.Context 68 } 69 70 // Do executes the request and returns response or error. 71 // 72 func (r UpdateByQueryRethrottleRequest) Do(ctx context.Context, transport Transport) (*Response, error) { 73 var ( 74 method string 75 path strings.Builder 76 params map[string]string 77 ) 78 79 method = "POST" 80 81 path.Grow(1 + len("_update_by_query") + 1 + len(r.TaskID) + 1 + len("_rethrottle")) 82 path.WriteString("/") 83 path.WriteString("_update_by_query") 84 path.WriteString("/") 85 path.WriteString(r.TaskID) 86 path.WriteString("/") 87 path.WriteString("_rethrottle") 88 89 params = make(map[string]string) 90 91 if r.RequestsPerSecond != nil { 92 params["requests_per_second"] = strconv.FormatInt(int64(*r.RequestsPerSecond), 10) 93 } 94 95 if r.Pretty { 96 params["pretty"] = "true" 97 } 98 99 if r.Human { 100 params["human"] = "true" 101 } 102 103 if r.ErrorTrace { 104 params["error_trace"] = "true" 105 } 106 107 if len(r.FilterPath) > 0 { 108 params["filter_path"] = strings.Join(r.FilterPath, ",") 109 } 110 111 req, err := newRequest(method, path.String(), nil) 112 if err != nil { 113 return nil, err 114 } 115 116 if len(params) > 0 { 117 q := req.URL.Query() 118 for k, v := range params { 119 q.Set(k, v) 120 } 121 req.URL.RawQuery = q.Encode() 122 } 123 124 if len(r.Header) > 0 { 125 if len(req.Header) == 0 { 126 req.Header = r.Header 127 } else { 128 for k, vv := range r.Header { 129 for _, v := range vv { 130 req.Header.Add(k, v) 131 } 132 } 133 } 134 } 135 136 if ctx != nil { 137 req = req.WithContext(ctx) 138 } 139 140 res, err := transport.Perform(req) 141 if err != nil { 142 return nil, err 143 } 144 145 response := Response{ 146 StatusCode: res.StatusCode, 147 Body: res.Body, 148 Header: res.Header, 149 } 150 151 return &response, nil 152 } 153 154 // WithContext sets the request context. 155 // 156 func (f UpdateByQueryRethrottle) WithContext(v context.Context) func(*UpdateByQueryRethrottleRequest) { 157 return func(r *UpdateByQueryRethrottleRequest) { 158 r.ctx = v 159 } 160 } 161 162 // WithRequestsPerSecond - the throttle to set on this request in floating sub-requests per second. -1 means set no throttle.. 163 // 164 func (f UpdateByQueryRethrottle) WithRequestsPerSecond(v int) func(*UpdateByQueryRethrottleRequest) { 165 return func(r *UpdateByQueryRethrottleRequest) { 166 r.RequestsPerSecond = &v 167 } 168 } 169 170 // WithPretty makes the response body pretty-printed. 171 // 172 func (f UpdateByQueryRethrottle) WithPretty() func(*UpdateByQueryRethrottleRequest) { 173 return func(r *UpdateByQueryRethrottleRequest) { 174 r.Pretty = true 175 } 176 } 177 178 // WithHuman makes statistical values human-readable. 179 // 180 func (f UpdateByQueryRethrottle) WithHuman() func(*UpdateByQueryRethrottleRequest) { 181 return func(r *UpdateByQueryRethrottleRequest) { 182 r.Human = true 183 } 184 } 185 186 // WithErrorTrace includes the stack trace for errors in the response body. 187 // 188 func (f UpdateByQueryRethrottle) WithErrorTrace() func(*UpdateByQueryRethrottleRequest) { 189 return func(r *UpdateByQueryRethrottleRequest) { 190 r.ErrorTrace = true 191 } 192 } 193 194 // WithFilterPath filters the properties of the response body. 195 // 196 func (f UpdateByQueryRethrottle) WithFilterPath(v ...string) func(*UpdateByQueryRethrottleRequest) { 197 return func(r *UpdateByQueryRethrottleRequest) { 198 r.FilterPath = v 199 } 200 } 201 202 // WithHeader adds the headers to the HTTP request. 203 // 204 func (f UpdateByQueryRethrottle) WithHeader(h map[string]string) func(*UpdateByQueryRethrottleRequest) { 205 return func(r *UpdateByQueryRethrottleRequest) { 206 if r.Header == nil { 207 r.Header = make(http.Header) 208 } 209 for k, v := range h { 210 r.Header.Add(k, v) 211 } 212 } 213 } 214 215 // WithOpaqueID adds the X-Opaque-Id header to the HTTP request. 216 // 217 func (f UpdateByQueryRethrottle) WithOpaqueID(s string) func(*UpdateByQueryRethrottleRequest) { 218 return func(r *UpdateByQueryRethrottleRequest) { 219 if r.Header == nil { 220 r.Header = make(http.Header) 221 } 222 r.Header.Set("X-Opaque-Id", s) 223 } 224 }