#1: Fix Mojang's API HTTPClient default configuration, make mojang.ResponseError interface not applicable to any type, add handling of some possible network errors

This commit is contained in:
ErickSkrauch
2019-04-21 03:04:03 +03:00
parent a8bbacf8b1
commit 7d1506d0d9
4 changed files with 191 additions and 134 deletions

View File

@@ -5,9 +5,12 @@ import (
"encoding/json"
"io/ioutil"
"net/http"
"time"
)
var HttpClient = &http.Client{}
var HttpClient = &http.Client{
Timeout: 3 * time.Second,
}
type SignedTexturesResponse struct {
Id string `json:"id"`
@@ -32,10 +35,7 @@ type ProfileInfo struct {
// See https://wiki.vg/Mojang_API#Playernames_-.3E_UUIDs
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, _ := http.NewRequest("POST", "https://api.mojang.com/profiles/minecraft", bytes.NewBuffer(requestBody))
request.Header.Set("Content-Type", "application/json")
@@ -65,10 +65,7 @@ func UuidToTextures(uuid string, signed bool) (*SignedTexturesResponse, error) {
url += "?unsigned=false"
}
request, err := http.NewRequest("GET", url, nil)
if err != nil {
panic(err)
}
request, _ := http.NewRequest("GET", url, nil)
response, err := HttpClient.Do(request)
if err != nil {
@@ -92,15 +89,30 @@ func validateResponse(response *http.Response) error {
switch {
case response.StatusCode == 204:
return &EmptyResponse{}
case response.StatusCode == 400:
type errorResponse struct {
Error string `json:"error"`
Message string `json:"errorMessage"`
}
var decodedError *errorResponse
body, _ := ioutil.ReadAll(response.Body)
_ = json.Unmarshal(body, &decodedError)
return &BadRequestError{ErrorType: decodedError.Error, Message: decodedError.Message}
case response.StatusCode == 429:
return &TooManyRequestsError{}
case response.StatusCode >= 500:
return &ServerError{response.StatusCode}
return &ServerError{Status: response.StatusCode}
}
return nil
}
type ResponseError interface {
IsMojangError() bool
}
// 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 {
@@ -110,19 +122,48 @@ func (*EmptyResponse) Error() string {
return "Empty Response"
}
func (*EmptyResponse) IsMojangError() bool {
return true
}
// When passed request params are invalid, Mojang returns 400 Bad Request error
type BadRequestError struct {
ResponseError
ErrorType string
Message string
}
func (e *BadRequestError) Error() string {
return e.Message
}
func (*BadRequestError) IsMojangError() bool {
return true
}
// When you exceed the set limit of requests, this error will be returned
type TooManyRequestsError struct {
ResponseError
}
func (*TooManyRequestsError) Error() string {
return "Too Many Requests"
}
func (*TooManyRequestsError) IsMojangError() bool {
return true
}
// ServerError happens when Mojang's API returns any response with 50* status
type ServerError struct {
ResponseError
Status int
}
func (e *ServerError) Error() string {
return "Server error"
}
func (*ServerError) IsMojangError() bool {
return true
}