From 2ffe416e044069a92c22ce51abe32d03c92c1cf9 Mon Sep 17 00:00:00 2001 From: Joshua Raphael Date: Fri, 1 Nov 2024 01:18:40 -0700 Subject: [PATCH] add GetUserRecentlyPlayedGames endpoint --- README.md | 1 + .../getuserrecentlyplayedgames.go | 25 +++++++++++++++ http/request.go | 14 +++++++++ http/request_test.go | 4 +++ models/progress.go | 14 +++++++++ user.go | 31 +++++++++++++++++++ 6 files changed, 89 insertions(+) create mode 100644 examples/user/getuserrecentlyplayedgames/getuserrecentlyplayedgames.go diff --git a/README.md b/README.md index 12856d3..443a04b 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,7 @@ For convenience, the API docs and examples can be found in the tables below |`GetUserGameRankAndScore(string,int)`|Get metadata about how a user has performed on a given game.|[docs](https://api-docs.retroachievements.org/v1/get-user-game-rank-and-score.html) \| [example](examples/user/getusergamerankandscore/getusergamerankandscore.go)| |`GetUserPoints(string)`|Get a user's total hardcore and softcore points.|[docs](https://api-docs.retroachievements.org/v1/get-user-points.html) \| [example](examples/user/getuserpoints/getuserpoints.go)| |`GetUserProgress(string,[]int)`|Get a user's progress on a list of specified games.|[docs](https://api-docs.retroachievements.org/v1/get-user-progress.html) \| [example](examples/user/getuserprogress/getuserprogress.go)| +|`GetUserRecentlyPlayedGames(string,int,int)`|Get a list of games a user has recently played.|[docs](https://api-docs.retroachievements.org/v1/get-user-recently-played-games.html) \| [example](examples/user/getuserrecentlyplayedgames/getuserrecentlyplayedgames.go)|

Game

diff --git a/examples/user/getuserrecentlyplayedgames/getuserrecentlyplayedgames.go b/examples/user/getuserrecentlyplayedgames/getuserrecentlyplayedgames.go new file mode 100644 index 0000000..63e8b58 --- /dev/null +++ b/examples/user/getuserrecentlyplayedgames/getuserrecentlyplayedgames.go @@ -0,0 +1,25 @@ +// Package getuserrecentachievements provides an example for a users achievements in the last X minutes +package main + +import ( + "fmt" + "os" + + "github.com/joshraphael/go-retroachievements" +) + +/* +Test script, add RA_API_KEY to your env and use `go run getuserrecentachievements.go` +*/ +func main() { + secret := os.Getenv("RA_API_KEY") + + client := retroachievements.NewClient(secret) + + resp, err := client.GetUserRecentlyPlayedGames("jamiras", 10, 0) + if err != nil { + panic(err) + } + + fmt.Printf("%+v\n", resp) +} diff --git a/http/request.go b/http/request.go index 23178f8..241b3f5 100644 --- a/http/request.go +++ b/http/request.go @@ -106,6 +106,20 @@ func GameID(gameId int) RequestDetail { }) } +// Count adds a count to the query parameters +func Count(count int) RequestDetail { + return requestDetailFn(func(r *Request) { + r.Params["c"] = strconv.Itoa(count) + }) +} + +// Offset adds a offset to the query parameters +func Offset(offset int) RequestDetail { + return requestDetailFn(func(r *Request) { + r.Params["o"] = strconv.Itoa(offset) + }) +} + // AwardMetadata adds a target game id to the query parameters func AwardMetadata(awardMetadata bool) RequestDetail { return requestDetailFn(func(r *Request) { diff --git a/http/request_test.go b/http/request_test.go index 7634dd3..8c55029 100644 --- a/http/request_test.go +++ b/http/request_test.go @@ -27,6 +27,8 @@ func TestNewRequest(t *testing.T) { raHttp.IDs([]int{2837, 4535}), raHttp.GameID(345), raHttp.AwardMetadata(true), + raHttp.Count(20), + raHttp.Offset(34), ) expected := &raHttp.Request{ @@ -46,6 +48,8 @@ func TestNewRequest(t *testing.T) { "i": "2837,4535", "g": "345", "a": "1", + "c": "20", + "o": "34", }, } diff --git a/models/progress.go b/models/progress.go index 9e81d91..c6aca1a 100644 --- a/models/progress.go +++ b/models/progress.go @@ -8,3 +8,17 @@ type Progress struct { NumAchievedHardcore int `json:"NumAchievedHardcore"` ScoreAchievedHardcore int `json:"ScoreAchievedHardcore"` } + +type UserRecentlyPlayed struct { + Progress + GameID int `json:"GameID"` + ConsoleID int `json:"ConsoleID"` + ConsoleName string `json:"ConsoleName"` + Title string `json:"Title"` + ImageIcon string `json:"ImageIcon"` + ImageTitle string `json:"ImageTitle"` + ImageIngame string `json:"ImageIngame"` + ImageBoxArt string `json:"ImageBoxArt"` + LastPlayed DateTime `json:"LastPlayed"` + AchievementsTotal int `json:"AchievementsTotal"` +} diff --git a/user.go b/user.go index 27afa0d..6d2f702 100644 --- a/user.go +++ b/user.go @@ -214,3 +214,34 @@ func (c *Client) GetUserProgress(username string, gameIDs []int) (map[string]mod } return *progress, nil } + +// GetUserRecentlyPlayedGames get a list of games a user has recently played. +func (c *Client) GetUserRecentlyPlayedGames(username string, count int, offset int) ([]models.UserRecentlyPlayed, error) { + numCount := count + if count <= 0 { + numCount = 10 + } + if count > 50 { + numCount = 50 + } + numOffset := offset + if offset < 0 { + numOffset = 0 + } + resp, err := c.do( + raHttp.Method(http.MethodGet), + raHttp.Path("/API/API_GetUserRecentlyPlayedGames.php"), + raHttp.APIToken(c.Secret), + raHttp.Username(username), + raHttp.Count(numCount), + raHttp.Offset(numOffset), + ) + if err != nil { + return nil, fmt.Errorf("calling endpoint: %w", err) + } + recentlyPlayed, err := raHttp.ResponseList[models.UserRecentlyPlayed](resp) + if err != nil { + return nil, fmt.Errorf("parsing response list: %w", err) + } + return recentlyPlayed, nil +}