Debian Codegraph @

pkg/api/options.go

raw
1 // Copyright 2025 The Archivista Contributors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 package api
15
16 import "net/http"
17
18 type RequestOption func(*requestOptions)
19
20 type requestOptions struct {
21 additionalHeaders http.Header
22 }
23
24 func WithHeaders(h http.Header) RequestOption {
25 return func(ro *requestOptions) {
26 if h != nil {
27 ro.additionalHeaders = h.Clone()
28 }
29 }
30 }
31
32 func applyRequestOptions(req *http.Request, requestOpts ...RequestOption) *http.Request {
33 if req == nil {
34 return nil
35 }
36
37 opts := &requestOptions{}
38 for _, opt := range requestOpts {
39 if opt == nil {
40 continue
41 }
42
43 opt(opts)
44 }
45
46 if opts.additionalHeaders != nil {
47 req.Header = opts.additionalHeaders
48 }
49
50 return req
51 }