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