-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequestHeaders.go
154 lines (128 loc) · 4.8 KB
/
requestHeaders.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package bingSpellCheck
import "net/http"
const (
// SubscriptionKeyHeader is the header for the required API Key
SubscriptionKeyHeader = "Ocp-Apim-Subscription-Key"
// AcceptHeader is the standard HTTP Accept header, and defaults to 'application/json'
AcceptHeader = "Accept"
// AcceptLanguageHeader is the standard HTTP Accept-Language header
//
// Notes
// Comma seperated lists of languges in decreasing order of preference.
// See RFC2616 for more info on format
//
AcceptLanguageHeader = "Accept-Language"
// AcceptApplicationJSON is the standard Accept header value for JSON
AcceptApplicationJSON = "application/json"
// UserAgentHeader is the standard HTTP User-Agent header
UserAgentHeader = "User-Agent"
// PragmaHeader is the standard HTTP Pragma header
PragmaHeader = "Pragma"
// ClientIDHeader is used to provide users with consistent
// behavior across Bing API calls
ClientIDHeader = "X-MSEdge-ClientID"
// ClientIPHeader The IPv4 or IPv6 address of the client device
//
// Notes
// Bing uses the ClientIPto discover the user's location. Bing uses the
// location information to determine safe search behavior
//
ClientIPHeader = "X-MSEdge-ClientIP"
// SearchLocationHeader is semicolon-delimited list of key/value pairs that
// describe the client's geographical location
SearchLocationHeader = "X-Search-Location"
// AcceptApplicationLinkedJSON is the Accept header value for Linked JSON
AcceptApplicationLinkedJSON = "application/ld+json"
// PragmaNoCache is the standard Pragma header value of no-cache
PragmaNoCache = "no-cache"
)
// SpellCheckHeaders are HTTP header values that may be sent with a request
// to the Bing Spell Check API
//
// Notes
// For more information see:
// https://docs.microsoft.com/en-us/rest/api/cognitiveservices/bing-spell-check-api-v7-reference#request-headers
//
type SpellCheckHeaders struct {
Headers http.Header
}
// NewSpellCheckHeaders returns a *SpellCheckHeaders using a specified
// Bing subscription key
func NewSpellCheckHeaders(subscriptionKey string) *SpellCheckHeaders {
return (&SpellCheckHeaders{Headers: http.Header{}}).WithSubscriptionKey(subscriptionKey).WithJSON()
}
// SetHeader sets the value of a header, or removes it if value is empty
func (sch *SpellCheckHeaders) SetHeader(header, value string) *SpellCheckHeaders {
if len(value) > 0 {
sch.Headers.Set(header, value)
} else {
sch.Headers.Del(header)
}
return sch
}
// WithJSON sets the accept header to 'application/json'
func (sch *SpellCheckHeaders) WithJSON() *SpellCheckHeaders {
return sch.SetHeader(AcceptHeader, AcceptApplicationJSON)
}
// WithLinkedJSON sets the accept header to 'application/ld+json'
func (sch *SpellCheckHeaders) WithLinkedJSON() *SpellCheckHeaders {
return sch.SetHeader(AcceptHeader, AcceptApplicationLinkedJSON)
}
// WithSubscriptionKey sets the SubscriptionKey header
//
// Notes
// This header values is *required*
//
func (sch *SpellCheckHeaders) WithSubscriptionKey(subscriptionKey string) *SpellCheckHeaders {
return sch.SetHeader(SubscriptionKeyHeader, subscriptionKey)
}
// WithUserAgent sets the UserAgent header (see RFC 2616 for format)
//
// Notes
// Passing in an empty string effectively removes the Header
//
func (sch *SpellCheckHeaders) WithUserAgent(userAgent string) *SpellCheckHeaders {
return sch.SetHeader(UserAgentHeader, userAgent)
}
// WithNoCachePragma sets the Pragma header to no-cache
func (sch *SpellCheckHeaders) WithNoCachePragma() *SpellCheckHeaders {
return sch.SetHeader(PragmaHeader, PragmaNoCache)
}
// WithAllowCachePragma removes the Pragma header
func (sch *SpellCheckHeaders) WithAllowCachePragma() *SpellCheckHeaders {
sch.Headers.Del(PragmaHeader)
return sch
}
// WithClientID sets the ClientID header
//
// Notes
// Passing in an empty string effectively removes the Header
//
func (sch *SpellCheckHeaders) WithClientID(clientID string) *SpellCheckHeaders {
return sch.SetHeader(ClientIDHeader, clientID)
}
// WithClientIP sets the ClientIP header
//
// Notes
// Passing in an empty string effectively removes the Header
//
func (sch *SpellCheckHeaders) WithClientIP(clientIP string) *SpellCheckHeaders {
return sch.SetHeader(ClientIPHeader, clientIP)
}
// WithAcceptLanguages sets the AcceptLanaguage header (see RFC2616 for format)
//
// Notes
// Passing in an empty string effectively removes the Header
//
func (sch *SpellCheckHeaders) WithAcceptLanguages(languages string) *SpellCheckHeaders {
return sch.SetHeader(AcceptLanguageHeader, languages)
}
// WithSearchLocation sets the SearchLocation header (see Bing Spell Check docs
// for values and expected format)
//
// Notes
// Passing in an empty string effectively removes the Header
//
func (sch *SpellCheckHeaders) WithSearchLocation(location string) *SpellCheckHeaders {
return sch.SetHeader(SearchLocationHeader, location)
}