Skip to content

Commit

Permalink
refactor GetUserAwards
Browse files Browse the repository at this point in the history
  • Loading branch information
joshraphael committed Nov 15, 2024
1 parent 971a950 commit 6702305
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 30 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ For convenience, the API docs and examples can be found in the tables below
|`GetAchievementsEarnedOnDay()`|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()`|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()`|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)|
|`GetUserAwards(string)`|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)|
|`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(string)`|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(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)|
Expand Down
5 changes: 4 additions & 1 deletion examples/user/getuserawards/getuserawards.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.GetUserAwards("jamiras")
resp, err := client.GetUserAwards(models.GetUserAwardsParameters{
Username: "jamiras",
})
if err != nil {
panic(err)
}
Expand Down
13 changes: 0 additions & 13 deletions models/awards.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,6 @@ type UserAwards struct {
VisibleUserAwards []Award `json:"VisibleUserAwards"`
}

type Award struct {
AwardedAt RFC3339NumColonTZ `json:"AwardedAt"`
AwardType string `json:"AwardType"`
AwardData int `json:"AwardData"`
AwardDataExtra int `json:"AwardDataExtra"`
DisplayOrder int `json:"DisplayOrder"`
Title *string `json:"Title"`
ConsoleID *int `json:"ConsoleID"`
ConsoleName *string `json:"ConsoleName"`
Flags *int `json:"Flags"`
ImageIcon *string `json:"ImageIcon"`
}

type Points struct {
Points int `json:"Points"`
SoftcorePoints int `json:"SoftcorePoints"`
Expand Down
31 changes: 31 additions & 0 deletions models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,3 +332,34 @@ type CompletionProgress struct {
HighestAwardKind *string `json:"HighestAwardKind"`
HighestAwardDate *RFC3339NumColonTZ `json:"HighestAwardDate"`
}

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

type GetUserAwards struct {
TotalAwardsCount int `json:"TotalAwardsCount"`
HiddenAwardsCount int `json:"HiddenAwardsCount"`
MasteryAwardsCount int `json:"MasteryAwardsCount"`
CompletionAwardsCount int `json:"CompletionAwardsCount"`
BeatenHardcoreAwardsCount int `json:"BeatenHardcoreAwardsCount"`
BeatenSoftcoreAwardsCount int `json:"BeatenSoftcoreAwardsCount"`
EventAwardsCount int `json:"EventAwardsCount"`
SiteAwardsCount int `json:"SiteAwardsCount"`
VisibleUserAwards []Award `json:"VisibleUserAwards"`
}

type Award struct {
AwardedAt RFC3339NumColonTZ `json:"AwardedAt"`
AwardType string `json:"AwardType"`
AwardData int `json:"AwardData"`
AwardDataExtra int `json:"AwardDataExtra"`
DisplayOrder int `json:"DisplayOrder"`
Title *string `json:"Title"`
ConsoleID *int `json:"ConsoleID"`
ConsoleName *string `json:"ConsoleName"`
Flags *int `json:"Flags"`
ImageIcon *string `json:"ImageIcon"`
}
6 changes: 3 additions & 3 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,17 @@ func (c *Client) GetUserCompletionProgress(params models.GetUserCompletionProgre
}

// GetUserAwards get a list of a user's site awards/badges.
func (c *Client) GetUserAwards(username string) (*models.UserAwards, error) {
func (c *Client) GetUserAwards(params models.GetUserAwardsParameters) (*models.GetUserAwards, error) {
resp, err := c.do(
raHttp.Method(http.MethodGet),
raHttp.Path("/API/API_GetUserAwards.php"),
raHttp.APIToken(c.Secret),
raHttp.Username(username),
raHttp.Username(params.Username),
)
if err != nil {
return nil, fmt.Errorf("calling endpoint: %w", err)
}
awards, err := raHttp.ResponseObject[models.UserAwards](resp)
awards, err := raHttp.ResponseObject[models.GetUserAwards](resp)
if err != nil {
return nil, fmt.Errorf("parsing response object: %w", err)
}
Expand Down
30 changes: 18 additions & 12 deletions user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1020,17 +1020,19 @@ func TestGetUserAwards(tt *testing.T) {
imageIcons := "/Images/074224.png"
tests := []struct {
name string
username string
params models.GetUserAwardsParameters
modifyURL func(url string) string
responseCode int
responseMessage models.UserAwards
responseError models.ErrorResponse
response func(messageBytes []byte, errorBytes []byte) []byte
assert func(t *testing.T, userAwards *models.UserAwards, err error)
assert func(t *testing.T, userAwards *models.GetUserAwards, err error)
}{
{
name: "fail to call endpoint",
username: "Test",
name: "fail to call endpoint",
params: models.GetUserAwardsParameters{
Username: "Test",
},
modifyURL: func(url string) string {
return ""
},
Expand All @@ -1048,14 +1050,16 @@ func TestGetUserAwards(tt *testing.T) {
response: func(messageBytes []byte, errorBytes []byte) []byte {
return errorBytes
},
assert: func(t *testing.T, userAwards *models.UserAwards, err error) {
assert: func(t *testing.T, userAwards *models.GetUserAwards, err error) {
require.Nil(t, userAwards)
require.EqualError(t, err, "calling endpoint: Get \"/API/API_GetUserAwards.php?u=Test&y=some_secret\": unsupported protocol scheme \"\"")
},
},
{
name: "error response",
username: "Test",
name: "error response",
params: models.GetUserAwardsParameters{
Username: "Test",
},
modifyURL: func(url string) string {
return url
},
Expand All @@ -1073,14 +1077,16 @@ func TestGetUserAwards(tt *testing.T) {
response: func(messageBytes []byte, errorBytes []byte) []byte {
return errorBytes
},
assert: func(t *testing.T, userAwards *models.UserAwards, err error) {
assert: func(t *testing.T, userAwards *models.GetUserAwards, err error) {
require.Nil(t, userAwards)
require.EqualError(t, err, "parsing response object: error responses: [401] Not Authorized")
},
},
{
name: "success",
username: "Test",
name: "success",
params: models.GetUserAwardsParameters{
Username: "Test",
},
modifyURL: func(url string) string {
return url
},
Expand Down Expand Up @@ -1114,7 +1120,7 @@ func TestGetUserAwards(tt *testing.T) {
response: func(messageBytes []byte, errorBytes []byte) []byte {
return messageBytes
},
assert: func(t *testing.T, userAwards *models.UserAwards, err error) {
assert: func(t *testing.T, userAwards *models.GetUserAwards, err error) {
require.NotNil(t, userAwards)
require.Equal(t, 9, userAwards.TotalAwardsCount)
require.Equal(t, 0, userAwards.HiddenAwardsCount)
Expand Down Expand Up @@ -1159,7 +1165,7 @@ func TestGetUserAwards(tt *testing.T) {
defer server.Close()

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

0 comments on commit 6702305

Please sign in to comment.