68 lines
1.8 KiB
Go
68 lines
1.8 KiB
Go
|
package mcfetch
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"errors"
|
||
|
"net/http"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
// AsyncPlayerFetcher is responsible for fetching Minecraft player data asynchronously
|
||
|
type AsyncPlayerFetcher struct {
|
||
|
playerName string
|
||
|
retries int
|
||
|
retryDelay time.Duration
|
||
|
timeout time.Duration
|
||
|
cache ICache
|
||
|
}
|
||
|
|
||
|
// NewAsyncPlayerFetcher creates a new AsyncPlayerFetcher with an abstract cache (ICache)
|
||
|
func NewAsyncPlayerFetcher(playerName string, cache ICache, retries int, retryDelay time.Duration, timeout time.Duration) *AsyncPlayerFetcher {
|
||
|
cache.Init()
|
||
|
return &AsyncPlayerFetcher{
|
||
|
playerName: playerName,
|
||
|
retries: retries,
|
||
|
retryDelay: retryDelay,
|
||
|
timeout: timeout,
|
||
|
cache: cache,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// FetchPlayerData fetches the player data asynchronously
|
||
|
func (pf *AsyncPlayerFetcher) FetchPlayerData(resultChan chan map[string]interface{}, errorChan chan error) {
|
||
|
go func() {
|
||
|
cachedData, found := pf.cache.Get(pf.playerName)
|
||
|
if found {
|
||
|
resultChan <- cachedData
|
||
|
return
|
||
|
}
|
||
|
|
||
|
var data map[string]interface{}
|
||
|
for i := 0; i < pf.retries; i++ {
|
||
|
resp, err := pf.makeRequest(pf.playerName)
|
||
|
if err == nil {
|
||
|
defer resp.Body.Close()
|
||
|
if err := json.NewDecoder(resp.Body).Decode(&data); err == nil {
|
||
|
pf.cache.Set(pf.playerName, data)
|
||
|
pf.cache.Sync()
|
||
|
resultChan <- data
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
time.Sleep(pf.retryDelay)
|
||
|
}
|
||
|
errorChan <- errors.New("Failed to fetch player data after retries")
|
||
|
}()
|
||
|
}
|
||
|
|
||
|
// makeRequest performs the HTTP request to Mojang API
|
||
|
func (pf *AsyncPlayerFetcher) makeRequest(playerName string) (*http.Response, error) {
|
||
|
client := http.Client{Timeout: pf.timeout}
|
||
|
url := "https://api.mojang.com/users/profiles/minecraft/" + playerName
|
||
|
resp, err := client.Get(url)
|
||
|
if err != nil || resp.StatusCode != http.StatusOK {
|
||
|
return nil, errors.New("Request failed")
|
||
|
}
|
||
|
return resp, nil
|
||
|
}
|