-
Notifications
You must be signed in to change notification settings - Fork 7
/
client.go
108 lines (99 loc) · 2.71 KB
/
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
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
package http
import (
"bytes"
"encoding/json"
stderrors "errors"
"fmt"
"io"
"net"
"net/http"
"net/url"
"time"
"github.com/google/uuid"
"github.com/pkg/errors"
)
type Client struct {
BaseURL string
Timeout time.Duration
Metadata Metadata
}
type Metadata struct {
AuthToken string
CLIVersion string
CallerEmail string
}
// URL returns a URL with provided path added to the client's base URL.
func (c *Client) URL(path string) (string, error) {
requestURL, err := url.Parse(fmt.Sprintf("%s/%s", c.BaseURL, path))
if err != nil {
return "", err
}
return requestURL.String(), nil
}
// URLWithQuery returns a URL with provided path and query params added to the
// client's base URL. All query param values are escaped.
func (c *Client) URLWithQuery(path string, queryParams url.Values) (string, error) {
if queryParams != nil {
path += fmt.Sprintf("?%s", queryParams.Encode())
}
return c.URL(path)
}
// Do sends an HTTP request defined by the provided method and path. The base
// URL is prefixed on the provided path.
//
// Request and response bodies are marshalled and unmarshalled as JSON and if
// the server returns a status code above 399 the response is parsed as an
// ErrorResponse object and returned as the error.
func (c *Client) Do(method string, path string, requestBody, responseBody interface{}) error {
client := &http.Client{
Timeout: c.Timeout,
}
var b io.ReadWriter
if requestBody != nil {
b = &bytes.Buffer{}
err := json.NewEncoder(b).Encode(requestBody)
if err != nil {
return err
}
}
req, err := http.NewRequest(method, path, b)
if err != nil {
return err
}
id, err := uuid.NewRandom()
if err == nil {
req.Header.Set("x-request-id", id.String())
}
req.Header.Set("Authorization", "Bearer "+c.Metadata.AuthToken)
req.Header.Set("X-Cli-Version", c.Metadata.CLIVersion)
req.Header.Set("X-Caller-Email", c.Metadata.CallerEmail)
resp, err := client.Do(req)
if err != nil {
var dnsError *net.DNSError
var urlError *url.Error
if stderrors.As(err, &dnsError) || (stderrors.As(err, &urlError) && stderrors.Is(err, io.EOF)) {
return &ErrorResponse{
Message: "could not connect to the release-manager server. Are you connected to the internet and, if required, a VPN?",
}
}
return &ErrorResponse{
Message: err.Error(),
ID: id.String(),
}
}
decoder := json.NewDecoder(resp.Body)
if resp.StatusCode >= http.StatusBadRequest {
var responseError ErrorResponse
err = decoder.Decode(&responseError)
if err != nil {
return errors.WithMessagef(err, "response status %s: unmarshal error response", resp.Status)
}
responseError.ID = id.String()
return &responseError
}
err = decoder.Decode(responseBody)
if err != nil {
return err
}
return nil
}