/
shuttle_http_client.go
56 lines (45 loc) · 1.16 KB
/
shuttle_http_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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"encoding/json"
"io"
"net/http"
"github.com/application-research/estuary/util"
)
type ShuttleHttpClient struct {
estuaryHost string
dev bool
}
func NewShuttleHttpClient(estuaryHost string, dev bool) *ShuttleHttpClient {
shc := ShuttleHttpClient{
estuaryHost: estuaryHost,
dev: dev,
}
return &shc
}
// Makes an HTTP to the main Estuary API with given parameters
func (shc *ShuttleHttpClient) MakeRequest(method string, url string, body io.Reader, authToken string) (*http.Response, func() error, error) {
scheme := "https"
if shc.dev {
scheme = "http"
}
req, err := http.NewRequest(method, scheme+"://"+shc.estuaryHost+url, body)
if err != nil {
return nil, nil, err
}
req.Header.Set("Content-Type", "application/json")
if authToken != "" {
req.Header.Set("Authorization", "Bearer "+authToken)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, nil, err
}
if resp.StatusCode != http.StatusOK {
var out util.HttpErrorResponse
if err := json.NewDecoder(resp.Body).Decode(&out); err != nil {
return nil, nil, err
}
return nil, nil, &out.Error
}
return resp, resp.Body.Close, nil
}