Deretardify the caching
This commit is contained in:
133
esi/client.go
133
esi/client.go
@@ -20,69 +20,6 @@ const (
|
||||
ESIBaseURL = "https://esi.evetech.net"
|
||||
)
|
||||
|
||||
type Planet struct {
|
||||
PlanetID int `json:"planet_id"`
|
||||
PlanetType string `json:"planet_type"`
|
||||
SolarSystemID int `json:"solar_system_id"`
|
||||
UpgradeLevel int `json:"upgrade_level"`
|
||||
NumPins int `json:"num_pins"`
|
||||
LastUpdate string `json:"last_update"`
|
||||
OwnerID int `json:"owner_id"`
|
||||
PlanetName string `json:"planet_name"`
|
||||
PlanetTypeID int `json:"planet_type_id"`
|
||||
Position struct {
|
||||
X float64 `json:"x"`
|
||||
Y float64 `json:"y"`
|
||||
Z float64 `json:"z"`
|
||||
} `json:"position"`
|
||||
}
|
||||
|
||||
type PlanetDetail struct {
|
||||
Links []Link `json:"links"`
|
||||
Pins []Pin `json:"pins"`
|
||||
Routes []Route `json:"routes"`
|
||||
LastUpdate string `json:"last_update"`
|
||||
}
|
||||
|
||||
type Link struct {
|
||||
DestinationPinID int `json:"destination_pin_id"`
|
||||
LinkLevel int `json:"link_level"`
|
||||
SourcePinID int `json:"source_pin_id"`
|
||||
}
|
||||
|
||||
type Pin struct {
|
||||
Contents []StorageContent `json:"contents"`
|
||||
ExpiryTime *string `json:"expiry_time"`
|
||||
ExtractorDetails *ExtractorDetails `json:"extractor_details"`
|
||||
FactoryDetails *FactoryDetails `json:"factory_details"`
|
||||
InstallTime *string `json:"install_time"`
|
||||
LastCycleStart *string `json:"last_cycle_start"`
|
||||
Latitude float64 `json:"latitude"`
|
||||
Longitude float64 `json:"longitude"`
|
||||
PinID int `json:"pin_id"`
|
||||
SchematicID *int `json:"schematic_id"`
|
||||
TypeID int `json:"type_id"`
|
||||
}
|
||||
|
||||
type StorageContent struct {
|
||||
Amount int `json:"amount"`
|
||||
TypeID int `json:"type_id"`
|
||||
}
|
||||
|
||||
type ExtractorDetails struct {
|
||||
CycleTime *int `json:"cycle_time"`
|
||||
HeadRadius *float64 `json:"head_radius"`
|
||||
Heads []Head `json:"heads"`
|
||||
ProductTypeID *int `json:"product_type_id"`
|
||||
QtyPerCycle *int `json:"qty_per_cycle"`
|
||||
}
|
||||
|
||||
type Head struct {
|
||||
HeadID int `json:"head_id"`
|
||||
Latitude float64 `json:"latitude"`
|
||||
Longitude float64 `json:"longitude"`
|
||||
}
|
||||
|
||||
type PlanetName struct {
|
||||
Name string `json:"name"`
|
||||
PlanetID int `json:"planet_id"`
|
||||
@@ -99,20 +36,11 @@ type FactoryDetails struct {
|
||||
SchematicID int `json:"schematic_id"`
|
||||
}
|
||||
|
||||
type Route struct {
|
||||
ContentTypeID int `json:"content_type_id"`
|
||||
DestinationPinID int `json:"destination_pin_id"`
|
||||
Quantity float64 `json:"quantity"`
|
||||
RouteID int `json:"route_id"`
|
||||
SourcePinID int `json:"source_pin_id"`
|
||||
Waypoints []int `json:"waypoints"`
|
||||
}
|
||||
|
||||
// ESIInterface defines the contract for ESI API interactions
|
||||
type ESIInterface interface {
|
||||
GetCharacterPlanets(ctx context.Context, characterID int, accessToken string) ([]Planet, error)
|
||||
GetPlanetDetails(ctx context.Context, characterID, planetID int, accessToken string) (*PlanetDetail, error)
|
||||
GetPlanetName(ctx context.Context, planetID int) (*PlanetName, error)
|
||||
GetPlanetPI(ctx context.Context, characterID int, planetID int64, accessToken string) (*PlanetPI, error)
|
||||
GetPlanetName(ctx context.Context, planetID int64) (*PlanetName, error)
|
||||
}
|
||||
|
||||
// DirectESI implements ESIInterface with direct API calls
|
||||
@@ -138,13 +66,14 @@ func NewDirectESI() *DirectESI {
|
||||
|
||||
// GetCharacterPlanets retrieves a list of planets for a character
|
||||
func (d *DirectESI) GetCharacterPlanets(ctx context.Context, characterID int, accessToken string) ([]Planet, error) {
|
||||
logger.Debug("Fetching planets for character ID %d", characterID)
|
||||
funclog := logger.Default.WithPrefix("DirectESI.GetCharacterPlanets").WithPrefix(fmt.Sprintf("characterID=%d", characterID))
|
||||
funclog.Debug("Fetching planets")
|
||||
|
||||
url := fmt.Sprintf("%s/v1/characters/%d/planets/", ESIBaseURL, characterID)
|
||||
url := fmt.Sprintf("%s/v3/characters/%d/planets/", ESIBaseURL, characterID)
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
logger.Error("Failed to create request for character planets: %v", err)
|
||||
funclog.Error("Failed to create request for character planets: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -153,36 +82,37 @@ func (d *DirectESI) GetCharacterPlanets(ctx context.Context, characterID int, ac
|
||||
|
||||
resp, err := d.httpClient.Do(req)
|
||||
if err != nil {
|
||||
logger.Error("Failed to fetch character planets: %v", err)
|
||||
funclog.Error("Failed to fetch character planets: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
logger.Error("Character planets API returned status %d: %s", resp.StatusCode, string(body))
|
||||
funclog.Error("Character planets API returned status %d: %s", resp.StatusCode, string(body))
|
||||
return nil, fmt.Errorf("API request failed with status %d: %s", resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
var planets []Planet
|
||||
if err := json.NewDecoder(resp.Body).Decode(&planets); err != nil {
|
||||
logger.Error("Failed to decode character planets response: %v", err)
|
||||
funclog.Error("Failed to decode character planets response: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
logger.Info("Successfully fetched %d planets for character ID %d", len(planets), characterID)
|
||||
funclog.Info("Successfully fetched %d planets", len(planets))
|
||||
return planets, nil
|
||||
}
|
||||
|
||||
// GetPlanetDetails retrieves detailed information about a specific planet
|
||||
func (d *DirectESI) GetPlanetDetails(ctx context.Context, characterID, planetID int, accessToken string) (*PlanetDetail, error) {
|
||||
logger.Debug("Fetching planet details for character ID %d, planet ID %d", characterID, planetID)
|
||||
// GetPlanetPI retrieves detailed information about a specific planet
|
||||
func (d *DirectESI) GetPlanetPI(ctx context.Context, characterID int, planetID int64, accessToken string) (*PlanetPI, error) {
|
||||
funclog := logger.Default.WithPrefix("DirectESI.GetPlanetPI").WithPrefix(fmt.Sprintf("characterID=%d", characterID)).WithPrefix(fmt.Sprintf("planetID=%d", planetID))
|
||||
funclog.Debug("Fetching planet details")
|
||||
|
||||
url := fmt.Sprintf("%s/v3/characters/%d/planets/%d/", ESIBaseURL, characterID, planetID)
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
logger.Error("Failed to create request for planet details: %v", err)
|
||||
funclog.Error("Failed to create request for planet details: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -191,36 +121,37 @@ func (d *DirectESI) GetPlanetDetails(ctx context.Context, characterID, planetID
|
||||
|
||||
resp, err := d.httpClient.Do(req)
|
||||
if err != nil {
|
||||
logger.Error("Failed to fetch planet details: %v", err)
|
||||
funclog.Error("Failed to fetch planet details: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
logger.Error("Planet details API returned status %d: %s", resp.StatusCode, string(body))
|
||||
funclog.Error("Planet details API returned status %d: %s", resp.StatusCode, string(body))
|
||||
return nil, fmt.Errorf("API request failed with status %d: %s", resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
var planetDetail PlanetDetail
|
||||
if err := json.NewDecoder(resp.Body).Decode(&planetDetail); err != nil {
|
||||
logger.Error("Failed to decode planet details response: %v", err)
|
||||
var pi PlanetPI
|
||||
if err := json.NewDecoder(resp.Body).Decode(&pi); err != nil {
|
||||
funclog.Error("Failed to decode planet details response: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
logger.Info("Successfully fetched planet details for character ID %d, planet ID %d", characterID, planetID)
|
||||
return &planetDetail, nil
|
||||
funclog.Info("Successfully fetched planet details")
|
||||
return &pi, nil
|
||||
}
|
||||
|
||||
// GetPlanetName retrieves planet name from universe endpoint
|
||||
func (d *DirectESI) GetPlanetName(ctx context.Context, planetID int) (*PlanetName, error) {
|
||||
logger.Debug("Fetching planet name for planet ID %d", planetID)
|
||||
func (d *DirectESI) GetPlanetName(ctx context.Context, planetID int64) (*PlanetName, error) {
|
||||
funclog := logger.Default.WithPrefix("DirectESI.GetPlanetName").WithPrefix(fmt.Sprintf("planetID=%d", planetID))
|
||||
funclog.Debug("Fetching planet name")
|
||||
|
||||
url := fmt.Sprintf("%s/v1/universe/planets/%d/", ESIBaseURL, planetID)
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
logger.Error("Failed to create request for planet name: %v", err)
|
||||
funclog.Error("Failed to create request for planet name: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -228,28 +159,30 @@ func (d *DirectESI) GetPlanetName(ctx context.Context, planetID int) (*PlanetNam
|
||||
|
||||
resp, err := d.httpClient.Do(req)
|
||||
if err != nil {
|
||||
logger.Error("Failed to fetch planet name: %v", err)
|
||||
funclog.Error("Failed to fetch planet name: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
logger.Error("Failed to read planet name response body: %v", err)
|
||||
funclog.Error("Failed to read planet name response body: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
logger.Error("API request failed with status %d: %s", resp.StatusCode, string(body))
|
||||
funclog.Error("API request failed with status %d: %s", resp.StatusCode, string(body))
|
||||
return nil, fmt.Errorf("API request failed with status %d: %s", resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
var planetName PlanetName
|
||||
if err := json.NewDecoder(bytes.NewReader(body)).Decode(&planetName); err != nil {
|
||||
logger.Error("Failed to decode planet name response: %v", err)
|
||||
funclog.Error("Failed to decode planet name response: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
logger.Info("Successfully fetched planet name for planet ID %d: %s", planetID, planetName.Name)
|
||||
funclog.Info("Successfully fetched planet name: %s", planetName.Name)
|
||||
return &planetName, nil
|
||||
}
|
||||
|
||||
var _ ESIInterface = &DirectESI{}
|
||||
Reference in New Issue
Block a user