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