Skip to content

Commit

Permalink
refact GetUserClaims
Browse files Browse the repository at this point in the history
  • Loading branch information
joshraphael committed Nov 16, 2024
1 parent 6702305 commit ac7f8b1
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 40 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ For convenience, the API docs and examples can be found in the tables below
|`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()`|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)|
|`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(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)|
|`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)|
Expand Down
5 changes: 4 additions & 1 deletion examples/user/getuserclaims/getuserclaims.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.GetUserClaims("jamiras")
resp, err := client.GetUserClaims(models.GetUserClaimsParameters{
Username: "jamiras",
})
if err != nil {
panic(err)
}
Expand Down
20 changes: 0 additions & 20 deletions models/claims.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,3 @@ type Claim struct {
Created DateTime `json:"Created"`
Expiration DateTime `json:"Expiration"`
}

type UserClaims struct {
ID int `json:"ID"`
User string `json:"User"`
GameID int `json:"GameID"`
GameTitle string `json:"GameTitle"`
GameIcon string `json:"GameIcon"`
ConsoleID int `json:"ConsoleID"`
ConsoleName string `json:"ConsoleName"`
ClaimType int `json:"ClaimType"`
SetType int `json:"SetType"`
Status int `json:"Status"`
Extension int `json:"Extension"`
Special int `json:"Special"`
Created DateTime `json:"Created"`
DoneTime DateTime `json:"DoneTime"`
Updated DateTime `json:"Updated"`
UserIsJrDev int `json:"UserIsJrDev"`
MinutesLeft int `json:"MinutesLeft"`
}
26 changes: 26 additions & 0 deletions models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,29 @@ type Award struct {
Flags *int `json:"Flags"`
ImageIcon *string `json:"ImageIcon"`
}

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

type GetUserClaims struct {
ID int `json:"ID"`
User string `json:"User"`
GameID int `json:"GameID"`
GameTitle string `json:"GameTitle"`
GameIcon string `json:"GameIcon"`
ConsoleID int `json:"ConsoleID"`
ConsoleName string `json:"ConsoleName"`
ClaimType int `json:"ClaimType"`
SetType int `json:"SetType"`
Status int `json:"Status"`
Extension int `json:"Extension"`
Special int `json:"Special"`
Created DateTime `json:"Created"`
DoneTime DateTime `json:"DoneTime"`
Updated DateTime `json:"Updated"`
UserIsJrDev int `json:"UserIsJrDev"`
MinutesLeft int `json:"MinutesLeft"`
}
8 changes: 4 additions & 4 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,21 +147,21 @@ func (c *Client) GetUserAwards(params models.GetUserAwardsParameters) (*models.G
}

// GetUserClaims get a list of set development claims made over the lifetime of a user.
func (c *Client) GetUserClaims(username string) ([]models.UserClaims, error) {
func (c *Client) GetUserClaims(params models.GetUserClaimsParameters) ([]models.GetUserClaims, error) {
resp, err := c.do(
raHttp.Method(http.MethodGet),
raHttp.Path("/API/API_GetUserClaims.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.ResponseList[models.UserClaims](resp)
claims, err := raHttp.ResponseList[models.GetUserClaims](resp)
if err != nil {
return nil, fmt.Errorf("parsing response list: %w", err)
}
return awards, nil
return claims, nil
}

// GetUserGameRankAndScore get metadata about how a user has performed on a given game.
Expand Down
34 changes: 20 additions & 14 deletions user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1178,17 +1178,19 @@ func TestGetUserClaims(tt *testing.T) {
require.NoError(tt, err)
tests := []struct {
name string
username string
params models.GetUserClaimsParameters
modifyURL func(url string) string
responseCode int
responseMessage []models.UserClaims
responseMessage []models.GetUserClaims
responseError models.ErrorResponse
response func(messageBytes []byte, errorBytes []byte) []byte
assert func(t *testing.T, userClaims []models.UserClaims, err error)
assert func(t *testing.T, userClaims []models.GetUserClaims, err error)
}{
{
name: "fail to call endpoint",
username: "Test",
name: "fail to call endpoint",
params: models.GetUserClaimsParameters{
Username: "Test",
},
modifyURL: func(url string) string {
return ""
},
Expand All @@ -1206,14 +1208,16 @@ func TestGetUserClaims(tt *testing.T) {
response: func(messageBytes []byte, errorBytes []byte) []byte {
return errorBytes
},
assert: func(t *testing.T, userClaims []models.UserClaims, err error) {
assert: func(t *testing.T, userClaims []models.GetUserClaims, err error) {
require.Nil(t, userClaims)
require.EqualError(t, err, "calling endpoint: Get \"/API/API_GetUserClaims.php?u=Test&y=some_secret\": unsupported protocol scheme \"\"")
},
},
{
name: "error response",
username: "Test",
name: "error response",
params: models.GetUserClaimsParameters{
Username: "Test",
},
modifyURL: func(url string) string {
return url
},
Expand All @@ -1231,19 +1235,21 @@ func TestGetUserClaims(tt *testing.T) {
response: func(messageBytes []byte, errorBytes []byte) []byte {
return errorBytes
},
assert: func(t *testing.T, userClaims []models.UserClaims, err error) {
assert: func(t *testing.T, userClaims []models.GetUserClaims, err error) {
require.Nil(t, userClaims)
require.EqualError(t, err, "parsing response list: error responses: [401] Not Authorized")
},
},
{
name: "success",
username: "Test",
name: "success",
params: models.GetUserClaimsParameters{
Username: "Test",
},
modifyURL: func(url string) string {
return url
},
responseCode: http.StatusOK,
responseMessage: []models.UserClaims{
responseMessage: []models.GetUserClaims{
{
ID: 13657,
User: "joshraphael",
Expand Down Expand Up @@ -1273,7 +1279,7 @@ func TestGetUserClaims(tt *testing.T) {
response: func(messageBytes []byte, errorBytes []byte) []byte {
return messageBytes
},
assert: func(t *testing.T, userClaims []models.UserClaims, err error) {
assert: func(t *testing.T, userClaims []models.GetUserClaims, err error) {
require.NotNil(t, userClaims)
require.Len(t, userClaims, 1)
require.Equal(t, 13657, userClaims[0].ID)
Expand Down Expand Up @@ -1317,7 +1323,7 @@ func TestGetUserClaims(tt *testing.T) {
defer server.Close()

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

0 comments on commit ac7f8b1

Please sign in to comment.