Skip to content

Commit

Permalink
refactor some endpoints optional arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
joshraphael committed Nov 1, 2024
1 parent a6677f5 commit 4804967
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 32 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ For convenience, the API docs and examples can be found in the tables below

|Function|Description|Links|
|-|-|-|
|`GetUserProfile(string)`|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(string,int)`|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)|
|`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)|
|`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)|
Expand Down
5 changes: 4 additions & 1 deletion examples/user/getuserprofile/getuserprofile.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"

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

/*
Expand All @@ -16,7 +17,9 @@ func main() {

client := retroachievements.NewClient(secret)

resp, err := client.GetUserProfile("jamiras")
resp, err := client.GetUserProfile(models.GetUserProfileParameters{
Username: "Jamiras",
})
if err != nil {
panic(err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"

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

/*
Expand All @@ -16,7 +17,11 @@ func main() {

client := retroachievements.NewClient(secret)

resp, err := client.GetUserRecentAchievements("jamiras", 1000)
lookback := 1000
resp, err := client.GetUserRecentAchievements(models.GetUserRecentAchievementsParameters{
Username: "Jamiras",
LookbackMinutes: &lookback,
})
if err != nil {
panic(err)
}
Expand Down
16 changes: 16 additions & 0 deletions models/user.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package models

// GetUserProfileParameters contains the parameters needed for getting a users profile
type GetUserProfileParameters struct {
// The username of the profile
Username string
}

// GetUserProfile describes elements of a users profile
type GetUserProfile struct {
// Username of the profile
Expand Down Expand Up @@ -48,6 +54,16 @@ type GetUserProfile struct {
Motto string `json:"Motto"`
}

// GetUserRecentAchievementsParameters contains the parameters needed for getting a users recent achievements
type GetUserRecentAchievementsParameters struct {
// The username of the profile
Username string

// [Optional] Minutes to lookback (Default 60)
LookbackMinutes *int
}

// GetUserRecentAchievements describes elements of a users recent achievements
type GetUserRecentAchievements struct {
// Title of the achievement
Title string `json:"Title"`
Expand Down
17 changes: 10 additions & 7 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (
)

// GetUserProfile get a user's basic profile information.
func (c *Client) GetUserProfile(username string) (*models.GetUserProfile, error) {
func (c *Client) GetUserProfile(params models.GetUserProfileParameters) (*models.GetUserProfile, error) {
resp, err := c.do(
raHttp.Method(http.MethodGet),
raHttp.Path("/API/API_GetUserProfile.php"),
raHttp.APIToken(c.Secret),
raHttp.Username(username),
raHttp.Username(params.Username),
)
if err != nil {
return nil, fmt.Errorf("calling endpoint: %w", err)
Expand All @@ -28,14 +28,17 @@ func (c *Client) GetUserProfile(username string) (*models.GetUserProfile, error)
}

// GetUserRecentAchievements get a list of achievements recently earned by the user.
func (c *Client) GetUserRecentAchievements(username string, lookbackMinutes int) ([]models.GetUserRecentAchievements, error) {
resp, err := c.do(
func (c *Client) GetUserRecentAchievements(params models.GetUserRecentAchievementsParameters) ([]models.GetUserRecentAchievements, error) {
details := []raHttp.RequestDetail{
raHttp.Method(http.MethodGet),
raHttp.Path("/API/API_GetUserRecentAchievements.php"),
raHttp.APIToken(c.Secret),
raHttp.Username(username),
raHttp.LookbackMinutes(lookbackMinutes),
)
raHttp.Username(params.Username),
}
if params.LookbackMinutes != nil && *params.LookbackMinutes > 0 {
details = append(details, raHttp.LookbackMinutes(*params.LookbackMinutes))
}
resp, err := c.do(details...)
if err != nil {
return nil, fmt.Errorf("calling endpoint: %w", err)
}
Expand Down
52 changes: 31 additions & 21 deletions user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestGetUserProfile(tt *testing.T) {
require.NoError(tt, err)
tests := []struct {
name string
username string
params models.GetUserProfileParameters
modifyURL func(url string) string
responseCode int
responseMessage models.GetUserProfile
Expand All @@ -26,8 +26,10 @@ func TestGetUserProfile(tt *testing.T) {
assert func(t *testing.T, profile *models.GetUserProfile, err error)
}{
{
name: "fail to call endpoint",
username: "Test",
name: "fail to call endpoint",
params: models.GetUserProfileParameters{
Username: "Test",
},
modifyURL: func(url string) string {
return ""
},
Expand All @@ -51,8 +53,10 @@ func TestGetUserProfile(tt *testing.T) {
},
},
{
name: "error response",
username: "Test",
name: "error response",
params: models.GetUserProfileParameters{
Username: "Test",
},
modifyURL: func(url string) string {
return url
},
Expand All @@ -76,8 +80,10 @@ func TestGetUserProfile(tt *testing.T) {
},
},
{
name: "success",
username: "Test",
name: "success",
params: models.GetUserProfileParameters{
Username: "Test",
},
modifyURL: func(url string) string {
return url
},
Expand Down Expand Up @@ -147,19 +153,19 @@ func TestGetUserProfile(tt *testing.T) {
defer server.Close()

client := retroachievements.New(test.modifyURL(server.URL), "some_secret")
profile, err := client.GetUserProfile(test.username)
profile, err := client.GetUserProfile(test.params)
test.assert(t, profile, err)
})
}
}

func TestGetUserRecentAchievements(tt *testing.T) {
lookback := 20
now, err := time.Parse(time.DateTime, "2024-03-02 17:27:03")
require.NoError(tt, err)
tests := []struct {
name string
username string
lookbackMinutes int
params models.GetUserRecentAchievementsParameters
modifyURL func(url string) string
responseCode int
responseMessage []models.GetUserRecentAchievements
Expand All @@ -168,9 +174,11 @@ func TestGetUserRecentAchievements(tt *testing.T) {
assert func(t *testing.T, achievements []models.GetUserRecentAchievements, err error)
}{
{
name: "fail to call endpoint",
username: "Test",
lookbackMinutes: 60,
name: "fail to call endpoint",
params: models.GetUserRecentAchievementsParameters{
Username: "Test",
LookbackMinutes: &lookback,
},
modifyURL: func(url string) string {
return ""
},
Expand All @@ -190,13 +198,14 @@ func TestGetUserRecentAchievements(tt *testing.T) {
},
assert: func(t *testing.T, achievements []models.GetUserRecentAchievements, err error) {
require.Nil(t, achievements)
require.EqualError(t, err, "calling endpoint: Get \"/API/API_GetUserRecentAchievements.php?m=60&u=Test&y=some_secret\": unsupported protocol scheme \"\"")
require.EqualError(t, err, "calling endpoint: Get \"/API/API_GetUserRecentAchievements.php?m=20&u=Test&y=some_secret\": unsupported protocol scheme \"\"")
},
},
{
name: "error response",
username: "Test",
lookbackMinutes: 60,
name: "error response",
params: models.GetUserRecentAchievementsParameters{
Username: "Test",
},
modifyURL: func(url string) string {
return url
},
Expand All @@ -220,9 +229,10 @@ func TestGetUserRecentAchievements(tt *testing.T) {
},
},
{
name: "success",
username: "Test",
lookbackMinutes: 60,
name: "success",
params: models.GetUserRecentAchievementsParameters{
Username: "Test",
},
modifyURL: func(url string) string {
return url
},
Expand Down Expand Up @@ -297,7 +307,7 @@ func TestGetUserRecentAchievements(tt *testing.T) {
defer server.Close()

client := retroachievements.New(test.modifyURL(server.URL), "some_secret")
achievements, err := client.GetUserRecentAchievements(test.username, test.lookbackMinutes)
achievements, err := client.GetUserRecentAchievements(test.params)
test.assert(t, achievements, err)
})
}
Expand Down

0 comments on commit 4804967

Please sign in to comment.