-
Notifications
You must be signed in to change notification settings - Fork 7
/
profiles.go
96 lines (79 loc) · 2.72 KB
/
profiles.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
package minecraft
import (
"encoding/json"
"strings"
"github.com/pkg/errors"
)
type User struct {
UUID string `json:"id"`
Username string `json:"name"`
}
type APIProfileResponse struct {
User
Legacy bool `json:"legacy"`
Demo bool `json:"demo"`
}
type SessionProfileResponse struct {
User
Properties []SessionProfileProperty `json:"properties"`
}
type SessionProfileProperty struct {
Name string `json:"name"`
Value string `json:"value"`
}
// GetAPIProfile returns the API profile for a given username primarily of use
// for getting the UUID, but can also correct the capitilzation of a username or
// possibly get the account status (legacy or demo) - only included when true
func (mc *Minecraft) GetAPIProfile(username string) (APIProfileResponse, error) {
url := mc.UUIDAPI.ProfileURL
url += username
apiBody, err := mc.apiRequest(url)
if apiBody != nil {
defer apiBody.Close()
}
if err != nil {
return APIProfileResponse{}, errors.Wrap(err, "unable to GetAPIProfile")
}
apiProfile := APIProfileResponse{}
err = json.NewDecoder(apiBody).Decode(&apiProfile)
if err != nil {
return APIProfileResponse{}, errors.Wrap(err, "decoding GetAPIProfile failed")
}
return apiProfile, nil
}
// GetUUID returns the UUID for a given username (shorthand for GetAPIProfile)
func (mc *Minecraft) GetUUID(username string) (string, error) {
apiProfile, err := mc.GetAPIProfile(username)
return apiProfile.UUID, err
}
// NormalizePlayerForUUID takes either a Username or UUID and returns a UUID
// formatted without dashes, or an error (eg. no account or an API error)
func (mc *Minecraft) NormalizePlayerForUUID(player string) (string, error) {
if RegexUsername.MatchString(player) {
return mc.GetUUID(player)
} else if RegexUUID.MatchString(player) {
return strings.Replace(player, "-", "", 4), nil
}
// We shouldn't get this far as there should have been Regex checks already.
return "", errors.New("unable to NormalizePlayerForUUID due to invalid Username/UUID")
}
// GetSessionProfile fetches the session profile of the UUID, this includes
// extra properties for the user (currently just a textures property)
// Rate limits if performing same request within 30 seconds
func (mc *Minecraft) GetSessionProfile(uuid string) (SessionProfileResponse, error) {
url := mc.UUIDAPI.SessionServerURL
url += uuid
apiBody, err := mc.apiRequest(url)
if apiBody != nil {
defer apiBody.Close()
}
if err != nil {
return SessionProfileResponse{}, errors.Wrap(err, "unable to GetSessionProfile")
}
sessionProfile := SessionProfileResponse{}
err = json.NewDecoder(apiBody).Decode(&sessionProfile)
if err != nil {
return SessionProfileResponse{}, errors.Wrap(err, "decoding GetSessionProfile failed")
}
return sessionProfile, nil
}