-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
36 lines (28 loc) · 850 Bytes
/
client.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
package main
import "net/http"
// ClientWithHeaders represents a HTTP client with custom request headers.
type ClientWithHeaders struct {
headers map[string]string
}
// NewClientWithHeaders creates a HTTP client which will add given headers to request.
func NewClientWithHeaders(headers map[string]string) *ClientWithHeaders {
return &ClientWithHeaders{headers: headers}
}
// Get requests given url.
func (c ClientWithHeaders) Get(url string) (*http.Response, error) {
req, err := c.buildRequest(url)
if err != nil {
return nil, err
}
return http.DefaultClient.Do(req)
}
func (c ClientWithHeaders) buildRequest(url string) (*http.Request, error) {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
for header, value := range c.headers {
req.Header.Set(header, value)
}
return req, nil
}