-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmods.go
More file actions
327 lines (318 loc) · 9.72 KB
/
mods.go
File metadata and controls
327 lines (318 loc) · 9.72 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package gomodio
import (
"bytes"
"encoding/json"
"errors"
"io"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
// Mods struct which maps to the JSON response of Get Mods
type Mods struct {
Data []Mod `json:"data"`
ResultCount int `json:"result_count"`
ResultLimit int `json:"result_limit"`
ResultTotal int `json:"result_total"`
ResultOffset int `json:"result_offset"`
}
// Mod struct which maps to the JSON response of Get/Edit/Add/Delete Mod/s
type Mod struct {
ID int `json:"id"`
GameID int `json:"game_id"`
Status int `json:"status"`
Visible int `json:"visible"`
SubmittedBy struct {
ID int `json:"id"`
NameID string `json:"name_id"`
Username string `json:"username"`
DateOnline int `json:"date_online"`
Avatar struct {
Filename string `json:"filename"`
Original string `json:"original"`
Thumb50X50 string `json:"thumb_50x50"`
Thumb100X100 string `json:"thumb_100x100"`
} `json:"avatar"`
Timezone string `json:"timezone"`
Language string `json:"language"`
ProfileURL string `json:"profile_url"`
} `json:"submitted_by"`
DateAdded int `json:"date_added"`
DateUpdated int `json:"date_updated"`
DateLive int `json:"date_live"`
MaturityOption int `json:"maturity_option"`
Logo struct {
Filename string `json:"filename"`
Original string `json:"original"`
Thumb320X180 string `json:"thumb_320x180"`
Thumb640X360 string `json:"thumb_640x360"`
Thumb1280X720 string `json:"thumb_1280x720"`
} `json:"logo"`
HomepageURL string `json:"homepage_url"`
Name string `json:"name"`
NameID string `json:"name_id"`
Summary string `json:"summary"`
Description string `json:"description"`
DescriptionPlaintext string `json:"description_plaintext"`
MetadataBlob string `json:"metadata_blob"`
ProfileURL string `json:"profile_url"`
Media struct {
Youtube []string `json:"youtube"`
Sketchfab []string `json:"sketchfab"`
Images []struct {
Filename string `json:"filename"`
Original string `json:"original"`
Thumb320X180 string `json:"thumb_320x180"`
} `json:"images"`
} `json:"media"`
Modfile struct {
ID int `json:"id"`
ModID int `json:"mod_id"`
DateAdded int `json:"date_added"`
DateScanned int `json:"date_scanned"`
VirusStatus int `json:"virus_status"`
VirusPositive int `json:"virus_positive"`
VirustotalHash string `json:"virustotal_hash"`
Filesize int `json:"filesize"`
Filehash struct {
Md5 string `json:"md5"`
} `json:"filehash"`
Filename string `json:"filename"`
Version string `json:"version"`
Changelog string `json:"changelog"`
MetadataBlob string `json:"metadata_blob"`
Download struct {
BinaryURL string `json:"binary_url"`
DateExpires int `json:"date_expires"`
} `json:"download"`
} `json:"modfile"`
MetadataKvp []struct {
Metakey string `json:"metakey"`
Metavalue string `json:"metavalue"`
} `json:"metadata_kvp"`
Tags []struct {
Name string `json:"name"`
DateAdded int `json:"date_added"`
} `json:"tags"`
Stats struct {
ModID int `json:"mod_id"`
PopularityRankPosition int `json:"popularity_rank_position"`
PopularityRankTotalMods int `json:"popularity_rank_total_mods"`
DownloadsTotal int `json:"downloads_total"`
SubscribersTotal int `json:"subscribers_total"`
RatingsTotal int `json:"ratings_total"`
RatingsPositive int `json:"ratings_positive"`
RatingsNegative int `json:"ratings_negative"`
RatingsPercentagePositive int `json:"ratings_percentage_positive"`
RatingsWeightedAggregate float64 `json:"ratings_weighted_aggregate"`
RatingsDisplayText string `json:"ratings_display_text"`
DateExpires int `json:"date_expires"`
} `json:"stats"`
}
// GetMods searches for mods and returns a Mods object
func (user *User) GetMods(gameID int, query map[string]string) (res *Mods, err error) {
query["api_key"] = user.APIKey()
queryString := ParseArgsGet(query)
client := http.Client{Timeout: time.Duration(5 * time.Second)}
req, err := http.NewRequest("GET", "https://api.mod.io/v1/games/"+strconv.Itoa(gameID)+"/mods?"+queryString, nil)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
if err != nil {
return res, err
}
if user.OAuth2Token() != "" || len(user.OAuth2Token()) > 1 {
req.Header.Set("Authorization", "Bearer "+user.OAuth2Token())
}
resp, err := client.Do(req)
if err != nil {
return res, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if resp.StatusCode != 200 {
var errObj ErrorCase
e := json.Unmarshal(body, &errObj)
if e != nil {
log.Fatalln(e)
}
return nil, HandleResponseError(errObj)
}
if err != nil {
return res, err
}
err = json.Unmarshal(body, &res)
if err != nil {
return res, err
}
return res, nil
}
// EditMod edits a mod
func (user *User) EditMod(modID int, gameID int, options map[string]string) (res *Mod, err error) {
if user.OAuth2Token() == "" {
return res, errors.New("requires OAuth2 token")
}
reqBody := ParseArgsBody(options)
client := http.Client{Timeout: time.Duration(5 * time.Second)}
req, err := http.NewRequest("PUT", "https://api.mod.io/v1/games/"+strconv.Itoa(gameID)+"/mods/"+strconv.Itoa(modID), strings.NewReader(reqBody.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
if err != nil {
return res, err
}
if user.OAuth2Token() != "" || len(user.OAuth2Token()) > 1 {
req.Header.Set("Authorization", "Bearer "+user.OAuth2Token())
}
resp, err := client.Do(req)
if err != nil {
return res, err
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if resp.StatusCode != 200 {
var errObj ErrorCase
e := json.Unmarshal(b, &errObj)
if e != nil {
log.Fatalln(e)
}
return nil, HandleResponseError(errObj)
}
if err != nil {
return res, err
}
err = json.Unmarshal(b, &res)
if err != nil {
return res, err
}
return res, nil
}
// DeleteMod sends a request to delete a mod
func (user *User) DeleteMod(modID int, gameID int) (err error) {
client := http.Client{Timeout: time.Duration(5 * time.Second)}
req, err := http.NewRequest("DELETE", "https://api.mod.io/v1/games/"+strconv.Itoa(gameID)+"/mods/"+strconv.Itoa(modID), nil)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
if err != nil {
return err
}
if user.OAuth2Token() != "" || len(user.OAuth2Token()) > 1 {
req.Header.Set("Authorization", "Bearer "+user.OAuth2Token())
}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode != 204 {
var errObj ErrorCase
e := json.Unmarshal(body, &errObj)
if e != nil {
log.Fatalln(e)
}
return HandleResponseError(errObj)
}
return nil
}
// AddMod adds a mod taking bytes for files and returns Mod object
func (user *User) AddMod(logo string, modName string, summary string, options map[string]string, gameID int) (res *Mod, err error) {
if user.OAuth2Token() == "" {
return res, errors.New("requires OAuth2 token")
}
file, err := os.Open(logo)
if err != nil {
return res, err
}
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("logo", filepath.Base(logo))
if err != nil {
return res, err
}
_, err = io.Copy(part, file)
_ = writer.WriteField("name", modName)
_ = writer.WriteField("summary", summary)
if options != nil {
for k, v := range options {
_ = writer.WriteField(k, v)
}
}
client := http.Client{Timeout: time.Duration(5 * time.Second)}
req, err := http.NewRequest("POST", "https://api.mod.io/v1/games/"+strconv.Itoa(gameID)+"/mods", body)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
if err != nil {
return res, err
}
if user.OAuth2Token() != "" || len(user.OAuth2Token()) > 1 {
req.Header.Set("Authorization", "Bearer "+user.OAuth2Token())
}
resp, err := client.Do(req)
if err != nil {
return res, err
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return res, err
}
if resp.StatusCode != 201 {
var errObj ErrorCase
e := json.Unmarshal(b, &errObj)
if e != nil {
log.Fatalln(e)
}
return nil, HandleResponseError(errObj)
}
err = json.Unmarshal(b, &res)
if err != nil {
return res, err
}
return res, nil
}
// GetMod searches for a mod and returns a Mods object
func (user *User) GetMod(modID int, gameID int, query map[string]string) (res *Mods, err error) {
var queryString string
if query != nil {
query["api_key"] = user.APIKey()
queryString = ParseArgsGet(query)
} else {
queryString = "api_key=" + user.APIKey()
}
client := http.Client{Timeout: time.Duration(5 * time.Second)}
req, err := http.NewRequest("GET", "https://api.mod.io/v1/games/"+strconv.Itoa(gameID)+"/mods/"+strconv.Itoa(gameID)+"?"+queryString, nil)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
if err != nil {
return res, err
}
if user.OAuth2Token() != "" || len(user.OAuth2Token()) > 1 {
req.Header.Set("Authorization", "Bearer "+user.OAuth2Token())
}
resp, err := client.Do(req)
if err != nil {
return res, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if resp.StatusCode != 200 {
var errObj ErrorCase
e := json.Unmarshal(body, &errObj)
if e != nil {
log.Fatalln(e)
}
return nil, HandleResponseError(errObj)
}
if err != nil {
return res, err
}
err = json.Unmarshal(body, &res)
if err != nil {
return res, err
}
return res, err
}