/
user.go
53 lines (39 loc) · 1.08 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
package gorinth
import (
"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
body, status := post("https://api.modrinth.com/v2/project", overriddenValues, authHeader(user.auth), map[string]io.Reader{})
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)
}