-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
139 lines (109 loc) · 2.88 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
package deploygate
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"os"
"path"
"runtime"
cleanhttp "github.com/hashicorp/go-cleanhttp"
)
const (
DGApiTokenEnv = "DEPLOYGATE_API_KEY"
DGApiEndpoint = "https://deploygate.com/api"
)
func DefaultClient() (*Client, error) {
conf := ClientConfig{
ApiKey: os.Getenv(DGApiTokenEnv),
}
c, err := NewClient(conf)
if err != nil {
return nil, err
}
return c, nil
}
type ClientConfig struct {
ApiKey string
ApiEndpoint *string
}
func NewClient(conf ClientConfig) (*Client, error) {
if len(conf.ApiKey) == 0 {
return nil, errors.New("missing apiKey")
}
if conf.ApiEndpoint == nil {
endpoint := DGApiEndpoint
conf.ApiEndpoint = &endpoint
}
endpoint, err := url.Parse(*conf.ApiEndpoint)
if err != nil {
return nil, err
}
c := &Client{apiKey: conf.ApiKey, endpoint: endpoint}
if c.HttpClient == nil {
c.HttpClient = cleanhttp.DefaultClient()
}
return c, nil
}
const packageName = "github.com/fnaoto/go_deploygate"
var userAgent = fmt.Sprintf("GoDeployGate (+%s; %s)", packageName, runtime.Version())
func (c *Client) Get(httpRequest *HttpRequest) (*http.Response, error) {
httpRequest.method = "GET"
return c.NewRequest(httpRequest)
}
func (c *Client) Post(httpRequest *HttpRequest) (*http.Response, error) {
httpRequest.method = "POST"
return c.NewRequest(httpRequest)
}
func (c *Client) Patch(httpRequest *HttpRequest) (*http.Response, error) {
httpRequest.method = "PATCH"
return c.NewRequest(httpRequest)
}
func (c *Client) Delete(httpRequest *HttpRequest) (*http.Response, error) {
httpRequest.method = "DELETE"
return c.NewRequest(httpRequest)
}
func (c *Client) NewRequest(httpRequest *HttpRequest) (*http.Response, error) {
u := *c.endpoint
u.Path = path.Join(c.endpoint.Path, httpRequest.path)
req, err := http.NewRequest(httpRequest.method, u.String(), httpRequest.body)
if err != nil {
return nil, err
}
if httpRequest.header == nil {
httpRequest.header = &Header{
accept: "application/json",
contentType: "application/x-www-form-urlencoded",
}
}
req.Header.Set("Accept", httpRequest.header.accept)
req.Header.Set("Content-Type", httpRequest.header.contentType)
req.Header.Set("User-Agent", userAgent)
req.Header.Set("Authorization", fmt.Sprintf("Token %s", c.apiKey))
resp, err := c.HttpClient.Do(req)
if err != nil {
return nil, err
}
return resp, nil
}
func (c *Client) Decode(resp *http.Response, out interface{}) error {
defer resp.Body.Close()
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
if resp.StatusCode >= 300 {
return fmt.Errorf("status code is %v, body is %v", resp.StatusCode, buf.String())
}
if resp.ContentLength == 0 {
return nil
}
if buf.String() == "" {
return nil
}
err := json.Unmarshal(buf.Bytes(), &out)
if err != nil {
return fmt.Errorf("%v, output type is %T, body is %v", err, out, buf.String())
}
return nil
}