Skip to content

Commit 971a950

Browse files
committed
refactor GetUserCompletionProgress
1 parent bdd9e04 commit 971a950

File tree

6 files changed

+55
-39
lines changed

6 files changed

+55
-39
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ For convenience, the API docs and examples can be found in the tables below
5353
|`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)|
5454
|`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)|
5555
|`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)|
56-
|`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)|
56+
|`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)|
5757
|`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)|
5858
|`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)|
5959
|`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)|

examples/user/getusercompletionprogress/getusercompletionprogress.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"os"
77

88
"github.com/joshraphael/go-retroachievements"
9+
"github.com/joshraphael/go-retroachievements/models"
910
)
1011

1112
/*
@@ -16,7 +17,9 @@ func main() {
1617

1718
client := retroachievements.NewClient(secret)
1819

19-
resp, err := client.GetUserCompletionProgress("jamiras")
20+
resp, err := client.GetUserCompletionProgress(models.GetUserCompletionProgressParameters{
21+
Username: "jamiras",
22+
})
2023
if err != nil {
2124
panic(err)
2225
}

models/game.go

-20
Original file line numberDiff line numberDiff line change
@@ -57,26 +57,6 @@ type UserGameProgress struct {
5757
HighestAwardDate *RFC3339NumColonTZ `json:"HighestAwardDate"`
5858
}
5959

60-
type UserCompletionProgress struct {
61-
Count int `json:"Count"`
62-
Total int `json:"Total"`
63-
Results []CompletionProgress `json:"Results"`
64-
}
65-
66-
type CompletionProgress struct {
67-
GameID int `json:"GameID"`
68-
Title string `json:"Title"`
69-
ImageIcon string `json:"ImageIcon"`
70-
ConsoleID int `json:"ConsoleID"`
71-
ConsoleName string `json:"ConsoleName"`
72-
MaxPossible int `json:"MaxPossible"`
73-
NumAwarded int `json:"NumAwarded"`
74-
NumAwardedHardcore int `json:"NumAwardedHardcore"`
75-
MostRecentAwardedDate RFC3339NumColonTZ `json:"MostRecentAwardedDate"`
76-
HighestAwardKind *string `json:"HighestAwardKind"`
77-
HighestAwardDate *RFC3339NumColonTZ `json:"HighestAwardDate"`
78-
}
79-
8060
type UserGameRankScore struct {
8161
User string `json:"User"`
8262
UserRank int `json:"UserRank"`

models/user.go

+27
Original file line numberDiff line numberDiff line change
@@ -305,3 +305,30 @@ type GetGameInfoAndUserProgress struct {
305305
HighestAwardKind *string `json:"HighestAwardKind"`
306306
HighestAwardDate *RFC3339NumColonTZ `json:"HighestAwardDate"`
307307
}
308+
309+
// GetUserCompletionProgressParameters contains the parameters needed for getting a users completion progress
310+
type GetUserCompletionProgressParameters struct {
311+
// The target username
312+
Username string
313+
}
314+
315+
// GetUserCompletionProgress
316+
type GetUserCompletionProgress struct {
317+
Count int `json:"Count"`
318+
Total int `json:"Total"`
319+
Results []CompletionProgress `json:"Results"`
320+
}
321+
322+
type CompletionProgress struct {
323+
GameID int `json:"GameID"`
324+
Title string `json:"Title"`
325+
ImageIcon string `json:"ImageIcon"`
326+
ConsoleID int `json:"ConsoleID"`
327+
ConsoleName string `json:"ConsoleName"`
328+
MaxPossible int `json:"MaxPossible"`
329+
NumAwarded int `json:"NumAwarded"`
330+
NumAwardedHardcore int `json:"NumAwardedHardcore"`
331+
MostRecentAwardedDate RFC3339NumColonTZ `json:"MostRecentAwardedDate"`
332+
HighestAwardKind *string `json:"HighestAwardKind"`
333+
HighestAwardDate *RFC3339NumColonTZ `json:"HighestAwardDate"`
334+
}

user.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -111,17 +111,17 @@ func (c *Client) GetGameInfoAndUserProgress(params models.GetGameInfoAndUserProg
111111
}
112112

113113
// GetUserCompletionProgress get metadata about all the user's played games and any awards associated with them.
114-
func (c *Client) GetUserCompletionProgress(username string) (*models.UserCompletionProgress, error) {
114+
func (c *Client) GetUserCompletionProgress(params models.GetUserCompletionProgressParameters) (*models.GetUserCompletionProgress, error) {
115115
resp, err := c.do(
116116
raHttp.Method(http.MethodGet),
117117
raHttp.Path("/API/API_GetUserCompletionProgress.php"),
118118
raHttp.APIToken(c.Secret),
119-
raHttp.Username(username),
119+
raHttp.Username(params.Username),
120120
)
121121
if err != nil {
122122
return nil, fmt.Errorf("calling endpoint: %w", err)
123123
}
124-
completionProgress, err := raHttp.ResponseObject[models.UserCompletionProgress](resp)
124+
completionProgress, err := raHttp.ResponseObject[models.GetUserCompletionProgress](resp)
125125
if err != nil {
126126
return nil, fmt.Errorf("parsing response object: %w", err)
127127
}

user_test.go

+20-14
Original file line numberDiff line numberDiff line change
@@ -867,17 +867,19 @@ func TestGetUserCompletionProgress(tt *testing.T) {
867867
require.NoError(tt, err)
868868
tests := []struct {
869869
name string
870-
username string
870+
params models.GetUserCompletionProgressParameters
871871
modifyURL func(url string) string
872872
responseCode int
873-
responseMessage models.UserCompletionProgress
873+
responseMessage models.GetUserCompletionProgress
874874
responseError models.ErrorResponse
875875
response func(messageBytes []byte, errorBytes []byte) []byte
876-
assert func(t *testing.T, completionProgress *models.UserCompletionProgress, err error)
876+
assert func(t *testing.T, completionProgress *models.GetUserCompletionProgress, err error)
877877
}{
878878
{
879-
name: "fail to call endpoint",
880-
username: "Test",
879+
name: "fail to call endpoint",
880+
params: models.GetUserCompletionProgressParameters{
881+
Username: "Test",
882+
},
881883
modifyURL: func(url string) string {
882884
return ""
883885
},
@@ -895,14 +897,16 @@ func TestGetUserCompletionProgress(tt *testing.T) {
895897
response: func(messageBytes []byte, errorBytes []byte) []byte {
896898
return errorBytes
897899
},
898-
assert: func(t *testing.T, completionProgress *models.UserCompletionProgress, err error) {
900+
assert: func(t *testing.T, completionProgress *models.GetUserCompletionProgress, err error) {
899901
require.Nil(t, completionProgress)
900902
require.EqualError(t, err, "calling endpoint: Get \"/API/API_GetUserCompletionProgress.php?u=Test&y=some_secret\": unsupported protocol scheme \"\"")
901903
},
902904
},
903905
{
904-
name: "error response",
905-
username: "Test",
906+
name: "error response",
907+
params: models.GetUserCompletionProgressParameters{
908+
Username: "Test",
909+
},
906910
modifyURL: func(url string) string {
907911
return url
908912
},
@@ -920,19 +924,21 @@ func TestGetUserCompletionProgress(tt *testing.T) {
920924
response: func(messageBytes []byte, errorBytes []byte) []byte {
921925
return errorBytes
922926
},
923-
assert: func(t *testing.T, completionProgress *models.UserCompletionProgress, err error) {
927+
assert: func(t *testing.T, completionProgress *models.GetUserCompletionProgress, err error) {
924928
require.Nil(t, completionProgress)
925929
require.EqualError(t, err, "parsing response object: error responses: [401] Not Authorized")
926930
},
927931
},
928932
{
929-
name: "success",
930-
username: "Test",
933+
name: "success",
934+
params: models.GetUserCompletionProgressParameters{
935+
Username: "Test",
936+
},
931937
modifyURL: func(url string) string {
932938
return url
933939
},
934940
responseCode: http.StatusOK,
935-
responseMessage: models.UserCompletionProgress{
941+
responseMessage: models.GetUserCompletionProgress{
936942
Count: 19,
937943
Total: 18,
938944
Results: []models.CompletionProgress{
@@ -958,7 +964,7 @@ func TestGetUserCompletionProgress(tt *testing.T) {
958964
response: func(messageBytes []byte, errorBytes []byte) []byte {
959965
return messageBytes
960966
},
961-
assert: func(t *testing.T, completionProgress *models.UserCompletionProgress, err error) {
967+
assert: func(t *testing.T, completionProgress *models.GetUserCompletionProgress, err error) {
962968
require.NotNil(t, completionProgress)
963969
require.Equal(t, 19, completionProgress.Count)
964970
require.Equal(t, 18, completionProgress.Total)
@@ -998,7 +1004,7 @@ func TestGetUserCompletionProgress(tt *testing.T) {
9981004
defer server.Close()
9991005

10001006
client := retroachievements.New(test.modifyURL(server.URL), "some_secret")
1001-
userCompletionProgress, err := client.GetUserCompletionProgress(test.username)
1007+
userCompletionProgress, err := client.GetUserCompletionProgress(test.params)
10021008
test.assert(t, userCompletionProgress, err)
10031009
})
10041010
}

0 commit comments

Comments
 (0)