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