forked from CircleCI-Public/circleci-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
145 lines (127 loc) · 3.48 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package rest
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
"time"
"github.com/CircleCI-Public/circleci-cli/api/header"
"github.com/CircleCI-Public/circleci-cli/settings"
"github.com/CircleCI-Public/circleci-cli/version"
)
type Client struct {
BaseURL *url.URL
circleToken string
client *http.Client
}
func New(baseURL *url.URL, token string, httpClient *http.Client) *Client {
return &Client{
BaseURL: baseURL,
circleToken: token,
client: httpClient,
}
}
func NewFromConfig(host string, config *settings.Config) *Client {
// Ensure endpoint ends with a slash
endpoint := config.RestEndpoint
if !strings.HasSuffix(endpoint, "/") {
endpoint += "/"
}
baseURL, _ := url.Parse(host)
timeout := header.GetDefaultTimeout()
if timeoutEnv, ok := os.LookupEnv("CIRCLECI_CLI_TIMEOUT"); ok {
if parsedTimeout, err := time.ParseDuration(timeoutEnv); err == nil {
timeout = parsedTimeout
} else {
fmt.Printf("failed to parse CIRCLECI_CLI_TIMEOUT: %s\n", err.Error())
}
}
client := config.HTTPClient
client.Timeout = timeout
return New(
baseURL.ResolveReference(&url.URL{Path: endpoint}),
config.Token,
client,
)
}
func (c *Client) NewRequest(method string, u *url.URL, payload interface{}) (req *http.Request, err error) {
var r io.Reader
if payload != nil {
buf := &bytes.Buffer{}
r = buf
err = json.NewEncoder(buf).Encode(payload)
if err != nil {
return nil, err
}
}
req, err = http.NewRequest(method, c.BaseURL.ResolveReference(u).String(), r)
if err != nil {
return nil, err
}
c.enrichRequestHeaders(req, payload)
return req, nil
}
func (c *Client) enrichRequestHeaders(req *http.Request, payload interface{}) {
if c.circleToken != "" {
req.Header.Set("Circle-Token", c.circleToken)
}
req.Header.Set("Accept", "application/json")
req.Header.Set("User-Agent", version.UserAgent())
commandStr := header.GetCommandStr()
if commandStr != "" {
req.Header.Set("Circleci-Cli-Command", commandStr)
}
if payload != nil {
req.Header.Set("Content-Type", "application/json")
}
}
func (c *Client) DoRequest(req *http.Request, resp interface{}) (int, error) {
httpResp, err := c.client.Do(req)
if err != nil {
fmt.Printf("failed to make http request: %s\n", err.Error())
return 0, err
}
defer httpResp.Body.Close()
if httpResp.StatusCode >= 400 {
var msgErr struct {
Message string `json:"message"`
}
body, err := io.ReadAll(httpResp.Body)
if err != nil {
return httpResp.StatusCode, err
}
err = json.Unmarshal(body, &msgErr)
if err != nil {
return httpResp.StatusCode, &HTTPError{Code: httpResp.StatusCode, Message: string(body)}
}
return httpResp.StatusCode, &HTTPError{Code: httpResp.StatusCode, Message: msgErr.Message}
}
if resp != nil {
if !strings.Contains(httpResp.Header.Get("Content-Type"), "application/json") {
body, _ := io.ReadAll(httpResp.Body)
return httpResp.StatusCode, fmt.Errorf("wrong content type received. method: %s. path: %s. content-type: %s. body: %s", req.Method, req.URL.Path, httpResp.Header.Get("Content-Type"), string(body))
}
err = json.NewDecoder(httpResp.Body).Decode(resp)
if err != nil {
return httpResp.StatusCode, err
}
}
return httpResp.StatusCode, nil
}
type HTTPError struct {
Code int
Message string
}
func (e *HTTPError) Error() string {
if e.Code == 0 {
e.Code = http.StatusInternalServerError
}
if e.Message != "" {
return e.Message
}
return fmt.Sprintf("response %d (%s)", e.Code, http.StatusText(e.Code))
}