-
Notifications
You must be signed in to change notification settings - Fork 40
/
options.go
111 lines (97 loc) · 2.16 KB
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package client
import (
"net/http"
"net/url"
"github.com/hashicorp/go-azure-sdk/sdk/odata"
)
type Options interface {
ToHeaders() *Headers
ToOData() *odata.Query
ToQuery() *QueryParams
}
// Headers is a representation of the HTTP headers to be sent with a Request
type Headers struct {
vals map[string]string
}
// AppendHeader appends the http.Header values
func (h *Headers) AppendHeader(h2 http.Header) {
if h.vals == nil {
h.vals = map[string]string{}
}
for k, v := range h2 {
if len(v) > 0 {
h.vals[k] = v[0]
}
}
}
// Append sets a single header value
func (h *Headers) Append(key, value string) {
if h.vals == nil {
h.vals = map[string]string{}
}
h.vals[key] = value
}
// Merge copies the header values from h2, overwriting as necessary
func (h *Headers) Merge(h2 Headers) {
if h.vals == nil {
h.vals = map[string]string{}
}
if h2.vals == nil {
return
}
for k, v := range h2.vals {
h.vals[k] = v
}
}
// Headers returns an http.Headers map containing header values
func (h *Headers) Headers() http.Header {
out := make(http.Header)
for k, v := range h.vals {
out.Add(k, v)
}
return out
}
// QueryParams is a representation of the URL query parameters to be sent with a Request
type QueryParams struct {
vals map[string]string
}
// AppendValues appends the url.Values values
func (q *QueryParams) AppendValues(q2 url.Values) {
if q.vals == nil {
q.vals = map[string]string{}
}
for k, v := range q2 {
if len(v) > 0 {
q.vals[k] = v[0]
}
}
}
// Append sets a single query parameter value
func (q *QueryParams) Append(key, value string) {
if q.vals == nil {
q.vals = map[string]string{}
}
q.vals[key] = value
}
// Merge copies the query parameter values from q2, overwriting as necessary
func (q *QueryParams) Merge(q2 Headers) {
if q.vals == nil {
q.vals = map[string]string{}
}
if q2.vals == nil {
return
}
for k, v := range q2.vals {
q.vals[k] = v
}
}
// Values returns a url.Values map containing query parameter values
func (q *QueryParams) Values() url.Values {
va := make(url.Values)
for k, v := range q.vals {
va.Set(k, v)
}
return va
}