-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
67 lines (50 loc) · 1.23 KB
/
user.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
package gorinth
import (
"bytes"
"encoding/json"
"fmt"
"io"
)
func GetUserFromAuth(auth string) User {
body, status := get("https://api.modrinth.com/v2/user", authHeader(auth))
if status == 401 {
logError("Invalid authorisation token given")
}
if status == 200 {
user := User{}
err := json.Unmarshal(body, &user)
if err != nil {
logError(err.Error())
}
user.auth = auth
return user
}
logError("Unexpected response status %d", status)
return User{}
}
func (user User) CreateProject(project Project) error {
project.Validate()
overriddenValues := removeNullValues(project)
overriddenValues["license_id"] = project.License.Id
overriddenValues["is_draft"] = true
parts := map[string]io.Reader{}
if project.Icon != nil {
parts["icon"] = bytes.NewBuffer(project.Icon)
}
body, status := post(
"https://api.modrinth.com/v2/project",
overriddenValues,
authHeader(user.auth),
parts,
)
if status == 200 {
return nil
}
if status == 400 {
return fmt.Errorf("invalid request when attempting to create project %q: %s", project.Title, string(body))
}
if status == 401 {
return fmt.Errorf("no authorisation to create project %q", project.Title)
}
return fmt.Errorf("unexpected status code %d", status)
}