github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.indices.get_datastream.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  package opensearchapi
    11  
    12  import (
    13  	"context"
    14  	"net/http"
    15  	"strings"
    16  	"time"
    17  )
    18  
    19  func newIndicesGetDataStreamFunc(t Transport) IndicesGetDataStream {
    20  	return func(o ...func(*IndicesGetDataStreamRequest)) (*Response, error) {
    21  		var r = IndicesGetDataStreamRequest{}
    22  		for _, f := range o {
    23  			f(&r)
    24  		}
    25  		return r.Do(r.ctx, t)
    26  	}
    27  }
    28  
    29  // ----- API Definition -------------------------------------------------------
    30  
    31  // IndicesGetDataStream returns a data stream specific information if Name parameter is passed. Otherwise, returns all data streams.
    32  type IndicesGetDataStream func(o ...func(*IndicesGetDataStreamRequest)) (*Response, error)
    33  
    34  // IndicesGetDataStreamRequest configures the Indices Get Data Stream API request.
    35  type IndicesGetDataStreamRequest struct {
    36  	Name string
    37  
    38  	ClusterManagerTimeout time.Duration
    39  
    40  	Pretty     bool
    41  	Human      bool
    42  	ErrorTrace bool
    43  	FilterPath []string
    44  
    45  	Header http.Header
    46  
    47  	ctx context.Context
    48  }
    49  
    50  // Do execute the request and returns response or error.
    51  func (r IndicesGetDataStreamRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
    52  	var (
    53  		method string
    54  		path   strings.Builder
    55  		params map[string]string
    56  	)
    57  
    58  	method = "GET"
    59  
    60  	path.Grow(1 + len("_data_stream") + 1 + len(r.Name))
    61  	path.WriteString("/_data_stream/")
    62  	path.WriteString(r.Name)
    63  
    64  	params = make(map[string]string)
    65  
    66  	if r.ClusterManagerTimeout != 0 {
    67  		params["cluster_manager_timeout"] = formatDuration(r.ClusterManagerTimeout)
    68  	}
    69  
    70  	if r.Pretty {
    71  		params["pretty"] = "true"
    72  	}
    73  
    74  	if r.Human {
    75  		params["human"] = "true"
    76  	}
    77  
    78  	if r.ErrorTrace {
    79  		params["error_trace"] = "true"
    80  	}
    81  
    82  	if len(r.FilterPath) > 0 {
    83  		params["filter_path"] = strings.Join(r.FilterPath, ",")
    84  	}
    85  
    86  	req, err := newRequest(method, path.String(), nil)
    87  	if err != nil {
    88  		return nil, err
    89  	}
    90  
    91  	if len(params) > 0 {
    92  		q := req.URL.Query()
    93  		for k, v := range params {
    94  			q.Set(k, v)
    95  		}
    96  		req.URL.RawQuery = q.Encode()
    97  	}
    98  
    99  	if len(r.Header) > 0 {
   100  		if len(req.Header) == 0 {
   101  			req.Header = r.Header
   102  		} else {
   103  			for k, vv := range r.Header {
   104  				for _, v := range vv {
   105  					req.Header.Add(k, v)
   106  				}
   107  			}
   108  		}
   109  	}
   110  
   111  	if ctx != nil {
   112  		req = req.WithContext(ctx)
   113  	}
   114  
   115  	res, err := transport.Perform(req)
   116  	if err != nil {
   117  		return nil, err
   118  	}
   119  
   120  	response := Response{
   121  		StatusCode: res.StatusCode,
   122  		Body:       res.Body,
   123  		Header:     res.Header,
   124  	}
   125  
   126  	return &response, nil
   127  }
   128  
   129  // WithContext sets the request context.
   130  func (f IndicesGetDataStream) WithContext(v context.Context) func(*IndicesGetDataStreamRequest) {
   131  	return func(r *IndicesGetDataStreamRequest) {
   132  		r.ctx = v
   133  	}
   134  }
   135  
   136  // WithName - the comma separated names of the index templates.
   137  func (f IndicesGetDataStream) WithName(v string) func(*IndicesGetDataStreamRequest) {
   138  	return func(r *IndicesGetDataStreamRequest) {
   139  		r.Name = v
   140  	}
   141  }
   142  
   143  // WithClusterManagerTimeout - explicit operation timeout for connection to cluster-manager node.
   144  func (f IndicesGetDataStream) WithClusterManagerTimeout(v time.Duration) func(*IndicesGetDataStreamRequest) {
   145  	return func(r *IndicesGetDataStreamRequest) {
   146  		r.ClusterManagerTimeout = v
   147  	}
   148  }
   149  
   150  // WithPretty makes the response body pretty-printed.
   151  func (f IndicesGetDataStream) WithPretty() func(*IndicesGetDataStreamRequest) {
   152  	return func(r *IndicesGetDataStreamRequest) {
   153  		r.Pretty = true
   154  	}
   155  }
   156  
   157  // WithHuman makes statistical values human-readable.
   158  func (f IndicesGetDataStream) WithHuman() func(*IndicesGetDataStreamRequest) {
   159  	return func(r *IndicesGetDataStreamRequest) {
   160  		r.Human = true
   161  	}
   162  }
   163  
   164  // WithErrorTrace includes the stack trace for errors in the response body.
   165  func (f IndicesGetDataStream) WithErrorTrace() func(*IndicesGetDataStreamRequest) {
   166  	return func(r *IndicesGetDataStreamRequest) {
   167  		r.ErrorTrace = true
   168  	}
   169  }
   170  
   171  // WithFilterPath filters the properties of the response body.
   172  func (f IndicesGetDataStream) WithFilterPath(v ...string) func(*IndicesGetDataStreamRequest) {
   173  	return func(r *IndicesGetDataStreamRequest) {
   174  		r.FilterPath = v
   175  	}
   176  }
   177  
   178  // WithHeader adds the headers to the HTTP request.
   179  func (f IndicesGetDataStream) WithHeader(h map[string]string) func(*IndicesGetDataStreamRequest) {
   180  	return func(r *IndicesGetDataStreamRequest) {
   181  		if r.Header == nil {
   182  			r.Header = make(http.Header)
   183  		}
   184  		for k, v := range h {
   185  			r.Header.Add(k, v)
   186  		}
   187  	}
   188  }
   189  
   190  // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
   191  func (f IndicesGetDataStream) WithOpaqueID(s string) func(*IndicesGetDataStreamRequest) {
   192  	return func(r *IndicesGetDataStreamRequest) {
   193  		if r.Header == nil {
   194  			r.Header = make(http.Header)
   195  		}
   196  		r.Header.Set("X-Opaque-Id", s)
   197  	}
   198  }