Skip to content

Commit

Permalink
refactor optional parameters for getachievementsearnedbetween
Browse files Browse the repository at this point in the history
  • Loading branch information
joshraphael committed Nov 1, 2024
1 parent 4804967 commit 90e245c
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 39 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

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

/*
Expand All @@ -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)
}
Expand Down
70 changes: 67 additions & 3 deletions models/user.go
Original file line number Diff line number Diff line change
@@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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"`
}
10 changes: 5 additions & 5 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
60 changes: 31 additions & 29 deletions user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
},
Expand All @@ -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
},
Expand All @@ -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,
},
Expand All @@ -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{
Expand Down Expand Up @@ -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)
})
}
Expand Down

0 comments on commit 90e245c

Please sign in to comment.