-
Notifications
You must be signed in to change notification settings - Fork 24
/
http.go
44 lines (37 loc) · 900 Bytes
/
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
package client
import (
"fmt"
"io"
"net/http"
"strings"
)
// Post performs a post request.
func (c *PreflightClient) Post(path string, body io.Reader) (*http.Response, error) {
var bearer string
if c.usingOAuth2() {
token, err := c.getValidAccessToken()
if err != nil {
return nil, err
}
bearer = token.bearer
}
req, err := http.NewRequest(http.MethodPost, c.fullURL(path), body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
if len(bearer) > 0 {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", bearer))
}
return http.DefaultClient.Do(req)
}
func (c *PreflightClient) fullURL(path string) string {
base := c.baseURL
for strings.HasSuffix(base, "/") {
base = strings.TrimSuffix(base, "/")
}
for strings.HasPrefix(path, "/") {
path = strings.TrimPrefix(path, "/")
}
return fmt.Sprintf("%s/%s", base, path)
}