Skip to content

Commit

Permalink
add news API (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
xonvanetta authored Sep 30, 2021
1 parent 9df90e4 commit ec58273
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
2 changes: 2 additions & 0 deletions pkg/tibiadata/v2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type Client interface {
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 {
Expand Down
25 changes: 25 additions & 0 deletions pkg/tibiadata/v2/news.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package v2

import (
"context"
"fmt"
)

type NewsResponse struct {
News News `json:"news"`
Information *Information `json:"information"`
}

type News struct {
ID int `json:"id"`
Title string `json:"title"`
Content string `json:"content"`
Date *Timezone `json:"date"`
}

func (c client) News(context context.Context, newsId int) (*NewsResponse, error) {
newsResponse := &NewsResponse{}
url := tibiaDataURL(fmt.Sprintf("news/%d.json", newsId))
err := c.client.Get(context, url, newsResponse)
return newsResponse, err
}
21 changes: 20 additions & 1 deletion test/v2/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,26 @@ func TestAnticaHighscore(t *testing.T) {
v2.URL = "https://api.tibiadata.com/v2/"
client := v2.NewClient()

_, err := client.Highscore(context.Background(), "Antica", "", "")
categories := []string{"experience", "magic", "shielding", "distance", "sword", "club", "axe", "fist", "fishing", "achievements", "charmpoints", "goshnarstaint", "loyalty"}

for _, category := range categories {
_, err := client.Highscore(context.Background(), "Antica", category, "")
if err != nil {
assert.NoError(t, err)
panic(err)
}
}
}

func TestNews(t *testing.T) {
if testing.Short() {
t.Skip("skipping test, Really long test is not verified yet. JSON unmarshal error guaranteed.")
}

v2.URL = "https://api.tibiadata.com/v2/"
client := v2.NewClient()

_, err := client.News(context.Background(), 6000)
if err != nil {
assert.NoError(t, err)
panic(err)
Expand Down

0 comments on commit ec58273

Please sign in to comment.