Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update to TibiaData API v4 #12

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/httpclient/httpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

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

type Client struct {
Expand Down
58 changes: 58 additions & 0 deletions pkg/tibiadata/v4/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package v2

import (
"context"
"errors"
"fmt"

"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/v4/"
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 {
APIDetails APIDetails `json:"api"`
Timestamp string `json:"timestamp"`
Status Status `json:"status"`
}
type APIDetails struct {
Version int `json:"version"`
Release string `json:"release"`
Commit string `json:"commit"`
}
type Status struct {
HTTPCode int `json:"http_code"`
Error int `json:"error,omitempty"`
Message string `json:"message,omitempty"`
}
40 changes: 40 additions & 0 deletions pkg/tibiadata/v4/world.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package v2

import (
"context"
"fmt"
"time"
)

type WorldResponse 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"`
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/v4/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
}