-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.go
145 lines (125 loc) · 4.36 KB
/
render.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
package render
import (
"encoding/json"
"fmt"
"net/http"
"time"
)
type RenderClient struct {
httpClient http.Client
token string
}
type Deploy struct {
ID string `json:"id"`
Commit string `json:"commit"`
Status string `json:"status"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
FinishedAt time.Time `json:"finishedAt"`
}
type ServiceDetailsEnv struct {
BuildCommand string `json:"buildCommand"`
StartCommand string `json:"startCommand"`
DockerCommand string `json:"dockerCommand"`
DockerContext string `json:"dockerContext"`
DockerfilePath string `json:"dockerfilePath"`
}
type ServiceDetails struct {
Disk map[string]interface{} `json:"disk"`
Env string `json:"env"`
EnvSpecificDetails ServiceDetailsEnv `json:"envSpecificDetails"`
HealthCheckPath string `json:"healthCheckPath"`
NumInstances int `json:"numInstances"`
OpenPorts map[string]interface{} `json:"openPorts"`
PublishPath string `json:"publishPath"`
ParentServer map[string]interface{} `json:"parentServer"`
Plan string `json:"plan"`
PullRequestPreviewsEnabled bool `json:"pullRequestPreviewsEnabled"`
Region string `json:"region"`
URL string `json:"url"`
BuildCommand string `json:"buildCommand"`
}
type Service struct {
ID string `json:"id"`
Type string `json:"type"`
Repo string `json:"repo"`
Name string `json:"name"`
AutoDeploy bool `json:"autoDeploy"`
Branch string `json:"branch"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
NotifyOnFail string `json:"notifyOnFail"`
OwnerID string `json:"ownerId"`
Slug string `json:"slug"`
Suspenders []string `json:"suspenders"`
Schedule string `json:"schedule"`
LastSuccessfulRunAt time.Time `json:"lastSuccessfulRunAt"`
ServiceDetails ServiceDetails `json:"serviceDetails"`
Deploys []Deploy `json:"deploys"`
}
type ResponseItem struct {
Cursor string `json:"cursor"`
Service Service `json:"service,omitempty"`
Deploy Deploy `json:"deploy,omitempty"`
}
type Response []ResponseItem
func New(token string) (*RenderClient, error) {
renderClient := new(RenderClient)
renderClient.httpClient = http.Client{Timeout: 10 * time.Second}
renderClient.token = token
return renderClient, nil
}
func (r *RenderClient) ListServices() ([]Service, error) {
req, err := http.NewRequest(
"GET",
"https://api.render.com/v1/services",
nil,
)
if err != nil {
return []Service{}, err
}
req.Header.Add(
"Authorization",
fmt.Sprintf("Bearer %s", r.token),
)
resp, err := r.httpClient.Do(req)
if err != nil {
return []Service{}, err
}
defer resp.Body.Close()
var response Response
json.NewDecoder(resp.Body).Decode(&response)
var services []Service
for _, responseItem := range response {
deploys, _ := r.ListDeploys(responseItem.Service.ID)
responseItem.Service.Deploys = deploys
services = append(services, responseItem.Service)
}
return services, nil
}
func (r *RenderClient) ListDeploys(serviceID string) ([]Deploy, error) {
req, err := http.NewRequest(
"GET",
fmt.Sprintf("https://api.render.com/v1/services/%s/deploys", serviceID),
nil,
)
if err != nil {
return []Deploy{}, err
}
req.Header.Add(
"Authorization",
fmt.Sprintf("Bearer %s", r.token),
)
resp, err := r.httpClient.Do(req)
if err != nil {
return []Deploy{}, err
}
defer resp.Body.Close()
var response Response
json.NewDecoder(resp.Body).Decode(&response)
var deploys []Deploy
for _, responseItem := range response {
deploys = append(deploys, responseItem.Deploy)
}
return deploys, nil
}