This repository was archived by the owner on May 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
167 lines (142 loc) · 4.34 KB
/
client.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
package mojango
import (
"encoding/json"
"fmt"
"github.com/valyala/fasthttp"
)
// Client represents an API client
type Client struct {
http *fasthttp.Client
}
// New creates a new fasthttp client and wraps it into an API client
func New() *Client {
return &Client{
http: &fasthttp.Client{
Name: "mojango",
},
}
}
// FetchStatus fetches the states of all Mojang services and wraps them into a single object
func (client *Client) FetchStatus() (*Status, error) {
// Call the Mojang status endpoint
code, body, err := client.http.Get(nil, fmt.Sprintf("%s/check", uriStatus))
if err != nil {
return nil, err
}
// Handle possible errors
if code != fasthttp.StatusOK {
return nil, errorFromCode(code)
}
// Parse the result into a status object and return it
return parseStatusFromBody(body)
}
// FetchUUID fetches the current UUID of the given username
func (client *Client) FetchUUID(username string) (string, error) {
return client.FetchUUIDAtTime(username, -1)
}
// FetchUUIDAtTime fetches the UUID of the given username at a given timestamp
func (client *Client) FetchUUIDAtTime(username string, timestamp int64) (string, error) {
// Call the Mojang profile endpoint
atExtension := ""
if timestamp >= 0 {
atExtension = fmt.Sprintf("?at=%d", timestamp)
}
code, body, err := client.http.Get(nil, fmt.Sprintf("%s/users/profiles/minecraft/%s%s", uriApi, username, atExtension))
if err != nil {
return "", err
}
// Handle possible errors
if code != fasthttp.StatusOK {
return "", errorFromCode(code)
}
// Parse the result into a map containing the profile data
var result map[string]interface{}
err = json.Unmarshal(body, &result)
if err != nil {
return "", err
}
// Return the UUID of the requested profile
return result["id"].(string), nil
}
// FetchMultipleUUIDs fetches the UUIDs of the given usernames
func (client *Client) FetchMultipleUUIDs(usernames []string) (map[string]string, error) {
// Define the request object
request := fasthttp.AcquireRequest()
defer fasthttp.ReleaseRequest(request)
request.SetRequestURI(fmt.Sprintf("%s/profiles/minecraft", uriApi))
request.Header.SetMethod("POST")
request.Header.SetContentType("application/json")
reqBody, err := json.Marshal(usernames)
if err != nil {
return nil, err
}
request.SetBody(reqBody)
// Define the response object
response := fasthttp.AcquireResponse()
defer fasthttp.ReleaseResponse(response)
// Call the Mojang profile endpoint
err = client.http.Do(request, response)
if err != nil {
return nil, err
}
// Define the important response values
code := response.StatusCode()
body := response.Body()
// Handle possible errors
if code != fasthttp.StatusOK {
return nil, errorFromCode(code)
}
// Parse the response body into a list of results
var rawResults []struct {
UUID string `json:"id"`
Name string `json:"name"`
}
err = json.Unmarshal(body, &rawResults)
if err != nil {
return nil, err
}
// Parse the list of results into a map and return it
result := make(map[string]string)
for _, rawResult := range rawResults {
result[rawResult.Name] = rawResult.UUID
}
return result, nil
}
// FetchNameHistory fetches all names of the given UUID and their corresponding changing timestamps
func (client *Client) FetchNameHistory(uuid string) ([]NameHistoryEntry, error) {
// Call the Mojang profile endpoint
code, body, err := client.http.Get(nil, fmt.Sprintf("%s/user/profiles/%s/names", uriApi, uuid))
if err != nil {
return nil, err
}
// Handle possible errors
if code != fasthttp.StatusOK {
return nil, errorFromCode(code)
}
// Parse the response body into a list of name history entries and return it
var entries []NameHistoryEntry
err = json.Unmarshal(body, &entries)
if err != nil {
return nil, err
}
return entries, nil
}
// FetchProfile fetches the profile of the given UUID
func (client *Client) FetchProfile(uuid string, unsigned bool) (*Profile, error) {
// Call the Mojang profile endpoint
code, body, err := client.http.Get(nil, fmt.Sprintf("%s/session/minecraft/profile/%s?unsigned=%t", uriSession, uuid, unsigned))
if err != nil {
return nil, err
}
// Handle possible errors
if code != fasthttp.StatusOK {
return nil, errorFromCode(code)
}
// Parse the response body into a profile and return it
profile := new(Profile)
err = json.Unmarshal(body, profile)
if err != nil {
return nil, err
}
return profile, nil
}