github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.delete_script.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 "strings" 33 "time" 34 ) 35 36 func newDeleteScriptFunc(t Transport) DeleteScript { 37 return func(id string, o ...func(*DeleteScriptRequest)) (*Response, error) { 38 var r = DeleteScriptRequest{ScriptID: id} 39 for _, f := range o { 40 f(&r) 41 } 42 return r.Do(r.ctx, t) 43 } 44 } 45 46 // ----- API Definition ------------------------------------------------------- 47 48 // DeleteScript deletes a script. 49 // 50 // 51 type DeleteScript func(id string, o ...func(*DeleteScriptRequest)) (*Response, error) 52 53 // DeleteScriptRequest configures the Delete Script API request. 54 // 55 type DeleteScriptRequest struct { 56 ScriptID string 57 58 MasterTimeout time.Duration 59 ClusterManagerTimeout time.Duration 60 Timeout time.Duration 61 62 Pretty bool 63 Human bool 64 ErrorTrace bool 65 FilterPath []string 66 67 Header http.Header 68 69 ctx context.Context 70 } 71 72 // Do executes the request and returns response or error. 73 // 74 func (r DeleteScriptRequest) Do(ctx context.Context, transport Transport) (*Response, error) { 75 var ( 76 method string 77 path strings.Builder 78 params map[string]string 79 ) 80 81 method = "DELETE" 82 83 path.Grow(1 + len("_scripts") + 1 + len(r.ScriptID)) 84 path.WriteString("/") 85 path.WriteString("_scripts") 86 path.WriteString("/") 87 path.WriteString(r.ScriptID) 88 89 params = make(map[string]string) 90 91 if r.MasterTimeout != 0 { 92 params["master_timeout"] = formatDuration(r.MasterTimeout) 93 } 94 95 if r.ClusterManagerTimeout != 0 { 96 params["cluster_manager_timeout"] = formatDuration(r.ClusterManagerTimeout) 97 } 98 99 if r.Timeout != 0 { 100 params["timeout"] = formatDuration(r.Timeout) 101 } 102 103 if r.Pretty { 104 params["pretty"] = "true" 105 } 106 107 if r.Human { 108 params["human"] = "true" 109 } 110 111 if r.ErrorTrace { 112 params["error_trace"] = "true" 113 } 114 115 if len(r.FilterPath) > 0 { 116 params["filter_path"] = strings.Join(r.FilterPath, ",") 117 } 118 119 req, err := newRequest(method, path.String(), nil) 120 if err != nil { 121 return nil, err 122 } 123 124 if len(params) > 0 { 125 q := req.URL.Query() 126 for k, v := range params { 127 q.Set(k, v) 128 } 129 req.URL.RawQuery = q.Encode() 130 } 131 132 if len(r.Header) > 0 { 133 if len(req.Header) == 0 { 134 req.Header = r.Header 135 } else { 136 for k, vv := range r.Header { 137 for _, v := range vv { 138 req.Header.Add(k, v) 139 } 140 } 141 } 142 } 143 144 if ctx != nil { 145 req = req.WithContext(ctx) 146 } 147 148 res, err := transport.Perform(req) 149 if err != nil { 150 return nil, err 151 } 152 153 response := Response{ 154 StatusCode: res.StatusCode, 155 Body: res.Body, 156 Header: res.Header, 157 } 158 159 return &response, nil 160 } 161 162 // WithContext sets the request context. 163 // 164 func (f DeleteScript) WithContext(v context.Context) func(*DeleteScriptRequest) { 165 return func(r *DeleteScriptRequest) { 166 r.ctx = v 167 } 168 } 169 170 // WithMasterTimeout - explicit operation timeout for connection to cluster-manager node. 171 // 172 // Deprecated: To promote inclusive language, use WithClusterManagerTimeout instead. 173 // 174 func (f DeleteScript) WithMasterTimeout(v time.Duration) func(*DeleteScriptRequest) { 175 return func(r *DeleteScriptRequest) { 176 r.MasterTimeout = v 177 } 178 } 179 180 // WithClusterManagerTimeout - explicit operation timeout for connection to cluster-manager node. 181 // 182 func (f DeleteScript) WithClusterManagerTimeout(v time.Duration) func(*DeleteScriptRequest) { 183 return func(r *DeleteScriptRequest) { 184 r.ClusterManagerTimeout = v 185 } 186 } 187 188 // WithTimeout - explicit operation timeout. 189 // 190 func (f DeleteScript) WithTimeout(v time.Duration) func(*DeleteScriptRequest) { 191 return func(r *DeleteScriptRequest) { 192 r.Timeout = v 193 } 194 } 195 196 // WithPretty makes the response body pretty-printed. 197 // 198 func (f DeleteScript) WithPretty() func(*DeleteScriptRequest) { 199 return func(r *DeleteScriptRequest) { 200 r.Pretty = true 201 } 202 } 203 204 // WithHuman makes statistical values human-readable. 205 // 206 func (f DeleteScript) WithHuman() func(*DeleteScriptRequest) { 207 return func(r *DeleteScriptRequest) { 208 r.Human = true 209 } 210 } 211 212 // WithErrorTrace includes the stack trace for errors in the response body. 213 // 214 func (f DeleteScript) WithErrorTrace() func(*DeleteScriptRequest) { 215 return func(r *DeleteScriptRequest) { 216 r.ErrorTrace = true 217 } 218 } 219 220 // WithFilterPath filters the properties of the response body. 221 // 222 func (f DeleteScript) WithFilterPath(v ...string) func(*DeleteScriptRequest) { 223 return func(r *DeleteScriptRequest) { 224 r.FilterPath = v 225 } 226 } 227 228 // WithHeader adds the headers to the HTTP request. 229 // 230 func (f DeleteScript) WithHeader(h map[string]string) func(*DeleteScriptRequest) { 231 return func(r *DeleteScriptRequest) { 232 if r.Header == nil { 233 r.Header = make(http.Header) 234 } 235 for k, v := range h { 236 r.Header.Add(k, v) 237 } 238 } 239 } 240 241 // WithOpaqueID adds the X-Opaque-Id header to the HTTP request. 242 // 243 func (f DeleteScript) WithOpaqueID(s string) func(*DeleteScriptRequest) { 244 return func(r *DeleteScriptRequest) { 245 if r.Header == nil { 246 r.Header = make(http.Header) 247 } 248 r.Header.Set("X-Opaque-Id", s) 249 } 250 }