github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.indices.create_datastream.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 package opensearchapi 11 12 import ( 13 "context" 14 "net/http" 15 "strings" 16 ) 17 18 func newIndicesCreateDataStreamFunc(t Transport) IndicesCreateDataStream { 19 return func(name string, o ...func(*IndicesCreateDataStreamRequest)) (*Response, error) { 20 var r = IndicesCreateDataStreamRequest{Name: name} 21 for _, f := range o { 22 f(&r) 23 } 24 return r.Do(r.ctx, t) 25 } 26 } 27 28 // ----- API Definition ------------------------------------------------------- 29 30 // IndicesCreateDataStream creates a data stream. 31 type IndicesCreateDataStream func(index string, o ...func(*IndicesCreateDataStreamRequest)) (*Response, error) 32 33 // IndicesCreateDataStreamRequest configures the Indices Create Data Stream API request. 34 type IndicesCreateDataStreamRequest struct { 35 Name string 36 37 Pretty bool 38 Human bool 39 ErrorTrace bool 40 FilterPath []string 41 42 Header http.Header 43 44 ctx context.Context 45 } 46 47 // Do execute the request and returns response or error. 48 func (r IndicesCreateDataStreamRequest) Do(ctx context.Context, transport Transport) (*Response, error) { 49 var ( 50 method string 51 path strings.Builder 52 params map[string]string 53 ) 54 55 method = "PUT" 56 57 path.Grow(1 + len("_data_stream") + 1 + len(r.Name)) 58 path.WriteString("/_data_stream/") 59 path.WriteString(r.Name) 60 61 params = make(map[string]string) 62 63 if r.Pretty { 64 params["pretty"] = "true" 65 } 66 67 if r.Human { 68 params["human"] = "true" 69 } 70 71 if r.ErrorTrace { 72 params["error_trace"] = "true" 73 } 74 75 if len(r.FilterPath) > 0 { 76 params["filter_path"] = strings.Join(r.FilterPath, ",") 77 } 78 79 req, err := newRequest(method, path.String(), nil) 80 if err != nil { 81 return nil, err 82 } 83 84 if len(params) > 0 { 85 q := req.URL.Query() 86 for k, v := range params { 87 q.Set(k, v) 88 } 89 req.URL.RawQuery = q.Encode() 90 } 91 92 if len(r.Header) > 0 { 93 if len(req.Header) == 0 { 94 req.Header = r.Header 95 } else { 96 for k, vv := range r.Header { 97 for _, v := range vv { 98 req.Header.Add(k, v) 99 } 100 } 101 } 102 } 103 104 if ctx != nil { 105 req = req.WithContext(ctx) 106 } 107 108 res, err := transport.Perform(req) 109 if err != nil { 110 return nil, err 111 } 112 113 response := Response{ 114 StatusCode: res.StatusCode, 115 Body: res.Body, 116 Header: res.Header, 117 } 118 119 return &response, nil 120 } 121 122 // WithContext sets the request context. 123 func (f IndicesCreateDataStream) WithContext(v context.Context) func(*IndicesCreateDataStreamRequest) { 124 return func(r *IndicesCreateDataStreamRequest) { 125 r.ctx = v 126 } 127 } 128 129 // WithPretty makes the response body pretty-printed. 130 func (f IndicesCreateDataStream) WithPretty() func(*IndicesCreateDataStreamRequest) { 131 return func(r *IndicesCreateDataStreamRequest) { 132 r.Pretty = true 133 } 134 } 135 136 // WithHuman makes statistical values human-readable. 137 func (f IndicesCreateDataStream) WithHuman() func(*IndicesCreateDataStreamRequest) { 138 return func(r *IndicesCreateDataStreamRequest) { 139 r.Human = true 140 } 141 } 142 143 // WithErrorTrace includes the stack trace for errors in the response body. 144 func (f IndicesCreateDataStream) WithErrorTrace() func(*IndicesCreateDataStreamRequest) { 145 return func(r *IndicesCreateDataStreamRequest) { 146 r.ErrorTrace = true 147 } 148 } 149 150 // WithFilterPath filters the properties of the response body. 151 func (f IndicesCreateDataStream) WithFilterPath(v ...string) func(*IndicesCreateDataStreamRequest) { 152 return func(r *IndicesCreateDataStreamRequest) { 153 r.FilterPath = v 154 } 155 } 156 157 // WithHeader adds the headers to the HTTP request. 158 func (f IndicesCreateDataStream) WithHeader(h map[string]string) func(*IndicesCreateDataStreamRequest) { 159 return func(r *IndicesCreateDataStreamRequest) { 160 if r.Header == nil { 161 r.Header = make(http.Header) 162 } 163 for k, v := range h { 164 r.Header.Add(k, v) 165 } 166 } 167 } 168 169 // WithOpaqueID adds the X-Opaque-Id header to the HTTP request. 170 func (f IndicesCreateDataStream) WithOpaqueID(s string) func(*IndicesCreateDataStreamRequest) { 171 return func(r *IndicesCreateDataStreamRequest) { 172 if r.Header == nil { 173 r.Header = make(http.Header) 174 } 175 r.Header.Set("X-Opaque-Id", s) 176 } 177 }