chrly/api/mojang/mojang.go

118 lines
2.6 KiB
Go
Raw Normal View History

2019-04-14 20:06:27 +05:30
package mojang
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
)
var HttpClient = &http.Client{}
type SignedTexturesResponse struct {
Id string `json:"id"`
Name string `json:"name"`
Props []Property `json:"properties"`
}
type Property struct {
Name string `json:"name"`
Signature string `json:"signature,omitempty"`
Value string `json:"value"`
}
type ProfileInfo struct {
Id string `json:"id"`
Name string `json:"name"`
IsLegacy bool `json:"legacy,omitempty"`
IsDemo bool `json:"demo,omitempty"`
}
// Exchanges usernames array to array of uuids
// See https://wiki.vg/Mojang_API#Playernames_-.3E_UUIDs
2019-04-14 20:06:27 +05:30
func UsernamesToUuids(usernames []string) ([]*ProfileInfo, error) {
requestBody, _ := json.Marshal(usernames)
request, err := http.NewRequest("POST", "https://api.mojang.com/profiles/minecraft", bytes.NewBuffer(requestBody))
if err != nil {
panic(err)
}
request.Header.Set("Content-Type", "application/json")
response, err := HttpClient.Do(request)
if err != nil {
return nil, err
}
defer response.Body.Close()
if responseErr := validateResponse(response); responseErr != nil {
return nil, responseErr
2019-04-14 20:06:27 +05:30
}
var result []*ProfileInfo
body, _ := ioutil.ReadAll(response.Body)
_ = json.Unmarshal(body, &result)
return result, nil
}
// Obtains textures information for provided uuid
// See https://wiki.vg/Mojang_API#UUID_-.3E_Profile_.2B_Skin.2FCape
func UuidToTextures(uuid string, signed bool) (*SignedTexturesResponse, error) {
url := "https://sessionserver.mojang.com/session/minecraft/profile/" + uuid
if signed {
url += "?unsigned=false"
}
request, err := http.NewRequest("GET", url, nil)
2019-04-14 20:06:27 +05:30
if err != nil {
panic(err)
}
response, err := HttpClient.Do(request)
if err != nil {
return nil, err
}
defer response.Body.Close()
if responseErr := validateResponse(response); responseErr != nil {
return nil, responseErr
2019-04-14 20:06:27 +05:30
}
var result *SignedTexturesResponse
body, _ := ioutil.ReadAll(response.Body)
_ = json.Unmarshal(body, &result)
return result, nil
}
func validateResponse(response *http.Response) error {
switch response.StatusCode {
case 204:
return &EmptyResponse{}
case 429:
return &TooManyRequestsError{}
}
return nil
}
// Mojang API doesn't return a 404 Not Found error for non-existent data identifiers
// Instead, they return 204 with an empty body
type EmptyResponse struct {
}
func (*EmptyResponse) Error() string {
return "Empty Response"
}
// When you exceed the set limit of requests, this error will be returned
2019-04-14 20:06:27 +05:30
type TooManyRequestsError struct {
}
func (*TooManyRequestsError) Error() string {
2019-04-14 20:06:27 +05:30
return "Too Many Requests"
}