-
Notifications
You must be signed in to change notification settings - Fork 0
/
firim.go
187 lines (164 loc) · 3.96 KB
/
firim.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
package drone_firim
import (
"bytes"
"encoding/json"
"errors"
"gopkg.in/h2non/gentleman.v2"
"gopkg.in/h2non/gentleman.v2/plugins/body"
"io"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"strings"
)
const host = "http://api.fir.im"
type Firim struct {
AppType string
BundleId string
ApiToken string
host string
upload *Upload
}
type Upload struct {
File string
Name string
Version string
Build string
ReleaseType string
Changelog string
}
func NewUpload(file string, name string, version string, build string, releaseType string, changelog string) *Upload {
return &Upload{File: file, Name: name, Version: version, Build: build, ReleaseType: releaseType, Changelog: changelog}
}
type AppsResponse struct {
ID string `json:"id"`
Type string `json:"type"`
Short string `json:"short"`
Cert struct {
Icon struct {
Key string `json:"key"`
Token string `json:"token"`
UploadURL string `json:"upload_url"`
} `json:"icon"`
Binary struct {
Key string `json:"key"`
Token string `json:"token"`
UploadURL string `json:"upload_url"`
} `json:"binary"`
} `json:"cert"`
}
func NewFirim(appType string, bundleId string, apiToken string, file string, name string, version string, build string, releaseType string, changelog string) *Firim {
appType = strings.ToLower(appType)
if appType != "ios" && appType != "android" {
appType = "android"
}
return &Firim{
AppType: appType,
BundleId: bundleId,
ApiToken: apiToken,
host: host,
upload: NewUpload(file, name, version, build, releaseType, changelog),
}
}
func (f *Firim) getToken() (AppsResponse, error) {
cli := gentleman.New()
cli.URL(f.host)
req := cli.Request()
req.Path("/apps")
data := map[string]string{
"type": f.AppType,
"bundle_id": f.BundleId,
"api_token": f.ApiToken,
}
req.Method("POST")
req.Use(body.JSON(data))
res, err := req.Send()
if err != nil {
return AppsResponse{}, err
}
var apps AppsResponse
err = json.Unmarshal(res.Bytes(), &apps)
if err != nil {
return AppsResponse{}, err
}
return apps, nil
}
func (f *Firim) check() error {
if f.ApiToken == "" {
return errors.New("api_token can not nil")
}
if f.BundleId == "" {
return errors.New("bundle_id can not nil")
}
if f.upload.Name == "" {
return errors.New("name can not nil")
}
if f.upload.Build == "" {
return errors.New("build cant nil")
}
if f.upload.Version == "" {
return errors.New("version cant nil")
}
if f.upload.File == "" {
return errors.New("file cant nil")
}
return nil
}
func (f *Firim) Exec() error {
if err := f.check(); nil != err {
return err
}
apps, err := f.getToken()
if err != nil {
return err
}
extraParams := map[string]string{
"key": apps.Cert.Binary.Key,
"token": apps.Cert.Binary.Token,
"x:name": f.upload.Name,
"x:version": f.upload.Version,
"x:build": f.upload.Build,
}
if f.upload.Changelog != "" {
extraParams["x:changelog"] = f.upload.Changelog
}
if f.upload.ReleaseType != "" {
extraParams["x:release_type"] = f.upload.ReleaseType
}
request, err := fileUploadRequest(apps.Cert.Binary.UploadURL, extraParams, "file", f.upload.File)
if err != nil {
return err
}
client := &http.Client{}
resp, err := client.Do(request)
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}
func fileUploadRequest(uri string, params map[string]string, paramName, path string) (*http.Request, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
buffer := &bytes.Buffer{}
writer := multipart.NewWriter(buffer)
part, err := writer.CreateFormFile(paramName, filepath.Base(path))
if err != nil {
return nil, err
}
_, err = io.Copy(part, file)
for key, val := range params {
_ = writer.WriteField(key, val)
}
err = writer.Close()
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", uri, buffer)
req.Header.Set("Content-Type", writer.FormDataContentType())
return req, err
}