2017-04-10 17:23:26 +05:30
|
|
|
package worker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"elyby/minecraft-skinsystem/lib/external/accounts"
|
|
|
|
)
|
|
|
|
|
2017-04-10 22:58:47 +05:30
|
|
|
var AccountsTokenConfig *accounts.TokenRequest
|
|
|
|
|
2017-04-10 17:23:26 +05:30
|
|
|
var token *accounts.Token
|
|
|
|
|
|
|
|
const repeatsLimit = 3
|
|
|
|
var repeatsCount = 0
|
|
|
|
|
|
|
|
func getById(id int) (accounts.AccountInfoResponse, error) {
|
|
|
|
return _getByField("id", strconv.Itoa(id))
|
|
|
|
}
|
|
|
|
|
|
|
|
func _getByField(field string, value string) (accounts.AccountInfoResponse, error) {
|
|
|
|
defer resetRepeatsCount()
|
|
|
|
|
|
|
|
apiToken, err := getToken()
|
|
|
|
if err != nil {
|
|
|
|
return accounts.AccountInfoResponse{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err := apiToken.AccountInfo(field, value)
|
|
|
|
if err != nil {
|
|
|
|
_, ok := err.(*accounts.UnauthorizedResponse)
|
|
|
|
if !ok || repeatsCount >= repeatsLimit {
|
|
|
|
return accounts.AccountInfoResponse{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
repeatsCount++
|
|
|
|
token = nil
|
|
|
|
|
|
|
|
return _getByField(field, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getToken() (*accounts.Token, error) {
|
|
|
|
if token == nil {
|
2017-04-10 22:58:47 +05:30
|
|
|
tempToken, err := accounts.GetToken(*AccountsTokenConfig)
|
2017-04-10 17:23:26 +05:30
|
|
|
if err != nil {
|
|
|
|
return &accounts.Token{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
token = &tempToken
|
|
|
|
}
|
|
|
|
|
|
|
|
return token, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resetRepeatsCount() {
|
|
|
|
repeatsCount = 0
|
|
|
|
}
|