github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.get_script_languages.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  )
    34  
    35  func newGetScriptLanguagesFunc(t Transport) GetScriptLanguages {
    36  	return func(o ...func(*GetScriptLanguagesRequest)) (*Response, error) {
    37  		var r = GetScriptLanguagesRequest{}
    38  		for _, f := range o {
    39  			f(&r)
    40  		}
    41  		return r.Do(r.ctx, t)
    42  	}
    43  }
    44  
    45  // ----- API Definition -------------------------------------------------------
    46  
    47  // GetScriptLanguages returns available script types, languages and contexts
    48  //
    49  // This API is experimental.
    50  //
    51  //
    52  type GetScriptLanguages func(o ...func(*GetScriptLanguagesRequest)) (*Response, error)
    53  
    54  // GetScriptLanguagesRequest configures the Get Script Languages API request.
    55  //
    56  type GetScriptLanguagesRequest struct {
    57  	Pretty     bool
    58  	Human      bool
    59  	ErrorTrace bool
    60  	FilterPath []string
    61  
    62  	Header http.Header
    63  
    64  	ctx context.Context
    65  }
    66  
    67  // Do executes the request and returns response or error.
    68  //
    69  func (r GetScriptLanguagesRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
    70  	var (
    71  		method string
    72  		path   strings.Builder
    73  		params map[string]string
    74  	)
    75  
    76  	method = "GET"
    77  
    78  	path.Grow(len("/_script_language"))
    79  	path.WriteString("/_script_language")
    80  
    81  	params = make(map[string]string)
    82  
    83  	if r.Pretty {
    84  		params["pretty"] = "true"
    85  	}
    86  
    87  	if r.Human {
    88  		params["human"] = "true"
    89  	}
    90  
    91  	if r.ErrorTrace {
    92  		params["error_trace"] = "true"
    93  	}
    94  
    95  	if len(r.FilterPath) > 0 {
    96  		params["filter_path"] = strings.Join(r.FilterPath, ",")
    97  	}
    98  
    99  	req, err := newRequest(method, path.String(), nil)
   100  	if err != nil {
   101  		return nil, err
   102  	}
   103  
   104  	if len(params) > 0 {
   105  		q := req.URL.Query()
   106  		for k, v := range params {
   107  			q.Set(k, v)
   108  		}
   109  		req.URL.RawQuery = q.Encode()
   110  	}
   111  
   112  	if len(r.Header) > 0 {
   113  		if len(req.Header) == 0 {
   114  			req.Header = r.Header
   115  		} else {
   116  			for k, vv := range r.Header {
   117  				for _, v := range vv {
   118  					req.Header.Add(k, v)
   119  				}
   120  			}
   121  		}
   122  	}
   123  
   124  	if ctx != nil {
   125  		req = req.WithContext(ctx)
   126  	}
   127  
   128  	res, err := transport.Perform(req)
   129  	if err != nil {
   130  		return nil, err
   131  	}
   132  
   133  	response := Response{
   134  		StatusCode: res.StatusCode,
   135  		Body:       res.Body,
   136  		Header:     res.Header,
   137  	}
   138  
   139  	return &response, nil
   140  }
   141  
   142  // WithContext sets the request context.
   143  //
   144  func (f GetScriptLanguages) WithContext(v context.Context) func(*GetScriptLanguagesRequest) {
   145  	return func(r *GetScriptLanguagesRequest) {
   146  		r.ctx = v
   147  	}
   148  }
   149  
   150  // WithPretty makes the response body pretty-printed.
   151  //
   152  func (f GetScriptLanguages) WithPretty() func(*GetScriptLanguagesRequest) {
   153  	return func(r *GetScriptLanguagesRequest) {
   154  		r.Pretty = true
   155  	}
   156  }
   157  
   158  // WithHuman makes statistical values human-readable.
   159  //
   160  func (f GetScriptLanguages) WithHuman() func(*GetScriptLanguagesRequest) {
   161  	return func(r *GetScriptLanguagesRequest) {
   162  		r.Human = true
   163  	}
   164  }
   165  
   166  // WithErrorTrace includes the stack trace for errors in the response body.
   167  //
   168  func (f GetScriptLanguages) WithErrorTrace() func(*GetScriptLanguagesRequest) {
   169  	return func(r *GetScriptLanguagesRequest) {
   170  		r.ErrorTrace = true
   171  	}
   172  }
   173  
   174  // WithFilterPath filters the properties of the response body.
   175  //
   176  func (f GetScriptLanguages) WithFilterPath(v ...string) func(*GetScriptLanguagesRequest) {
   177  	return func(r *GetScriptLanguagesRequest) {
   178  		r.FilterPath = v
   179  	}
   180  }
   181  
   182  // WithHeader adds the headers to the HTTP request.
   183  //
   184  func (f GetScriptLanguages) WithHeader(h map[string]string) func(*GetScriptLanguagesRequest) {
   185  	return func(r *GetScriptLanguagesRequest) {
   186  		if r.Header == nil {
   187  			r.Header = make(http.Header)
   188  		}
   189  		for k, v := range h {
   190  			r.Header.Add(k, v)
   191  		}
   192  	}
   193  }
   194  
   195  // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
   196  //
   197  func (f GetScriptLanguages) WithOpaqueID(s string) func(*GetScriptLanguagesRequest) {
   198  	return func(r *GetScriptLanguagesRequest) {
   199  		if r.Header == nil {
   200  			r.Header = make(http.Header)
   201  		}
   202  		r.Header.Set("X-Opaque-Id", s)
   203  	}
   204  }