-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapplication.go
194 lines (182 loc) · 4.86 KB
/
application.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package resingo
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
)
//Application holds information about the application that is running on resin.
type Application struct {
ID int64 `json:"id"`
Name string `json:"app_name"`
Repository string `json:"git_repository"`
Metadata struct {
URI string `json:"uri"`
Type string `json:"type"`
} `json:"__metadata"`
DeviceType string `json:"device_type"`
User User `json:"user"`
Commit string `json:"commit"`
}
//AppGetAll retrieves all applications that belog to the user in the given
//context.
//
// For this to work, the context should be authorized, probably using the Login
// function.
func AppGetAll(ctx *Context) ([]*Application, error) {
h := authHeader(ctx.Config.AuthToken)
uri := ctx.Config.APIEndpoint("application")
b, err := doJSON(ctx, "GET", uri, h, nil, nil)
if err != nil {
return nil, err
}
var appRes = struct {
D []*Application `json:"d"`
}{}
err = json.Unmarshal(b, &appRes)
if err != nil {
return nil, err
}
return appRes.D, nil
}
//AppGetByName returns the application with the giveb name.
func AppGetByName(ctx *Context, name string) (*Application, error) {
h := authHeader(ctx.Config.AuthToken)
uri := ctx.Config.APIEndpoint("application")
params := make(url.Values)
params.Set("filter", "app_name")
params.Set("eq", name)
b, err := doJSON(ctx, "GET", uri, h, params, nil)
if err != nil {
return nil, err
}
var appRes = struct {
D []*Application `json:"d"`
}{}
err = json.Unmarshal(b, &appRes)
if err != nil {
return nil, err
}
if len(appRes.D) > 0 {
return appRes.D[0], nil
}
return nil, errors.New("application not found")
}
//The client expects a 200 status code for a successful reqest, any
// other status code will result into an error with the form [code] [ Any
// content read from the response body]
func do(ctx *Context, method, uri string, header http.Header,
params url.Values, body io.Reader) ([]byte, error) {
if params != nil {
uri = uri + "?" + Encode(params)
}
req, err := http.NewRequest(method, uri, body)
if err != nil {
return nil, err
}
if header != nil {
req.Header = header
}
req.Header = header
resp, err := ctx.Client.Do(req)
if err != nil {
return nil, err
}
defer func() {
_ = resp.Body.Close()
}()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if !checkStatus(resp.StatusCode) {
return nil, fmt.Errorf("resingo: [%d ] %s : %s", resp.StatusCode, req.URL.RequestURI(), string(b))
}
return b, nil
}
func checkStatus(status int) bool {
switch status {
case http.StatusOK, http.StatusCreated, http.StatusAccepted:
return true
}
return false
}
func doJSON(ctx *Context, method, uri string, header http.Header,
params url.Values, body io.Reader) ([]byte, error) {
header.Set("Content-Type", "application/json")
return do(ctx, method, uri, header, params, body)
}
//AppGetByID returns application with the given id
func AppGetByID(ctx *Context, id int64) (*Application, error) {
h := authHeader(ctx.Config.AuthToken)
uri := ctx.Config.APIEndpoint(fmt.Sprintf("application(%d)", id))
b, err := doJSON(ctx, "GET", uri, h, nil, nil)
if err != nil {
return nil, err
}
var appRes = struct {
D []*Application `json:"d"`
}{}
err = json.Unmarshal(b, &appRes)
if err != nil {
return nil, err
}
if len(appRes.D) > 0 {
return appRes.D[0], nil
}
return nil, errors.New("application not found")
}
//AppCreate creates a new application with the given name
func AppCreate(ctx *Context, name string, typ DeviceType) (*Application, error) {
h := authHeader(ctx.Config.AuthToken)
uri := ctx.Config.APIEndpoint("application")
data := make(map[string]interface{})
data["app_name"] = name
data["device_type"] = typ.String()
body, err := marhsalReader(data)
if err != nil {
return nil, err
}
b, err := doJSON(ctx, "POST", uri, h, nil, body)
if err != nil {
return nil, err
}
rst := &Application{}
err = json.Unmarshal(b, rst)
if err != nil {
return nil, err
}
return rst, nil
}
func marhsalReader(o interface{}) (io.Reader, error) {
b, err := json.Marshal(o)
if err != nil {
return nil, err
}
return bytes.NewReader(b), nil
}
//AppDelete removes the application with the given id
func AppDelete(ctx *Context, id int64) (bool, error) {
h := authHeader(ctx.Config.AuthToken)
uri := ctx.Config.APIEndpoint(fmt.Sprintf("application(%d)", id))
b, err := doJSON(ctx, "DELETE", uri, h, nil, nil)
if err != nil {
return false, err
}
return string(b) == "OK", nil
}
//AppGetAPIKey returns the application with the given api key
func AppGetAPIKey(ctx *Context, name string) ([]byte, error) {
h := authHeader(ctx.Config.AuthToken)
app, err := AppGetByName(ctx, name)
if err != nil {
return nil, err
}
end := fmt.Sprintf("application/%d/generate-api-key", app.ID)
uri := "https://api.resin.io/" + end
return doJSON(ctx, "POST", uri, h, nil, nil)
}