Implement planet name resolution
This commit is contained in:
@@ -150,3 +150,23 @@ func getCachedResponse[T any](c *CachedESI, url string, fetchFunc func() (T, err
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// GetPlanetName retrieves planet name with caching
|
||||
func (c *CachedESI) GetPlanetName(ctx context.Context, planetID int) (*PlanetName, error) {
|
||||
url := fmt.Sprintf("/v1/universe/planets/%d/", planetID)
|
||||
logger.Info("CachedESI: Getting planet name for planet %d", planetID)
|
||||
|
||||
fetchFunc := func() (*PlanetName, error) {
|
||||
logger.Debug("CachedESI: Calling direct ESI for planet %d name", planetID)
|
||||
return c.direct.GetPlanetName(ctx, planetID)
|
||||
}
|
||||
|
||||
result, err := getCachedResponse(c, url, fetchFunc)
|
||||
if err != nil {
|
||||
logger.Error("CachedESI: Failed to get planet name for planet %d: %v", planetID, err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
logger.Info("CachedESI: Successfully retrieved planet name for planet %d: %s", planetID, result.Name)
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
package esi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
@@ -82,6 +83,18 @@ type Head struct {
|
||||
Longitude float64 `json:"longitude"`
|
||||
}
|
||||
|
||||
type PlanetName struct {
|
||||
Name string `json:"name"`
|
||||
PlanetID int `json:"planet_id"`
|
||||
SystemID int `json:"system_id"`
|
||||
TypeID int `json:"type_id"`
|
||||
Position struct {
|
||||
X float64 `json:"x"`
|
||||
Y float64 `json:"y"`
|
||||
Z float64 `json:"z"`
|
||||
} `json:"position"`
|
||||
}
|
||||
|
||||
type FactoryDetails struct {
|
||||
SchematicID int `json:"schematic_id"`
|
||||
}
|
||||
@@ -99,6 +112,7 @@ type Route struct {
|
||||
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)
|
||||
}
|
||||
|
||||
// DirectESI implements ESIInterface with direct API calls
|
||||
@@ -197,3 +211,45 @@ func (d *DirectESI) GetPlanetDetails(ctx context.Context, characterID, planetID
|
||||
logger.Info("Successfully fetched planet details for character ID %d, planet ID %d", characterID, planetID)
|
||||
return &planetDetail, 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)
|
||||
|
||||
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)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Set("Accept", "application/json")
|
||||
|
||||
resp, err := d.httpClient.Do(req)
|
||||
if err != nil {
|
||||
logger.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)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
logger.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)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
logger.Info("Successfully fetched planet name for planet ID %d: %s", planetID, planetName.Name)
|
||||
return &planetName, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user