Skip to content

Commit

Permalink
add v3 api for tibiadata (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
xonvanetta authored Mar 4, 2022
1 parent ec58273 commit 0eb04ad
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 0 deletions.
3 changes: 3 additions & 0 deletions internal/httpclient/httpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

var (
jsonUnmarshalTypeError *json.UnmarshalTypeError
UserAgent = "tibiadata/v3"
)

type Client struct {
Expand Down Expand Up @@ -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++ {
Expand Down
48 changes: 48 additions & 0 deletions pkg/tibiadata/v3/client.go
Original file line number Diff line number Diff line change
@@ -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"`
}
42 changes: 42 additions & 0 deletions pkg/tibiadata/v3/world.go
Original file line number Diff line number Diff line change
@@ -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
}
47 changes: 47 additions & 0 deletions pkg/tibiadata/v3/worlds.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 0eb04ad

Please sign in to comment.