github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.clear_scroll.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 "io" 32 "net/http" 33 "strings" 34 ) 35 36 func newClearScrollFunc(t Transport) ClearScroll { 37 return func(o ...func(*ClearScrollRequest)) (*Response, error) { 38 var r = ClearScrollRequest{} 39 for _, f := range o { 40 f(&r) 41 } 42 return r.Do(r.ctx, t) 43 } 44 } 45 46 // ----- API Definition ------------------------------------------------------- 47 48 // ClearScroll explicitly clears the search context for a scroll. 49 // 50 // 51 type ClearScroll func(o ...func(*ClearScrollRequest)) (*Response, error) 52 53 // ClearScrollRequest configures the Clear Scroll API request. 54 // 55 type ClearScrollRequest struct { 56 Body io.Reader 57 58 ScrollID []string 59 60 Pretty bool 61 Human bool 62 ErrorTrace bool 63 FilterPath []string 64 65 Header http.Header 66 67 ctx context.Context 68 } 69 70 // Do executes the request and returns response or error. 71 // 72 func (r ClearScrollRequest) Do(ctx context.Context, transport Transport) (*Response, error) { 73 var ( 74 method string 75 path strings.Builder 76 params map[string]string 77 ) 78 79 method = "DELETE" 80 81 path.Grow(1 + len("_search") + 1 + len("scroll") + 1 + len(strings.Join(r.ScrollID, ","))) 82 path.WriteString("/") 83 path.WriteString("_search") 84 path.WriteString("/") 85 path.WriteString("scroll") 86 if len(r.ScrollID) > 0 { 87 path.WriteString("/") 88 path.WriteString(strings.Join(r.ScrollID, ",")) 89 } 90 91 params = make(map[string]string) 92 93 if r.Pretty { 94 params["pretty"] = "true" 95 } 96 97 if r.Human { 98 params["human"] = "true" 99 } 100 101 if r.ErrorTrace { 102 params["error_trace"] = "true" 103 } 104 105 if len(r.FilterPath) > 0 { 106 params["filter_path"] = strings.Join(r.FilterPath, ",") 107 } 108 109 req, err := newRequest(method, path.String(), r.Body) 110 if err != nil { 111 return nil, err 112 } 113 114 if len(params) > 0 { 115 q := req.URL.Query() 116 for k, v := range params { 117 q.Set(k, v) 118 } 119 req.URL.RawQuery = q.Encode() 120 } 121 122 if r.Body != nil { 123 req.Header[headerContentType] = headerContentTypeJSON 124 } 125 126 if len(r.Header) > 0 { 127 if len(req.Header) == 0 { 128 req.Header = r.Header 129 } else { 130 for k, vv := range r.Header { 131 for _, v := range vv { 132 req.Header.Add(k, v) 133 } 134 } 135 } 136 } 137 138 if ctx != nil { 139 req = req.WithContext(ctx) 140 } 141 142 res, err := transport.Perform(req) 143 if err != nil { 144 return nil, err 145 } 146 147 response := Response{ 148 StatusCode: res.StatusCode, 149 Body: res.Body, 150 Header: res.Header, 151 } 152 153 return &response, nil 154 } 155 156 // WithContext sets the request context. 157 // 158 func (f ClearScroll) WithContext(v context.Context) func(*ClearScrollRequest) { 159 return func(r *ClearScrollRequest) { 160 r.ctx = v 161 } 162 } 163 164 // WithBody - A comma-separated list of scroll IDs to clear if none was specified via the scroll_id parameter. 165 // 166 func (f ClearScroll) WithBody(v io.Reader) func(*ClearScrollRequest) { 167 return func(r *ClearScrollRequest) { 168 r.Body = v 169 } 170 } 171 172 // WithScrollID - a list of scroll ids to clear. 173 // 174 func (f ClearScroll) WithScrollID(v ...string) func(*ClearScrollRequest) { 175 return func(r *ClearScrollRequest) { 176 r.ScrollID = v 177 } 178 } 179 180 // WithPretty makes the response body pretty-printed. 181 // 182 func (f ClearScroll) WithPretty() func(*ClearScrollRequest) { 183 return func(r *ClearScrollRequest) { 184 r.Pretty = true 185 } 186 } 187 188 // WithHuman makes statistical values human-readable. 189 // 190 func (f ClearScroll) WithHuman() func(*ClearScrollRequest) { 191 return func(r *ClearScrollRequest) { 192 r.Human = true 193 } 194 } 195 196 // WithErrorTrace includes the stack trace for errors in the response body. 197 // 198 func (f ClearScroll) WithErrorTrace() func(*ClearScrollRequest) { 199 return func(r *ClearScrollRequest) { 200 r.ErrorTrace = true 201 } 202 } 203 204 // WithFilterPath filters the properties of the response body. 205 // 206 func (f ClearScroll) WithFilterPath(v ...string) func(*ClearScrollRequest) { 207 return func(r *ClearScrollRequest) { 208 r.FilterPath = v 209 } 210 } 211 212 // WithHeader adds the headers to the HTTP request. 213 // 214 func (f ClearScroll) WithHeader(h map[string]string) func(*ClearScrollRequest) { 215 return func(r *ClearScrollRequest) { 216 if r.Header == nil { 217 r.Header = make(http.Header) 218 } 219 for k, v := range h { 220 r.Header.Add(k, v) 221 } 222 } 223 } 224 225 // WithOpaqueID adds the X-Opaque-Id header to the HTTP request. 226 // 227 func (f ClearScroll) WithOpaqueID(s string) func(*ClearScrollRequest) { 228 return func(r *ClearScrollRequest) { 229 if r.Header == nil { 230 r.Header = make(http.Header) 231 } 232 r.Header.Set("X-Opaque-Id", s) 233 } 234 }