Skip to content

Commit

Permalink
refactor GetUserPoints
Browse files Browse the repository at this point in the history
  • Loading branch information
joshraphael committed Nov 16, 2024
1 parent 1eeb877 commit 832460a
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ For convenience, the API docs and examples can be found in the tables below
|`GetUserAwards()`|Get a list of a user's site awards/badges.|[docs](https://api-docs.retroachievements.org/v1/get-user-awards.html) \| [example](examples/user/getuserawards/getuserawards.go)|
|`GetUserClaims()`|Get a list of set development claims made over the lifetime of a user.|[docs](https://api-docs.retroachievements.org/v1/get-user-claims.html) \| [example](examples/user/getuserclaims/getuserclaims.go)|
|`GetUserGameRankAndScore()`|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)|
|`GetUserPoints()`|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)|

Expand Down
5 changes: 4 additions & 1 deletion examples/user/getuserpoints/getuserpoints.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.GetUserPoints("joshraphael")
resp, err := client.GetUserPoints(models.GetUserPointsParameters{
Username: "joshraphael",
})
if err != nil {
panic(err)
}
Expand Down
5 changes: 0 additions & 5 deletions models/awards.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,3 @@ type UserAwards struct {
SiteAwardsCount int `json:"SiteAwardsCount"`
VisibleUserAwards []Award `json:"VisibleUserAwards"`
}

type Points struct {
Points int `json:"Points"`
SoftcorePoints int `json:"SoftcorePoints"`
}
12 changes: 12 additions & 0 deletions models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ type GetUserClaims struct {
MinutesLeft int `json:"MinutesLeft"`
}

// GetUserGameRankAndScoreParameters contains the parameters needed for getting a users rank and score
type GetUserGameRankAndScoreParameters struct {
// The target username
Username string
Expand All @@ -404,3 +405,14 @@ type GetUserGameRankAndScore struct {
TotalScore int `json:"TotalScore"`
LastAward *DateTime `json:"LastAward"`
}

// GetUserPointsParameters contains the parameters needed for getting a users points
type GetUserPointsParameters struct {
// The target username
Username string
}

type GetUserPoints struct {
Points int `json:"Points"`
SoftcorePoints int `json:"SoftcorePoints"`
}
6 changes: 3 additions & 3 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,17 +184,17 @@ func (c *Client) GetUserGameRankAndScore(params models.GetUserGameRankAndScorePa
}

// GetUserPoints get a user's total hardcore and softcore points.
func (c *Client) GetUserPoints(username string) (*models.Points, error) {
func (c *Client) GetUserPoints(params models.GetUserPointsParameters) (*models.GetUserPoints, error) {
resp, err := c.do(
raHttp.Method(http.MethodGet),
raHttp.Path("/API/API_GetUserPoints.php"),
raHttp.APIToken(c.Secret),
raHttp.Username(username),
raHttp.Username(params.Username),
)
if err != nil {
return nil, fmt.Errorf("calling endpoint: %w", err)
}
points, err := raHttp.ResponseObject[models.Points](resp)
points, err := raHttp.ResponseObject[models.GetUserPoints](resp)
if err != nil {
return nil, fmt.Errorf("parsing response object: %w", err)
}
Expand Down
34 changes: 20 additions & 14 deletions user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1461,17 +1461,19 @@ func TestGetUserGameRankAndScore(tt *testing.T) {
func TestGetUserPoints(tt *testing.T) {
tests := []struct {
name string
username string
params models.GetUserPointsParameters
modifyURL func(url string) string
responseCode int
responseMessage models.Points
responseMessage models.GetUserPoints
responseError models.ErrorResponse
response func(messageBytes []byte, errorBytes []byte) []byte
assert func(t *testing.T, points *models.Points, err error)
assert func(t *testing.T, points *models.GetUserPoints, err error)
}{
{
name: "fail to call endpoint",
username: "Test",
name: "fail to call endpoint",
params: models.GetUserPointsParameters{
Username: "Test",
},
modifyURL: func(url string) string {
return ""
},
Expand All @@ -1489,14 +1491,16 @@ func TestGetUserPoints(tt *testing.T) {
response: func(messageBytes []byte, errorBytes []byte) []byte {
return errorBytes
},
assert: func(t *testing.T, points *models.Points, err error) {
assert: func(t *testing.T, points *models.GetUserPoints, err error) {
require.Nil(t, points)
require.EqualError(t, err, "calling endpoint: Get \"/API/API_GetUserPoints.php?u=Test&y=some_secret\": unsupported protocol scheme \"\"")
},
},
{
name: "error response",
username: "Test",
name: "error response",
params: models.GetUserPointsParameters{
Username: "Test",
},
modifyURL: func(url string) string {
return url
},
Expand All @@ -1514,26 +1518,28 @@ func TestGetUserPoints(tt *testing.T) {
response: func(messageBytes []byte, errorBytes []byte) []byte {
return errorBytes
},
assert: func(t *testing.T, points *models.Points, err error) {
assert: func(t *testing.T, points *models.GetUserPoints, err error) {
require.Nil(t, points)
require.EqualError(t, err, "parsing response object: error responses: [401] Not Authorized")
},
},
{
name: "success",
username: "Test",
name: "success",
params: models.GetUserPointsParameters{
Username: "Test",
},
modifyURL: func(url string) string {
return url
},
responseCode: http.StatusOK,
responseMessage: models.Points{
responseMessage: models.GetUserPoints{
Points: 230,
SoftcorePoints: 342,
},
response: func(messageBytes []byte, errorBytes []byte) []byte {
return messageBytes
},
assert: func(t *testing.T, points *models.Points, err error) {
assert: func(t *testing.T, points *models.GetUserPoints, err error) {
require.NotNil(t, points)
require.Equal(t, 230, points.Points)
require.Equal(t, 342, points.SoftcorePoints)
Expand Down Expand Up @@ -1561,7 +1567,7 @@ func TestGetUserPoints(tt *testing.T) {
defer server.Close()

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

0 comments on commit 832460a

Please sign in to comment.