Skip to content

Commit

Permalink
add GetConsoleIDs endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
joshraphael committed Nov 22, 2024
1 parent def559c commit 622eecc
Show file tree
Hide file tree
Showing 13 changed files with 344 additions and 118 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,10 @@ For convenience, the API docs and examples can be found in the tables below
|Function|Description|Links|
|-|-|-|
|`GetGameLeaderboards()`|Gets a given games's list of leaderboards.|[docs](https://api-docs.retroachievements.org/v1/get-game-leaderboards.html) \| [example](examples/leaderboards/getgameleaderboards/getgameleaderboards.go)|
|`GetLeaderboardEntries()`|Gets a given leadboard's entires|[docs](https://api-docs.retroachievements.org/v1/get-leaderboard-entries.html) \| [example](examples/leaderboards/getleaderboardentries/getleaderboardentries.go)|
|`GetLeaderboardEntries()`|Gets a given leadboard's entries|[docs](https://api-docs.retroachievements.org/v1/get-leaderboard-entries.html) \| [example](examples/leaderboards/getleaderboardentries/getleaderboardentries.go)|

<h3>System</h3>

|Function|Description|Links|
|-|-|-|
|`GetConsoleIDs()`|Gets the complete list of all system ID and name pairs on the site.|[docs](https://api-docs.retroachievements.org/v1/get-console-ids.html) \| [example](examples/system/getconsoleids/getconsoleids.go)|
26 changes: 26 additions & 0 deletions examples/system/getconsoleids/getconsoleids.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Package getconsoleids provides an example for getting the complete list of all system ID and name pairs on the site.
package main

import (
"fmt"
"os"

"github.com/joshraphael/go-retroachievements"
"github.com/joshraphael/go-retroachievements/models"
)

/*
Test script, add RA_API_KEY to your env and use `go run getconsoleids.go`
*/
func main() {
secret := os.Getenv("RA_API_KEY")

client := retroachievements.NewClient(secret)

resp, err := client.GetConsoleIDs(models.GetConsoleIDsParameters{})
if err != nil {
panic(err)
}

fmt.Printf("%+v\n", resp)
}
38 changes: 23 additions & 15 deletions game.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func (c *Client) GetGame(params models.GetGameParameters) (*models.GetGame, erro
raHttp.Method(http.MethodGet),
raHttp.Path("/API/API_GetGame.php"),
raHttp.APIToken(c.Secret),
raHttp.IDs([]int{params.GameID}),
raHttp.I([]int{params.GameID}),
)
if err != nil {
return nil, fmt.Errorf("calling endpoint: %w", err)
Expand All @@ -32,10 +32,14 @@ func (c *Client) GetGameExtended(params models.GetGameExtentedParameters) (*mode
raHttp.Method(http.MethodGet),
raHttp.Path("/API/API_GetGameExtended.php"),
raHttp.APIToken(c.Secret),
raHttp.IDs([]int{params.GameID}),
raHttp.I([]int{params.GameID}),
}
if params.Unofficial {
details = append(details, raHttp.From(int64(5)))
if params.Unofficial != nil {
f := 3
if *params.Unofficial {
f = 5
}
details = append(details, raHttp.F(f))
}
r, err := c.do(details...)
if err != nil {
Expand All @@ -54,7 +58,7 @@ func (c *Client) GetGameHashes(params models.GetGameHashesParameters) (*models.G
raHttp.Method(http.MethodGet),
raHttp.Path("/API/API_GetGameHashes.php"),
raHttp.APIToken(c.Secret),
raHttp.IDs([]int{params.GameID}),
raHttp.I([]int{params.GameID}),
)
if err != nil {
return nil, fmt.Errorf("calling endpoint: %w", err)
Expand All @@ -72,7 +76,7 @@ func (c *Client) GetAchievementCount(params models.GetAchievementCountParameters
raHttp.Method(http.MethodGet),
raHttp.Path("/API/API_GetAchievementCount.php"),
raHttp.APIToken(c.Secret),
raHttp.IDs([]int{params.GameID}),
raHttp.I([]int{params.GameID}),
)
if err != nil {
return nil, fmt.Errorf("calling endpoint: %w", err)
Expand All @@ -90,17 +94,21 @@ func (c *Client) GetAchievementDistribution(params models.GetAchievementDistribu
raHttp.Method(http.MethodGet),
raHttp.Path("/API/API_GetAchievementDistribution.php"),
raHttp.APIToken(c.Secret),
raHttp.IDs([]int{params.GameID}),
raHttp.I([]int{params.GameID}),
}
if params.Unofficial != nil {
f := 3
if *params.Unofficial {
details = append(details, raHttp.From(int64(5)))
} else {
details = append(details, raHttp.From(int64(3)))
f = 5
}
details = append(details, raHttp.F(f))
}
if params.Hardcore != nil {
details = append(details, raHttp.Hardcore(*params.Hardcore))
h := 0
if *params.Hardcore {
h = 1
}
details = append(details, raHttp.H(h))
}
r, err := c.do(details...)
if err != nil {
Expand All @@ -119,14 +127,14 @@ func (c *Client) GetGameRankAndScore(params models.GetGameRankAndScoreParameters
raHttp.Method(http.MethodGet),
raHttp.Path("/API/API_GetGameRankAndScore.php"),
raHttp.APIToken(c.Secret),
raHttp.Game(params.GameID),
raHttp.G(params.GameID),
}
if params.LatestMasters != nil {
t := 0
if *params.LatestMasters {
details = append(details, raHttp.To(1))
} else {
details = append(details, raHttp.To(0))
t = 1
}
details = append(details, raHttp.T(t))
}
r, err := c.do(details...)
if err != nil {
Expand Down
8 changes: 2 additions & 6 deletions game_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ func TestGetGameExtended(tt *testing.T) {
require.NoError(tt, err)
created, err := time.Parse(time.DateTime, "2022-09-28 00:36:26")
require.NoError(tt, err)
unofficial := true
tests := []struct {
name string
params models.GetGameExtentedParameters
Expand All @@ -195,7 +196,7 @@ func TestGetGameExtended(tt *testing.T) {
name: "fail to call endpoint",
params: models.GetGameExtentedParameters{
GameID: 2991,
Unofficial: true,
Unofficial: &unofficial,
},
modifyURL: func(url string) string {
return ""
Expand Down Expand Up @@ -400,7 +401,6 @@ func TestGetGameExtended(tt *testing.T) {
require.Equal(t, num, len(resp))
}))
defer server.Close()

client := retroachievements.New(test.modifyURL(server.URL), "go-retroachievements/v0.0.0", "some_secret")
resp, err := client.GetGameExtended(test.params)
test.assert(t, resp, err)
Expand Down Expand Up @@ -521,7 +521,6 @@ func TestGetGameHashes(tt *testing.T) {
require.Equal(t, num, len(resp))
}))
defer server.Close()

client := retroachievements.New(test.modifyURL(server.URL), "go-retroachievements/v0.0.0", "some_secret")
resp, err := client.GetGameHashes(test.params)
test.assert(t, resp, err)
Expand Down Expand Up @@ -657,7 +656,6 @@ func TestGetAchievementCount(tt *testing.T) {
require.Equal(t, num, len(resp))
}))
defer server.Close()

client := retroachievements.New(test.modifyURL(server.URL), "go-retroachievements/v0.0.0", "some_secret")
resp, err := client.GetAchievementCount(test.params)
test.assert(t, resp, err)
Expand Down Expand Up @@ -828,7 +826,6 @@ func TestGetAchievementDistribution(tt *testing.T) {
require.Equal(t, num, len(resp))
}))
defer server.Close()

client := retroachievements.New(test.modifyURL(server.URL), "go-retroachievements/v0.0.0", "some_secret")
resp, err := client.GetAchievementDistribution(test.params)
test.assert(t, resp, err)
Expand Down Expand Up @@ -963,7 +960,6 @@ func TestGetGameRankAndScore(tt *testing.T) {
require.Equal(t, num, len(resp))
}))
defer server.Close()

client := retroachievements.New(test.modifyURL(server.URL), "go-retroachievements/v0.0.0", "some_secret")
resp, err := client.GetGameRankAndScore(test.params)
test.assert(t, resp, err)
Expand Down
70 changes: 33 additions & 37 deletions http/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,88 +60,84 @@ func UserAgent() RequestDetail {
})
}

