From 90e245ca3de5c67c57adbce697d9296613bdeafc Mon Sep 17 00:00:00 2001 From: Joshua Raphael Date: Fri, 1 Nov 2024 10:15:52 -0700 Subject: [PATCH] refactor optional parameters for getachievementsearnedbetween --- README.md | 2 +- .../getachievementsearnedbetween.go | 7 +- models/user.go | 70 ++++++++++++++++++- user.go | 10 +-- user_test.go | 60 ++++++++-------- 5 files changed, 110 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index a02cda2..2e8410b 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ For convenience, the API docs and examples can be found in the tables below |-|-|-| |`GetUserProfile()`|Get a user's basic profile information.|[docs](https://api-docs.retroachievements.org/v1/get-user-profile.html) \| [example](examples/user/getuserprofile/getuserprofile.go)| |`GetUserRecentAchievements()`|Get a list of achievements recently earned by the user.|[docs](https://api-docs.retroachievements.org/v1/get-user-recent-achievements.html) \| [example](examples/user/getuserrecentachievements/getuserrecentachievements.go)| -|`GetAchievementsEarnedBetween(string,Time,Time)`|Get a list of achievements earned by a user between two dates.|[docs](https://api-docs.retroachievements.org/v1/get-achievements-earned-between.html) \| [example](examples/user/getachievementsearnedbetween/getachievementsearnedbetween.go)| +|`GetAchievementsEarnedBetween()`|Get a list of achievements earned by a user between two dates.|[docs](https://api-docs.retroachievements.org/v1/get-achievements-earned-between.html) \| [example](examples/user/getachievementsearnedbetween/getachievementsearnedbetween.go)| |`GetAchievementsEarnedOnDay(string,Time)`|Get a list of achievements earned by a user on a given date.|[docs](https://api-docs.retroachievements.org/v1/get-achievements-earned-on-day.html) \| [example](examples/user/getachievementsearnedonday/getachievementsearnedonday.go)| |`GetGameInfoAndUserProgress(string,int,bool)`|Get metadata about a game as well as a user's progress on that game.|[docs](https://api-docs.retroachievements.org/v1/get-game-info-and-user-progress.html) \| [example](examples/user/getgameinfoanduserprogress/getgameinfoanduserprogress.go)| |`GetUserCompletionProgress(string)`|Get metadata about all the user's played games and any awards associated with them.|[docs](https://api-docs.retroachievements.org/v1/get-user-completion-progress.html) \| [example](examples/user/getusercompletionprogress/getusercompletionprogress.go)| diff --git a/examples/user/getachievementsearnedbetween/getachievementsearnedbetween.go b/examples/user/getachievementsearnedbetween/getachievementsearnedbetween.go index 45d4342..d25738f 100644 --- a/examples/user/getachievementsearnedbetween/getachievementsearnedbetween.go +++ b/examples/user/getachievementsearnedbetween/getachievementsearnedbetween.go @@ -7,6 +7,7 @@ import ( "time" "github.com/joshraphael/go-retroachievements" + "github.com/joshraphael/go-retroachievements/models" ) /* @@ -23,7 +24,11 @@ func main() { } later := now.Add(10 * time.Minute) - resp, err := client.GetAchievementsEarnedBetween("jamiras", now, later) + resp, err := client.GetAchievementsEarnedBetween(models.GetAchievementsEarnedBetweenParameters{ + Username: "jamiras", + From: now, + To: later, + }) if err != nil { panic(err) } diff --git a/models/user.go b/models/user.go index d63298e..279b78b 100644 --- a/models/user.go +++ b/models/user.go @@ -1,8 +1,10 @@ package models +import "time" + // GetUserProfileParameters contains the parameters needed for getting a users profile type GetUserProfileParameters struct { - // The username of the profile + // The target username Username string } @@ -56,10 +58,10 @@ type GetUserProfile struct { // GetUserRecentAchievementsParameters contains the parameters needed for getting a users recent achievements type GetUserRecentAchievementsParameters struct { - // The username of the profile + // The target username Username string - // [Optional] Minutes to lookback (Default 60) + // [Optional] Minutes to look back (Default 60) LookbackMinutes *int } @@ -113,3 +115,65 @@ type GetUserRecentAchievements struct { // URL resource to the game page GameURL string `json:"GameURL"` } + +// GetAchievementsEarnedBetweenParameters contains the parameters needed for getting a users achievements earned between two dates +type GetAchievementsEarnedBetweenParameters struct { + // The target username + Username string + + // Time range start + From time.Time + + // Time range end + To time.Time +} + +type GetAchievementsEarnedBetween struct { + // Title of the achievement + Title string `json:"Title"` + + // Description of the achievement + Description string `json:"Description"` + + // Points awarded + Points int `json:"Points"` + + // Ratio of points the achievemnet is worth + TrueRatio int `json:"TrueRatio"` + + // Username of the author of the achievement + Author string `json:"Author"` + + // The date the achievement was unlocked + Date DateTime `json:"Date"` + + // Mode the achievement was unlocked in: 1 if in hardcore mode, 0 if not + HardcoreMode int `json:"HardcoreMode"` + + // The ID of the achievement + AchievementID int `json:"AchievementID"` + + // Name of the padge resource + BadgeName string `json:"BadgeName"` + + // Type of achievement (standard, missable, progression, win_condition) + Type string `json:"Type"` + + // Title of the game + GameTitle string `json:"GameTitle"` + + // URL resource of the game icon + GameIcon string `json:"GameIcon"` + + // ID of the game + GameID int `json:"GameID"` + + // Common name of the console + ConsoleName string `json:"ConsoleName"` + + // URL resource to the image used for the achievment badge + BadgeURL string `json:"BadgeURL"` + + // URL resource to the game page + GameURL string `json:"GameURL"` +} diff --git a/user.go b/user.go index c01f013..b2bdb28 100644 --- a/user.go +++ b/user.go @@ -50,19 +50,19 @@ func (c *Client) GetUserRecentAchievements(params models.GetUserRecentAchievemen } // GetAchievementsEarnedBetween get a list of achievements earned by a user between two dates. -func (c *Client) GetAchievementsEarnedBetween(username string, from time.Time, to time.Time) ([]models.UnlockedAchievement, error) { +func (c *Client) GetAchievementsEarnedBetween(params models.GetAchievementsEarnedBetweenParameters) ([]models.GetAchievementsEarnedBetween, error) { resp, err := c.do( raHttp.Method(http.MethodGet), raHttp.Path("/API/API_GetAchievementsEarnedBetween.php"), raHttp.APIToken(c.Secret), - raHttp.Username(username), - raHttp.FromTime(from), - raHttp.ToTime(to), + raHttp.Username(params.Username), + raHttp.FromTime(params.From), + raHttp.ToTime(params.To), ) if err != nil { return nil, fmt.Errorf("calling endpoint: %w", err) } - achievements, err := raHttp.ResponseList[models.UnlockedAchievement](resp) + achievements, err := raHttp.ResponseList[models.GetAchievementsEarnedBetween](resp) if err != nil { return nil, fmt.Errorf("parsing response list: %w", err) } diff --git a/user_test.go b/user_test.go index e989d55..7912b1d 100644 --- a/user_test.go +++ b/user_test.go @@ -319,21 +319,21 @@ func TestGetAchievementsEarnedBetween(tt *testing.T) { later := now.Add(10 * time.Minute) tests := []struct { name string - username string - fromTime time.Time - toTime time.Time + params models.GetAchievementsEarnedBetweenParameters modifyURL func(url string) string responseCode int - responseMessage []models.UnlockedAchievement + responseMessage []models.GetAchievementsEarnedBetween responseError models.ErrorResponse response func(messageBytes []byte, errorBytes []byte) []byte - assert func(t *testing.T, achievements []models.UnlockedAchievement, err error) + assert func(t *testing.T, achievements []models.GetAchievementsEarnedBetween, err error) }{ { - name: "fail to call endpoint", - username: "Test", - fromTime: now, - toTime: later, + name: "fail to call endpoint", + params: models.GetAchievementsEarnedBetweenParameters{ + Username: "Test", + From: now, + To: later, + }, modifyURL: func(url string) string { return "" }, @@ -351,16 +351,18 @@ func TestGetAchievementsEarnedBetween(tt *testing.T) { response: func(messageBytes []byte, errorBytes []byte) []byte { return errorBytes }, - assert: func(t *testing.T, achievements []models.UnlockedAchievement, err error) { + assert: func(t *testing.T, achievements []models.GetAchievementsEarnedBetween, err error) { require.Nil(t, achievements) require.EqualError(t, err, "calling endpoint: Get \"/API/API_GetAchievementsEarnedBetween.php?f=1709400423&t=1709401023&u=Test&y=some_secret\": unsupported protocol scheme \"\"") }, }, { - name: "error response", - username: "Test", - fromTime: now, - toTime: later, + name: "error response", + params: models.GetAchievementsEarnedBetweenParameters{ + Username: "Test", + From: now, + To: later, + }, modifyURL: func(url string) string { return url }, @@ -378,29 +380,29 @@ func TestGetAchievementsEarnedBetween(tt *testing.T) { response: func(messageBytes []byte, errorBytes []byte) []byte { return errorBytes }, - assert: func(t *testing.T, achievements []models.UnlockedAchievement, err error) { + assert: func(t *testing.T, achievements []models.GetAchievementsEarnedBetween, err error) { require.Nil(t, achievements) require.EqualError(t, err, "parsing response list: error responses: [401] Not Authorized") }, }, { - name: "success", - username: "Test", - fromTime: now, - toTime: later, + name: "success", + params: models.GetAchievementsEarnedBetweenParameters{ + Username: "Test", + From: now, + To: later, + }, modifyURL: func(url string) string { return url }, responseCode: http.StatusOK, - responseMessage: []models.UnlockedAchievement{ + responseMessage: []models.GetAchievementsEarnedBetween{ { - Achievement: models.Achievement{ - Title: "Beat Level 1", - Description: "Finish level 1", - Points: 10, - TrueRatio: 234, - Author: "jamiras", - }, + Title: "Beat Level 1", + Description: "Finish level 1", + Points: 10, + TrueRatio: 234, + Author: "jamiras", Date: models.DateTime{ Time: now, }, @@ -419,7 +421,7 @@ func TestGetAchievementsEarnedBetween(tt *testing.T) { response: func(messageBytes []byte, errorBytes []byte) []byte { return messageBytes }, - assert: func(t *testing.T, achievements []models.UnlockedAchievement, err error) { + assert: func(t *testing.T, achievements []models.GetAchievementsEarnedBetween, err error) { require.NotNil(t, achievements) require.Len(t, achievements, 1) require.Equal(t, models.DateTime{ @@ -464,7 +466,7 @@ func TestGetAchievementsEarnedBetween(tt *testing.T) { defer server.Close() client := retroachievements.New(test.modifyURL(server.URL), "some_secret") - achievements, err := client.GetAchievementsEarnedBetween(test.username, test.fromTime, test.toTime) + achievements, err := client.GetAchievementsEarnedBetween(test.params) test.assert(t, achievements, err) }) }