mirror of
https://github.com/elyby/chrly.git
synced 2024-11-06 08:11:11 +05:30
72 lines
1.5 KiB
Go
72 lines
1.5 KiB
Go
|
package mojangtextures
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"io/ioutil"
|
||
|
"net/http"
|
||
|
. "net/url"
|
||
|
"path"
|
||
|
"time"
|
||
|
|
||
|
"github.com/mono83/slf/wd"
|
||
|
|
||
|
"github.com/elyby/chrly/api/mojang"
|
||
|
"github.com/elyby/chrly/bootstrap"
|
||
|
)
|
||
|
|
||
|
var HttpClient = &http.Client{
|
||
|
Transport: &http.Transport{
|
||
|
MaxIdleConnsPerHost: 1024,
|
||
|
},
|
||
|
}
|
||
|
|
||
|
type RemoteApiUuidsProvider struct {
|
||
|
Url URL
|
||
|
Logger wd.Watchdog
|
||
|
}
|
||
|
|
||
|
func (ctx *RemoteApiUuidsProvider) GetUuid(username string) (*mojang.ProfileInfo, error) {
|
||
|
ctx.Logger.IncCounter("mojang_textures.usernames.request", 1)
|
||
|
|
||
|
url := ctx.Url
|
||
|
url.Path = path.Join(url.Path, username)
|
||
|
|
||
|
request, _ := http.NewRequest("GET", url.String(), nil)
|
||
|
request.Header.Add("Accept", "application/json")
|
||
|
// Change default User-Agent to allow specify "Username -> UUID at time" Mojang's api endpoint
|
||
|
request.Header.Add("User-Agent", "Chrly/"+bootstrap.GetVersion())
|
||
|
|
||
|
start := time.Now()
|
||
|
response, err := HttpClient.Do(request)
|
||
|
ctx.Logger.RecordTimer("mojang_textures.usernames.request_time", time.Since(start))
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
defer response.Body.Close()
|
||
|
|
||
|
if response.StatusCode == 204 {
|
||
|
return nil, nil
|
||
|
}
|
||
|
|
||
|
if response.StatusCode != 200 {
|
||
|
return nil, &UnexpectedRemoteApiResponse{response}
|
||
|
}
|
||
|
|
||
|
var result *mojang.ProfileInfo
|
||
|
body, _ := ioutil.ReadAll(response.Body)
|
||
|
err = json.Unmarshal(body, &result)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return result, nil
|
||
|
}
|
||
|
|
||
|
type UnexpectedRemoteApiResponse struct {
|
||
|
Response *http.Response
|
||
|
}
|
||
|
|
||
|
func (*UnexpectedRemoteApiResponse) Error() string {
|
||
|
return "Unexpected remote api response"
|
||
|
}
|