// Username adds the username to the query parameters
func Username(username string) RequestDetail {
// M adds a 'u' string to the query parameters
func U(u string) RequestDetail {
return requestDetailFn(func(r *Request) {
r.Params["u"] = username
r.Params["u"] = u
})
}

// LookbackMinutes adds the lookback minutes to the query parameters
func LookbackMinutes(minutes int) RequestDetail {
// M adds a 'm' number to the query parameters
func M(m int) RequestDetail {
return requestDetailFn(func(r *Request) {
r.Params["m"] = strconv.Itoa(minutes)
r.Params["m"] = strconv.Itoa(m)
})
}

// From adds a start time to the query parameters in unix seconds
func From(t int64) RequestDetail {
// F adds a 'f' number to the query parameters
func F(f int) RequestDetail {
return requestDetailFn(func(r *Request) {
r.Params["f"] = strconv.Itoa(int(t))
r.Params["f"] = strconv.Itoa(f)
})
}

// To adds a end time to the query parameters in unix seconds
func To(t int64) RequestDetail {
// T adds a 't' number to the query parameters
func T(t int) RequestDetail {
return requestDetailFn(func(r *Request) {
r.Params["t"] = strconv.Itoa(int(t))
r.Params["t"] = strconv.Itoa(t)
})
}

// Date adds a string date (ignoring timestamp) to the query parameters
func Date(t time.Time) RequestDetail {
// D adds a 'd' date to the query parameters
func D(d time.Time) RequestDetail {
return requestDetailFn(func(r *Request) {
r.Params["d"] = t.UTC().Format(time.DateOnly)
r.Params["d"] = d.UTC().Format(time.DateOnly)
})
}

// IDs adds the target game ids to the query parameters
func IDs(ids []int) RequestDetail {
// I adds a 'i' number list to the query parameters
func I(is []int) RequestDetail {
var strIDs []string
for _, i := range ids {
for _, i := range is {
strIDs = append(strIDs, strconv.Itoa(i))
}
return requestDetailFn(func(r *Request) {
r.Params["i"] = strings.Join(strIDs, ",")
})
}

// Game adds a target game id to the query parameters
func Game(game int) RequestDetail {
// G adds a 'g' number to the query parameters
func G(g int) RequestDetail {
return requestDetailFn(func(r *Request) {
r.Params["g"] = strconv.Itoa(game)
r.Params["g"] = strconv.Itoa(g)
})
}

// Count adds a count to the query parameters
func Count(count int) RequestDetail {
// C adds a 'c' number to the query parameters
func C(c int) RequestDetail {
return requestDetailFn(func(r *Request) {
r.Params["c"] = strconv.Itoa(count)
r.Params["c"] = strconv.Itoa(c)
})
}

// Offset adds a offset to the query parameters
func Offset(offset int) RequestDetail {
// O adds a 'o' number to the query parameters
func O(o int) RequestDetail {
return requestDetailFn(func(r *Request) {
r.Params["o"] = strconv.Itoa(offset)
r.Params["o"] = strconv.Itoa(o)
})
}

// Achievement adds a achievement number query parameter
func Achievement(achievement int) RequestDetail {
// A adds a 'a' number to the query parameters
func A(a int) RequestDetail {
return requestDetailFn(func(r *Request) {
r.Params["a"] = strconv.Itoa(achievement)
r.Params["a"] = strconv.Itoa(a)
})
}

// Hardcore adds a Hardcore boolean query parameter
func Hardcore(hardcore bool) RequestDetail {
numHardcore := 0
if hardcore {
numHardcore = 1
}
// H adds a 'h' number to the query parameters
func H(h int) RequestDetail {
return requestDetailFn(func(r *Request) {
r.Params["h"] = strconv.Itoa(numHardcore)
r.Params["h"] = strconv.Itoa(h)
})
}

Expand Down
22 changes: 11 additions & 11 deletions http/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ func TestNewRequest(t *testing.T) {
raHttp.UserAgent(),
raHttp.APIToken("secret_token"),
raHttp.BearerToken("secret_bearer"),
raHttp.Username("myUsername"),
raHttp.LookbackMinutes(10),
raHttp.From(now.Unix()),
raHttp.To(later.Unix()),
raHttp.Date(now),
raHttp.IDs([]int{2837, 4535}),
raHttp.Game(345),
raHttp.Achievement(1),
raHttp.Count(20),
raHttp.Offset(34),
raHttp.Hardcore(true),
raHttp.U("myUsername"),
raHttp.M(10),
raHttp.F(int(now.Unix())),
raHttp.T(int(later.Unix())),
raHttp.D(now),
raHttp.I([]int{2837, 4535}),
raHttp.G(345),
raHttp.A(1),
raHttp.C(20),
raHttp.O(34),
raHttp.H(1),
)

expected := &raHttp.Request{
Expand Down
12 changes: 6 additions & 6 deletions leaderboards.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ func (c *Client) GetGameLeaderboards(params models.GetGameLeaderboardsParameters
raHttp.Method(http.MethodGet),
raHttp.Path("/API/API_GetGameLeaderboards.php"),
raHttp.APIToken(c.Secret),
raHttp.IDs([]int{params.GameID}),
raHttp.I([]int{params.GameID}),
}
if params.Count != nil {
details = append(details, raHttp.Count(*params.Count))
details = append(details, raHttp.C(*params.Count))
}
if params.Offset != nil {
details = append(details, raHttp.Offset(*params.Offset))
details = append(details, raHttp.O(*params.Offset))
}
r, err := c.do(details...)
if err != nil {
Expand All @@ -39,13 +39,13 @@ func (c *Client) GetLeaderboardEntries(params models.GetLeaderboardEntriesParame
raHttp.Method(http.MethodGet),
raHttp.Path("/API/API_GetLeaderboardEntries.php"),
raHttp.APIToken(c.Secret),
raHttp.IDs([]int{params.LeaderboardID}),
raHttp.I([]int{params.LeaderboardID}),
}
if params.Count != nil {
details = append(details, raHttp.Count(*params.Count))
details = append(details, raHttp.C(*params.Count))
}
if params.Offset != nil {
details = append(details, raHttp.Offset(*params.Offset))
details = append(details, raHttp.O(*params.Offset))
}
r, err := c.do(details...)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions models/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ type GetGameExtentedParameters struct {
// The target game ID
GameID int

// Get unofficial achievements
Unofficial bool
// [Optional] Get unofficial achievements (default: false)
Unofficial *bool
}

type GetGameExtented struct {
Expand Down
Loading

0 comments on commit 622eecc

Please sign in to comment.