diff --git a/internal/httpclient/httpclient.go b/internal/httpclient/httpclient.go index a79db96..7304ced 100644 --- a/internal/httpclient/httpclient.go +++ b/internal/httpclient/httpclient.go @@ -12,6 +12,7 @@ import ( var ( jsonUnmarshalTypeError *json.UnmarshalTypeError + UserAgent = "tibiadata/v3" ) type Client struct { @@ -41,6 +42,8 @@ func (c Client) Get(context context.Context, url string, v interface{}) error { return fmt.Errorf("failed to create request for url: %s, err: %w", url, err) } + request.Header.Set("user-agent", UserAgent) + var errs Errors for i := 0; i < c.retries; i++ { diff --git a/pkg/tibiadata/v3/client.go b/pkg/tibiadata/v3/client.go new file mode 100644 index 0000000..edae9f7 --- /dev/null +++ b/pkg/tibiadata/v3/client.go @@ -0,0 +1,48 @@ +package v2 + +import ( + "context" + "errors" + "fmt" + "time" + + "github.com/xonvanetta/tibiadata/internal/httpclient" +) + +type Client interface { + //Guild(ctx context.Context, name string) (*GuildResponse, error) + //Guilds(ctx context.Context, world string) (*GuildsResponse, error) + + World(ctx context.Context, name string) (*WorldResponse, error) + Worlds(ctx context.Context) (*WorldsResponse, error) + + //Character(ctx context.Context, name string) (*CharacterResponse, error) + // + //Highscore(ctx context.Context, world, category string, vocation tibia.Vocation) (*HighscoreResponse, error) + // + //News(context context.Context, newsId int) (*NewsResponse, error) +} + +type client struct { + client httpclient.Client +} + +var ( + URL = "https://api.tibiadata.com/v3/" + ErrNotFound = errors.New("tibiadata: not found") +) + +func NewClient() Client { + return client{ + client: httpclient.New(), + } +} + +func tibiaDataURL(path string) string { + return fmt.Sprintf("%s%s", URL, path) +} + +type Information struct { + APIVersion int `json:"api_version"` + Timestamp time.Time `json:"timestamp"` +} diff --git a/pkg/tibiadata/v3/world.go b/pkg/tibiadata/v3/world.go new file mode 100644 index 0000000..15e8ad1 --- /dev/null +++ b/pkg/tibiadata/v3/world.go @@ -0,0 +1,42 @@ +package v2 + +import ( + "context" + "fmt" + "time" +) + +type WorldResponse struct { + Worlds *struct { + World *struct { + Name string `json:"name"` + Status string `json:"status"` + PlayersOnline int `json:"players_online"` + RecordPlayers int `json:"record_players"` + RecordDate time.Time `json:"record_date"` + CreationDate string `json:"creation_date"` + Location string `json:"location"` + PvpType string `json:"pvp_type"` + PremiumOnly bool `json:"premium_only"` + TransferType string `json:"transfer_type"` + WorldQuestTitles []string `json:"world_quest_titles"` + BattleyeProtected bool `json:"battleye_protected"` + BattleyeDate string `json:"battleye_date"` + GameWorldType string `json:"game_world_type"` + TournamentWorldType string `json:"tournament_world_type"` + OnlinePlayers []*struct { + Name string `json:"name"` + Level int `json:"level"` + Vocation string `json:"vocation"` + } `json:"online_players"` + } `json:"world"` + } `json:"worlds"` + Information *Information `json:"information"` +} + +func (c client) World(context context.Context, name string) (*WorldResponse, error) { + worldResponse := &WorldResponse{} + url := tibiaDataURL(fmt.Sprintf("world/%s", name)) + err := c.client.Get(context, url, worldResponse) + return worldResponse, err +} diff --git a/pkg/tibiadata/v3/worlds.go b/pkg/tibiadata/v3/worlds.go new file mode 100644 index 0000000..fc4b4f4 --- /dev/null +++ b/pkg/tibiadata/v3/worlds.go @@ -0,0 +1,47 @@ +package v2 + +import ( + "context" + "time" +) + +type WorldsResponse struct { + Worlds *struct { + PlayersOnline int `json:"players_online"` + RecordPlayers int `json:"record_players"` + RecordDate time.Time `json:"record_date"` + RegularWorlds []*struct { + Name string `json:"name"` + Status string `json:"status"` + PlayersOnline int `json:"players_online"` + Location string `json:"location"` + PvpType string `json:"pvp_type"` + PremiumOnly bool `json:"premium_only"` + TransferType string `json:"transfer_type"` + BattleyeProtected bool `json:"battleye_protected"` + BattleyeDate string `json:"battleye_date"` + GameWorldType string `json:"game_world_type"` + TournamentWorldType string `json:"tournament_world_type"` + } `json:"regular_worlds"` + TournamentWorlds []*struct { + Name string `json:"name"` + Status string `json:"status"` + PlayersOnline int `json:"players_online"` + Location string `json:"location"` + PvpType string `json:"pvp_type"` + PremiumOnly bool `json:"premium_only"` + TransferType string `json:"transfer_type"` + BattleyeProtected bool `json:"battleye_protected"` + BattleyeDate string `json:"battleye_date"` + GameWorldType string `json:"game_world_type"` + TournamentWorldType string `json:"tournament_world_type"` + } `json:"tournament_worlds"` + } `json:"worlds"` + Information *Information `json:"information"` +} + +func (c client) Worlds(context context.Context) (*WorldsResponse, error) { + worldsResponse := &WorldsResponse{} + err := c.client.Get(context, tibiaDataURL("worlds"), worldsResponse) + return worldsResponse, err +}