-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
48 lines (38 loc) · 1.22 KB
/
http.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
package greenhouseio
import (
"encoding/base64"
"fmt"
"net/http"
"regexp"
)
const (
baseURL = "https://harvest.greenhouse.io/v1"
)
var (
nextPageLinkRegex = regexp.MustCompile(`<(.*)>; rel="next"`)
)
type httpClient interface {
Do(req *http.Request) (*http.Response, error)
}
// generateHTTPRequest creates a request object with the auth header and client context attached.
func (c *Client) generateHTTPRequest(method, url string) (*http.Request, error) {
req, err := http.NewRequestWithContext(c.ctx, method, url, http.NoBody)
if err != nil {
return nil, fmt.Errorf("creating request: %w", err)
}
req.Header.Add("Authorization", c.authHeaderValue)
return req, nil
}
// generateAuthHeaderValue encodes the API token and formats it according to RFC 7617.
func generateAuthHeaderValue(apiToken string) string {
return "Basic " + base64.StdEncoding.EncodeToString([]byte(apiToken+":"))
}
// parseNextPageLink parses the `link` header in the given response for the `next` link.
func parseNextPageLink(res *http.Response) string {
matches := nextPageLinkRegex.FindStringSubmatch((res.Header.Get("link")))
if len(matches) == 0 {
return ""
}
// First element is the match, second element is the group.
return matches[1]
}