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