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