Skip to content

Commit

Permalink
refactor models to usue common structs
Browse files Browse the repository at this point in the history
  • Loading branch information
joshraphael committed Aug 15, 2024
1 parent 9cf27aa commit c88a628
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 83 deletions.
File renamed without changes.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '^1.22.6' # 1.23 breaks the linter
go-version: '^1.22.6' # 1.23 breaks the linter, use stable when its fixed
- name: golangci-lint
uses: golangci/golangci-lint-action@v6
with:
Expand All @@ -36,4 +36,4 @@ jobs:
- name: check test coverage
uses: vladopajic/go-test-coverage@v2
with:
config: ./.testcoverage.yml
config: ./.github/.testcoverage.yml
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Package client contains all call handlers to retro achievements
// Package retroachievements contains all call handlers to the retro achievements API
package retroachievements

import (
Expand Down
10 changes: 5 additions & 5 deletions game.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

// GetGame gets info of a game
func (c *Client) GetGame(id int) (*models.Game, error) {
func (c *Client) GetGame(id int) (*models.GameInfo, error) {
resp, err := c.do(
raHttp.Method(http.MethodGet),
raHttp.Path("/API/API_GetGame.php"),
Expand All @@ -19,15 +19,15 @@ func (c *Client) GetGame(id int) (*models.Game, error) {
if err != nil {
return nil, fmt.Errorf("calling endpoint: %w", err)
}
game, err := raHttp.ResponseObject[models.Game](resp)
game, err := raHttp.ResponseObject[models.GameInfo](resp)
if err != nil {
return nil, fmt.Errorf("parsing response object: %w", err)
}
return game, nil
}

// GetGame gets extended info of a game
func (c *Client) GetGameExtended(id int) (*models.ExtentedGame, error) {
// GetGameExtended gets extended info of a game
func (c *Client) GetGameExtended(id int) (*models.ExtentedGameInfo, error) {
resp, err := c.do(
raHttp.Method(http.MethodGet),
raHttp.Path("/API/API_GetGameExtended.php"),
Expand All @@ -37,7 +37,7 @@ func (c *Client) GetGameExtended(id int) (*models.ExtentedGame, error) {
if err != nil {
return nil, fmt.Errorf("calling endpoint: %w", err)
}
game, err := raHttp.ResponseObject[models.ExtentedGame](resp)
game, err := raHttp.ResponseObject[models.ExtentedGameInfo](resp)
if err != nil {
return nil, fmt.Errorf("parsing response object: %w", err)
}
Expand Down
18 changes: 9 additions & 9 deletions game_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ func TestGetGame(tt *testing.T) {
id int
modifyURL func(url string) string
responseCode int
responseGame models.Game
responseGame models.GameInfo
responseError models.ErrorResponse
response func(gameBytes []byte, errorBytes []byte) []byte
assert func(t *testing.T, game *models.Game, err error)
assert func(t *testing.T, game *models.GameInfo, err error)
}{
{
name: "fail to call endpoint",
Expand All @@ -32,8 +32,8 @@ func TestGetGame(tt *testing.T) {
return ""
},
responseCode: http.StatusOK,
responseGame: models.Game{
CommonGame: models.CommonGame{
responseGame: models.GameInfo{
Game: models.Game{
Title: "Twisted Metal: Black",
ConsoleID: 21,
ForumTopicID: 16654,
Expand All @@ -57,7 +57,7 @@ func TestGetGame(tt *testing.T) {
response: func(gameBytes []byte, errorBytes []byte) []byte {
return gameBytes
},
assert: func(t *testing.T, game *models.Game, err error) {
assert: func(t *testing.T, game *models.GameInfo, err error) {
require.Nil(t, game)
require.EqualError(t, err, "calling endpoint: Get \"/API/API_GetGame.php?i=2991&y=some_secret\": unsupported protocol scheme \"\"")
},
Expand All @@ -82,7 +82,7 @@ func TestGetGame(tt *testing.T) {
response: func(gameBytes []byte, errorBytes []byte) []byte {
return errorBytes
},
assert: func(t *testing.T, game *models.Game, err error) {
assert: func(t *testing.T, game *models.GameInfo, err error) {
require.Nil(t, game)
require.EqualError(t, err, "parsing response object: error responses: [401] Not Authorized")
},
Expand All @@ -94,8 +94,8 @@ func TestGetGame(tt *testing.T) {
return url
},
responseCode: http.StatusOK,
responseGame: models.Game{
CommonGame: models.CommonGame{
responseGame: models.GameInfo{
Game: models.Game{
Title: "Twisted Metal: Black",
ConsoleID: 21,
ForumTopicID: 16654,
Expand All @@ -119,7 +119,7 @@ func TestGetGame(tt *testing.T) {
response: func(gameBytes []byte, errorBytes []byte) []byte {
return gameBytes
},
assert: func(t *testing.T, game *models.Game, err error) {
assert: func(t *testing.T, game *models.GameInfo, err error) {
require.NotNil(t, game)
require.Equal(t, game.Title, "Twisted Metal: Black")
require.Equal(t, game.GameTitle, "Twisted Metal: Black")
Expand Down
44 changes: 22 additions & 22 deletions models/achievements.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package models

// Achievement is a representation of achievements unlocked by a user
type Achievement struct {
// UnlockedAchievement is a representation of achievements unlocked by a user
type UnlockedAchievement struct {
Achievement
// The date the achievement was unlocked
Date DateTime `json:"Date"`

Expand All @@ -11,27 +12,12 @@ type Achievement struct {
// The ID of the achievement
AchievementID int `json:"AchievementID"`

// Title of the achievement
Title string `json:"Title"`

// Description of the achievement
Description string `json:"Description"`

// Name of the padge resource
BadgeName string `json:"BadgeName"`

// Points awarded
Points int `json:"Points"`

// Ratio of points the achievemnet is worth
TrueRatio int `json:"TrueRatio"`

// Type of achievement (standard, missable, progression, win_condition)
Type string `json:"Type"`

// Username of the author of the achievement
Author string `json:"Author"`

// Title of the game
GameTitle string `json:"GameTitle"`

Expand All @@ -51,19 +37,33 @@ type Achievement struct {
GameURL string `json:"GameURL"`
}

// GameAchievement is a representation of an achievement in a game
type GameAchievement struct {
Achievement
ID int `json:"ID"`
NumAwarded int `json:"NumAwarded"`
NumAwardedHardcore int `json:"NumAwardedHardcore"`
Title string `json:"Title"`
Description string `json:"Description"`
Points int `json:"Points"`
TrueRatio int `json:"TrueRatio"`
Author string `json:"Author"`
DateModified DateTime `json:"DateModified"`
DateCreated DateTime `json:"DateCreated"`
BadgeName string `json:"BadgeName"`
DisplayOrder int `json:"DisplayOrder"`
MemAddr string `json:"MemAddr"`
Type string `json:"type"`
}

// Achievement is a common representation of an achievement
type Achievement struct {
// Title of the achievement
Title string `json:"Title"`

// Description of the achievement
Description string `json:"Description"`
// Points awarded
Points int `json:"Points"`

// Ratio of points the achievemnet is worth
TrueRatio int `json:"TrueRatio"`

// Username of the author of the achievement
Author string `json:"Author"`
}
10 changes: 5 additions & 5 deletions models/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package models

import "time"

type CommonGame struct {
type Game struct {
Title string `json:"Title"`
ConsoleID int `json:"ConsoleID"`
ForumTopicID int `json:"ForumTopicID"`
Expand All @@ -17,16 +17,16 @@ type CommonGame struct {
Released LongMonthDate `json:"Released"`
}

type Game struct {
CommonGame
type GameInfo struct {
Game
GameTitle string `json:"GameTitle"`
ConsoleName string `json:"ConsoleName"`
Console string `json:"Console"`
GameIcon string `json:"GameIcon"`
}

type ExtentedGame struct {
CommonGame
type ExtentedGameInfo struct {
Game
ID int `json:"ID"`
IsFinal int `json:"IsFinal"`
RichPresencePatch string `json:"RichPresencePatch"`
Expand Down
12 changes: 6 additions & 6 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (c *Client) GetUserProfile(username string) (*models.Profile, error) {
}

// GetUserRecentAchievements gets all achievements within the last specified amount of minutes for a given username
func (c *Client) GetUserRecentAchievements(username string, lookbackMinutes int) ([]models.Achievement, error) {
func (c *Client) GetUserRecentAchievements(username string, lookbackMinutes int) ([]models.UnlockedAchievement, error) {
resp, err := c.do(
raHttp.Method(http.MethodGet),
raHttp.Path("/API/API_GetUserRecentAchievements.php"),
Expand All @@ -39,15 +39,15 @@ func (c *Client) GetUserRecentAchievements(username string, lookbackMinutes int)
if err != nil {
return nil, fmt.Errorf("calling endpoint: %w", err)
}
achievements, err := raHttp.ResponseList[models.Achievement](resp)
achievements, err := raHttp.ResponseList[models.UnlockedAchievement](resp)
if err != nil {
return nil, fmt.Errorf("parsing response list: %w", err)
}
return achievements, nil
}

// GetAchievementsEarnedBetween gets all achievements earned within a time frame for a given username
func (c *Client) GetAchievementsEarnedBetween(username string, from time.Time, to time.Time) ([]models.Achievement, error) {
func (c *Client) GetAchievementsEarnedBetween(username string, from time.Time, to time.Time) ([]models.UnlockedAchievement, error) {
resp, err := c.do(
raHttp.Method(http.MethodGet),
raHttp.Path("/API/API_GetAchievementsEarnedBetween.php"),
Expand All @@ -59,15 +59,15 @@ func (c *Client) GetAchievementsEarnedBetween(username string, from time.Time, t
if err != nil {
return nil, fmt.Errorf("calling endpoint: %w", err)
}
achievements, err := raHttp.ResponseList[models.Achievement](resp)
achievements, err := raHttp.ResponseList[models.UnlockedAchievement](resp)
if err != nil {
return nil, fmt.Errorf("parsing response list: %w", err)
}
return achievements, nil
}

// GetAchievementsEarnedOnDay gets all achievements earned on a specific day for a given username
func (c *Client) GetAchievementsEarnedOnDay(username string, date time.Time) ([]models.Achievement, error) {
func (c *Client) GetAchievementsEarnedOnDay(username string, date time.Time) ([]models.UnlockedAchievement, error) {
resp, err := c.do(
raHttp.Method(http.MethodGet),
raHttp.Path("/API/API_GetAchievementsEarnedOnDay.php"),
Expand All @@ -78,7 +78,7 @@ func (c *Client) GetAchievementsEarnedOnDay(username string, date time.Time) ([]
if err != nil {
return nil, fmt.Errorf("calling endpoint: %w", err)
}
achievements, err := raHttp.ResponseList[models.Achievement](resp)
achievements, err := raHttp.ResponseList[models.UnlockedAchievement](resp)
if err != nil {
return nil, fmt.Errorf("parsing response list: %w", err)
}
Expand Down
Loading

0 comments on commit c88a628

Please sign in to comment.