github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.pointintime.delete.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  	"bytes"
    31  	"context"
    32  	"encoding/json"
    33  	"io"
    34  	"net/http"
    35  	"strings"
    36  )
    37  
    38  func newPointInTimeDeleteFunc(t Transport) PointInTimeDelete {
    39  	return func(o ...func(*PointInTimeDeleteRequest)) (*Response, *PointInTimeDeleteResp, error) {
    40  		var r = PointInTimeDeleteRequest{}
    41  		for _, f := range o {
    42  			f(&r)
    43  		}
    44  		return r.Do(r.ctx, t)
    45  	}
    46  }
    47  
    48  // ----- API Definition -------------------------------------------------------
    49  
    50  // PointInTimeDelete lets you delete pits used for searching with pagination
    51  type PointInTimeDelete func(o ...func(*PointInTimeDeleteRequest)) (*Response, *PointInTimeDeleteResp, error)
    52  
    53  // PointInTimeDeleteRequest configures the Point In Time Delete API request.
    54  type PointInTimeDeleteRequest struct {
    55  	PitID []string
    56  
    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  // PointInTimeDeleteRequestBody is used to from the delete request body
    68  type PointInTimeDeleteRequestBody struct {
    69  	PitID []string `json:"pit_id"`
    70  }
    71  
    72  // PointInTimeDeleteResp is a custom type to parse the Point In Time Delete Reponse
    73  type PointInTimeDeleteResp struct {
    74  	Pits []struct {
    75  		PitID      string `json:"pit_id"`
    76  		Successful bool   `json:"successful"`
    77  	} `json:"pits"`
    78  }
    79  
    80  // Do executes the request and returns response or error.
    81  func (r PointInTimeDeleteRequest) Do(ctx context.Context, transport Transport) (*Response, *PointInTimeDeleteResp, error) {
    82  	var (
    83  		path   strings.Builder
    84  		params map[string]string
    85  		body   io.Reader
    86  
    87  		data PointInTimeDeleteResp
    88  	)
    89  	method := "DELETE"
    90  
    91  	path.Grow(len("/_search/point_in_time"))
    92  	path.WriteString("/_search/point_in_time")
    93  
    94  	params = make(map[string]string)
    95  
    96  	if len(r.PitID) > 0 {
    97  		bodyStruct := PointInTimeDeleteRequestBody{PitID: r.PitID}
    98  		bodyJSON, err := json.Marshal(bodyStruct)
    99  		if err != nil {
   100  			return nil, nil, err
   101  		}
   102  		body = bytes.NewBuffer(bodyJSON)
   103  	}
   104  
   105  	if r.Pretty {
   106  		params["pretty"] = "true"
   107  	}
   108  
   109  	if r.Human {
   110  		params["human"] = "true"
   111  	}
   112  
   113  	if r.ErrorTrace {
   114  		params["error_trace"] = "true"
   115  	}
   116  
   117  	if len(r.FilterPath) > 0 {
   118  		params["filter_path"] = strings.Join(r.FilterPath, ",")
   119  	}
   120  
   121  	req, err := newRequest(method, path.String(), body)
   122  	if err != nil {
   123  		return nil, nil, err
   124  	}
   125  
   126  	if len(params) > 0 {
   127  		q := req.URL.Query()
   128  		for k, v := range params {
   129  			q.Set(k, v)
   130  		}
   131  		req.URL.RawQuery = q.Encode()
   132  	}
   133  
   134  	if body != nil {
   135  		req.Header[headerContentType] = headerContentTypeJSON
   136  	}
   137  
   138  	if len(r.Header) > 0 {
   139  		if len(req.Header) == 0 {
   140  			req.Header = r.Header
   141  		} else {
   142  			for k, vv := range r.Header {
   143  				for _, v := range vv {
   144  					req.Header.Add(k, v)
   145  				}
   146  			}
   147  		}
   148  	}
   149  
   150  	if ctx != nil {
   151  		req = req.WithContext(ctx)
   152  	}
   153  
   154  	res, err := transport.Perform(req)
   155  	if err != nil {
   156  		return nil, nil, err
   157  	}
   158  
   159  	response := Response{
   160  		StatusCode: res.StatusCode,
   161  		Body:       res.Body,
   162  		Header:     res.Header,
   163  	}
   164  
   165  	if len(r.FilterPath) != 0 {
   166  		return &response, nil, nil
   167  	}
   168  
   169  	if err := json.NewDecoder(response.Body).Decode(&data); err != nil {
   170  		return &response, nil, err
   171  	}
   172  	return &response, &data, nil
   173  }
   174  
   175  // WithPitID sets the Pit to delete.
   176  func (f PointInTimeDelete) WithPitID(v ...string) func(*PointInTimeDeleteRequest) {
   177  	return func(r *PointInTimeDeleteRequest) {
   178  		r.PitID = v
   179  	}
   180  }
   181  
   182  // WithContext sets the request context.
   183  func (f PointInTimeDelete) WithContext(v context.Context) func(*PointInTimeDeleteRequest) {
   184  	return func(r *PointInTimeDeleteRequest) {
   185  		r.ctx = v
   186  	}
   187  }
   188  
   189  // WithPretty makes the response body pretty-printed.
   190  func (f PointInTimeDelete) WithPretty() func(*PointInTimeDeleteRequest) {
   191  	return func(r *PointInTimeDeleteRequest) {
   192  		r.Pretty = true
   193  	}
   194  }
   195  
   196  // WithHuman makes statistical values human-readable.
   197  func (f PointInTimeDelete) WithHuman() func(*PointInTimeDeleteRequest) {
   198  	return func(r *PointInTimeDeleteRequest) {
   199  		r.Human = true
   200  	}
   201  }
   202  
   203  // WithErrorTrace includes the stack trace for errors in the response body.
   204  func (f PointInTimeDelete) WithErrorTrace() func(*PointInTimeDeleteRequest) {
   205  	return func(r *PointInTimeDeleteRequest) {
   206  		r.ErrorTrace = true
   207  	}
   208  }
   209  
   210  // WithFilterPath filters the properties of the response body.
   211  func (f PointInTimeDelete) WithFilterPath(v ...string) func(*PointInTimeDeleteRequest) {
   212  	return func(r *PointInTimeDeleteRequest) {
   213  		r.FilterPath = v
   214  	}
   215  }
   216  
   217  // WithHeader adds the headers to the HTTP request.
   218  func (f PointInTimeDelete) WithHeader(h map[string]string) func(*PointInTimeDeleteRequest) {
   219  	return func(r *PointInTimeDeleteRequest) {
   220  		if r.Header == nil {
   221  			r.Header = make(http.Header)
   222  		}
   223  		for k, v := range h {
   224  			r.Header.Add(k, v)
   225  		}
   226  	}
   227  }
   228  
   229  // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
   230  func (f PointInTimeDelete) WithOpaqueID(s string) func(*PointInTimeDeleteRequest) {
   231  	return func(r *PointInTimeDeleteRequest) {
   232  		if r.Header == nil {
   233  			r.Header = make(http.Header)
   234  		}
   235  		r.Header.Set("X-Opaque-Id", s)
   236  	}
   237  }