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