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