-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain.go
159 lines (131 loc) · 3.6 KB
/
main.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package statuspage
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
)
const apiRoot = "https://api.statuspage.io/v1"
// HTTPClient is the http wrapper for the application
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}
type IClient interface {
doHTTPRequest(method, endpoint string, item interface{}) (resp *http.Response, err error)
}
type Client struct {
token string
httpClient HTTPClient
}
func NewClient(token string) *Client {
return &Client{
token: token,
httpClient: NewRetryableClient().StandardClient(),
}
}
// Allows overriding the HTTP Client, leaving the choice
// of using a retry library to the user
func (client *Client) UseHTTPClient(httpClient HTTPClient) {
client.httpClient = httpClient
}
func (client *Client) doHTTPRequest(method, endpoint string, item interface{}) (resp *http.Response, err error) {
componentURL := apiRoot + endpoint
var body io.Reader
if item != nil {
data, err := json.Marshal(item)
if err != nil {
return nil, err
}
body = bytes.NewReader(data)
}
req, err := http.NewRequest(method, componentURL, body)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "OAuth "+client.token)
resp, err = client.httpClient.Do(req)
return resp, err
}
func createResource(client IClient, pageID, resourceType string, resource, result interface{}) error {
return createResourceCustomURL(client, "/pages/"+pageID+"/"+resourceType+"s", resource, result)
}
func createResourceCustomURL(client IClient, URL string, resource, result interface{}) error {
resp, err := client.doHTTPRequest(
"POST",
URL,
resource,
)
if err != nil {
return err
}
if resp.StatusCode == http.StatusCreated {
defer resp.Body.Close()
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return json.Unmarshal(bodyBytes, &result)
}
return fmt.Errorf("failed creating resource, request returned %d, full response: %+v", resp.StatusCode, resp)
}
func readResource(client IClient, pageID, ID, resourceType string, target interface{}) error {
resp, err := client.doHTTPRequest(
"GET",
"/pages/"+pageID+"/"+resourceType+"s/"+ID,
nil,
)
if err != nil {
return err
}
if resp.Body != nil {
defer resp.Body.Close()
}
switch resp.StatusCode {
case http.StatusOK:
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return json.Unmarshal(bodyBytes, target)
case http.StatusNotFound:
return nil
default:
return fmt.Errorf("could not find %s with ID: %s, http status %d", resourceType, ID, resp.StatusCode)
}
}
func updateResource(client IClient, pageID, resourceType, ID string, resource interface{}, result interface{}) error {
resp, err := client.doHTTPRequest(
"PATCH",
"/pages/"+pageID+"/"+resourceType+"s/"+ID,
resource,
)
if err != nil {
return err
}
if resp.StatusCode == http.StatusOK {
defer resp.Body.Close()
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return json.Unmarshal(bodyBytes, &result)
}
return fmt.Errorf("failed updating %s, request returned %d", resourceType, resp.StatusCode)
}
func deleteResource(client IClient, pageID, resourceType, ID string) error {
resp, err := client.doHTTPRequest(
"DELETE",
"/pages/"+pageID+"/"+resourceType+"s/"+ID,
nil,
)
if err != nil {
return err
}
// StatusGroup deletion returns StatusOK instead of StatusNoContent like other resources
if resp.StatusCode == http.StatusNoContent || resp.StatusCode == http.StatusOK {
return nil
}
return fmt.Errorf("failed deleting %s, request returned %d", resourceType, resp.StatusCode)
}