github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.scripts_painless_execute.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 "io" 32 "net/http" 33 "strings" 34 ) 35 36 func newScriptsPainlessExecuteFunc(t Transport) ScriptsPainlessExecute { 37 return func(o ...func(*ScriptsPainlessExecuteRequest)) (*Response, error) { 38 var r = ScriptsPainlessExecuteRequest{} 39 for _, f := range o { 40 f(&r) 41 } 42 return r.Do(r.ctx, t) 43 } 44 } 45 46 // ----- API Definition ------------------------------------------------------- 47 48 // ScriptsPainlessExecute allows an arbitrary script to be executed and a result to be returned 49 // 50 // This API is experimental. 51 // 52 // 53 type ScriptsPainlessExecute func(o ...func(*ScriptsPainlessExecuteRequest)) (*Response, error) 54 55 // ScriptsPainlessExecuteRequest configures the Scripts Painless Execute API request. 56 // 57 type ScriptsPainlessExecuteRequest struct { 58 Body io.Reader 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 ScriptsPainlessExecuteRequest) 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(len("/_scripts/painless/_execute")) 82 path.WriteString("/_scripts/painless/_execute") 83 84 params = make(map[string]string) 85 86 if r.Pretty { 87 params["pretty"] = "true" 88 } 89 90 if r.Human { 91 params["human"] = "true" 92 } 93 94 if r.ErrorTrace { 95 params["error_trace"] = "true" 96 } 97 98 if len(r.FilterPath) > 0 { 99 params["filter_path"] = strings.Join(r.FilterPath, ",") 100 } 101 102 req, err := newRequest(method, path.String(), r.Body) 103 if err != nil { 104 return nil, err 105 } 106 107 if len(params) > 0 { 108 q := req.URL.Query() 109 for k, v := range params { 110 q.Set(k, v) 111 } 112 req.URL.RawQuery = q.Encode() 113 } 114 115 if r.Body != nil { 116 req.Header[headerContentType] = headerContentTypeJSON 117 } 118 119 if len(r.Header) > 0 { 120 if len(req.Header) == 0 { 121 req.Header = r.Header 122 } else { 123 for k, vv := range r.Header { 124 for _, v := range vv { 125 req.Header.Add(k, v) 126 } 127 } 128 } 129 } 130 131 if ctx != nil { 132 req = req.WithContext(ctx) 133 } 134 135 res, err := transport.Perform(req) 136 if err != nil { 137 return nil, err 138 } 139 140 response := Response{ 141 StatusCode: res.StatusCode, 142 Body: res.Body, 143 Header: res.Header, 144 } 145 146 return &response, nil 147 } 148 149 // WithContext sets the request context. 150 // 151 func (f ScriptsPainlessExecute) WithContext(v context.Context) func(*ScriptsPainlessExecuteRequest) { 152 return func(r *ScriptsPainlessExecuteRequest) { 153 r.ctx = v 154 } 155 } 156 157 // WithBody - The script to execute. 158 // 159 func (f ScriptsPainlessExecute) WithBody(v io.Reader) func(*ScriptsPainlessExecuteRequest) { 160 return func(r *ScriptsPainlessExecuteRequest) { 161 r.Body = v 162 } 163 } 164 165 // WithPretty makes the response body pretty-printed. 166 // 167 func (f ScriptsPainlessExecute) WithPretty() func(*ScriptsPainlessExecuteRequest) { 168 return func(r *ScriptsPainlessExecuteRequest) { 169 r.Pretty = true 170 } 171 } 172 173 // WithHuman makes statistical values human-readable. 174 // 175 func (f ScriptsPainlessExecute) WithHuman() func(*ScriptsPainlessExecuteRequest) { 176 return func(r *ScriptsPainlessExecuteRequest) { 177 r.Human = true 178 } 179 } 180 181 // WithErrorTrace includes the stack trace for errors in the response body. 182 // 183 func (f ScriptsPainlessExecute) WithErrorTrace() func(*ScriptsPainlessExecuteRequest) { 184 return func(r *ScriptsPainlessExecuteRequest) { 185 r.ErrorTrace = true 186 } 187 } 188 189 // WithFilterPath filters the properties of the response body. 190 // 191 func (f ScriptsPainlessExecute) WithFilterPath(v ...string) func(*ScriptsPainlessExecuteRequest) { 192 return func(r *ScriptsPainlessExecuteRequest) { 193 r.FilterPath = v 194 } 195 } 196 197 // WithHeader adds the headers to the HTTP request. 198 // 199 func (f ScriptsPainlessExecute) WithHeader(h map[string]string) func(*ScriptsPainlessExecuteRequest) { 200 return func(r *ScriptsPainlessExecuteRequest) { 201 if r.Header == nil { 202 r.Header = make(http.Header) 203 } 204 for k, v := range h { 205 r.Header.Add(k, v) 206 } 207 } 208 } 209 210 // WithOpaqueID adds the X-Opaque-Id header to the HTTP request. 211 // 212 func (f ScriptsPainlessExecute) WithOpaqueID(s string) func(*ScriptsPainlessExecuteRequest) { 213 return func(r *ScriptsPainlessExecuteRequest) { 214 if r.Header == nil { 215 r.Header = make(http.Header) 216 } 217 r.Header.Set("X-Opaque-Id", s) 218 } 219 }