Skip to content

Commit

Permalink
refactor models
Browse files Browse the repository at this point in the history
  • Loading branch information
joshraphael committed Nov 1, 2024
1 parent 2ffe416 commit 847f040
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Package getuserrecentachievements provides an example for a users achievements in the last X minutes
// Package getuserrecentlyplayedgames provides an example for a users recently played games
package main

import (
Expand All @@ -9,7 +9,7 @@ import (
)

/*
Test script, add RA_API_KEY to your env and use `go run getuserrecentachievements.go`
Test script, add RA_API_KEY to your env and use `go run getuserrecentlyplayedgames.go`
*/
func main() {
secret := os.Getenv("RA_API_KEY")
Expand Down
99 changes: 99 additions & 0 deletions models/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package models

// GetUserProfile describes elements of a users profile
type GetUserProfile struct {
// Username of the profile
User string `json:"User"`

// URL resource to the image of the profile avatar
UserPic string `json:"UserPic"`

// Date the user joined
MemberSince DateTime `json:"MemberSince"`

// Last game played rich presence text
RichPresenceMsg string `json:"RichPresenceMsg"`

// ID of last game played
LastGameID int `json:"LastGameID"`

// Number of achievements unlocked by players
ContribCount int `json:"ContribCount"`

// Number of points awarded to players
ContribYield int `json:"ContribYield"`

// Number of points awarded on this profile
TotalPoints int `json:"TotalPoints"`

// Number of softcore points awarded on this profile
TotalSoftcorePoints int `json:"TotalSoftcorePoints"`

// Number of RetroPoints awarded on this profile
TotalTruePoints int `json:"TotalTruePoints"`

// Site permissions (0 = Normal, 1 = Jr. Dev, 2 = Developer, 3 = Moderator, 4 = Admin)
Permissions int `json:"Permissions"`

// "1" if the user is untracked, otherwise "0"
Untracked int `json:"Untracked"`

// ID of the profile
ID int `json:"ID"`

// Allows other users to comment on their profile wall
UserWallActive bool `json:"UserWallActive"`

// Custom status message displayed on profile
Motto string `json:"Motto"`
}

type GetUserRecentAchievements 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"`

// The date the achievement was unlocked
Date DateTime `json:"Date"`

// Mode the achievement was unlocked in: 1 if in hardcore mode, 0 if not
HardcoreMode int `json:"HardcoreMode"`

// The ID of the achievement
AchievementID int `json:"AchievementID"`

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

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

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

// URL resource of the game icon
GameIcon string `json:"GameIcon"`

// ID of the game
GameID int `json:"GameID"`

// Common name of the console
ConsoleName string `json:"ConsoleName"`

// URL resource to the image used for the achievment badge
BadgeURL string `json:"BadgeURL"`

// URL resource to the game page
GameURL string `json:"GameURL"`
}
8 changes: 4 additions & 4 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

// GetUserProfile get a user's basic profile information.
func (c *Client) GetUserProfile(username string) (*models.Profile, error) {
func (c *Client) GetUserProfile(username string) (*models.GetUserProfile, error) {
resp, err := c.do(
raHttp.Method(http.MethodGet),
raHttp.Path("/API/API_GetUserProfile.php"),
Expand All @@ -20,15 +20,15 @@ func (c *Client) GetUserProfile(username string) (*models.Profile, error) {
if err != nil {
return nil, fmt.Errorf("calling endpoint: %w", err)
}
profile, err := raHttp.ResponseObject[models.Profile](resp)
profile, err := raHttp.ResponseObject[models.GetUserProfile](resp)
if err != nil {
return nil, fmt.Errorf("parsing response object: %w", err)
}
return profile, nil
}

// GetUserRecentAchievements get a list of achievements recently earned by the user.
func (c *Client) GetUserRecentAchievements(username string, lookbackMinutes int) ([]models.UnlockedAchievement, error) {
func (c *Client) GetUserRecentAchievements(username string, lookbackMinutes int) ([]models.GetUserRecentAchievements, error) {
resp, err := c.do(
raHttp.Method(http.MethodGet),
raHttp.Path("/API/API_GetUserRecentAchievements.php"),
Expand All @@ -39,7 +39,7 @@ func (c *Client) GetUserRecentAchievements(username string, lookbackMinutes int)
if err != nil {
return nil, fmt.Errorf("calling endpoint: %w", err)
}
achievements, err := raHttp.ResponseList[models.UnlockedAchievement](resp)
achievements, err := raHttp.ResponseList[models.GetUserRecentAchievements](resp)
if err != nil {
return nil, fmt.Errorf("parsing response list: %w", err)
}
Expand Down
36 changes: 17 additions & 19 deletions user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ func TestGetUserProfile(tt *testing.T) {
username string
modifyURL func(url string) string
responseCode int
responseMessage models.Profile
responseMessage models.GetUserProfile
responseError models.ErrorResponse
response func(messageBytes []byte, errorBytes []byte) []byte
assert func(t *testing.T, profile *models.Profile, err error)
assert func(t *testing.T, profile *models.GetUserProfile, err error)
}{
{
name: "fail to call endpoint",
Expand All @@ -45,7 +45,7 @@ func TestGetUserProfile(tt *testing.T) {
response: func(messageBytes []byte, errorBytes []byte) []byte {
return errorBytes
},
assert: func(t *testing.T, profile *models.Profile, err error) {
assert: func(t *testing.T, profile *models.GetUserProfile, err error) {
require.Nil(t, profile)
require.EqualError(t, err, "calling endpoint: Get \"/API/API_GetUserProfile.php?u=Test&y=some_secret\": unsupported protocol scheme \"\"")
},
Expand All @@ -70,7 +70,7 @@ func TestGetUserProfile(tt *testing.T) {
response: func(messageBytes []byte, errorBytes []byte) []byte {
return errorBytes
},
assert: func(t *testing.T, profile *models.Profile, err error) {
assert: func(t *testing.T, profile *models.GetUserProfile, err error) {
require.Nil(t, profile)
require.EqualError(t, err, "parsing response object: error responses: [401] Not Authorized")
},
Expand All @@ -82,7 +82,7 @@ func TestGetUserProfile(tt *testing.T) {
return url
},
responseCode: http.StatusOK,
responseMessage: models.Profile{
responseMessage: models.GetUserProfile{
User: "xXxSnip3rxXx",
UserPic: "/some/resource.png",
MemberSince: models.DateTime{
Expand All @@ -104,7 +104,7 @@ func TestGetUserProfile(tt *testing.T) {
response: func(messageBytes []byte, errorBytes []byte) []byte {
return messageBytes
},
assert: func(t *testing.T, profile *models.Profile, err error) {
assert: func(t *testing.T, profile *models.GetUserProfile, err error) {
require.NoError(t, err)
require.NotNil(t, profile)
require.Equal(t, "xXxSnip3rxXx", profile.User)
Expand Down Expand Up @@ -162,10 +162,10 @@ func TestGetUserRecentAchievements(tt *testing.T) {
lookbackMinutes int
modifyURL func(url string) string
responseCode int
responseMessage []models.UnlockedAchievement
responseMessage []models.GetUserRecentAchievements
responseError models.ErrorResponse
response func(messageBytes []byte, errorBytes []byte) []byte
assert func(t *testing.T, achievements []models.UnlockedAchievement, err error)
assert func(t *testing.T, achievements []models.GetUserRecentAchievements, err error)
}{
{
name: "fail to call endpoint",
Expand All @@ -188,7 +188,7 @@ func TestGetUserRecentAchievements(tt *testing.T) {
response: func(messageBytes []byte, errorBytes []byte) []byte {
return errorBytes
},
assert: func(t *testing.T, achievements []models.UnlockedAchievement, err error) {
assert: func(t *testing.T, achievements []models.GetUserRecentAchievements, err error) {
require.Nil(t, achievements)
require.EqualError(t, err, "calling endpoint: Get \"/API/API_GetUserRecentAchievements.php?m=60&u=Test&y=some_secret\": unsupported protocol scheme \"\"")
},
Expand All @@ -214,7 +214,7 @@ func TestGetUserRecentAchievements(tt *testing.T) {
response: func(messageBytes []byte, errorBytes []byte) []byte {
return errorBytes
},
assert: func(t *testing.T, achievements []models.UnlockedAchievement, err error) {
assert: func(t *testing.T, achievements []models.GetUserRecentAchievements, err error) {
require.Nil(t, achievements)
require.EqualError(t, err, "parsing response list: error responses: [401] Not Authorized")
},
Expand All @@ -227,15 +227,13 @@ func TestGetUserRecentAchievements(tt *testing.T) {
return url
},
responseCode: http.StatusOK,
responseMessage: []models.UnlockedAchievement{
responseMessage: []models.GetUserRecentAchievements{
{
Achievement: models.Achievement{
Title: "Beat Level 1",
Description: "Finish level 1",
Points: 10,
TrueRatio: 234,
Author: "jamiras",
},
Title: "Beat Level 1",
Description: "Finish level 1",
Points: 10,
TrueRatio: 234,
Author: "jamiras",
Date: models.DateTime{
Time: now,
},
Expand All @@ -254,7 +252,7 @@ func TestGetUserRecentAchievements(tt *testing.T) {
response: func(messageBytes []byte, errorBytes []byte) []byte {
return messageBytes
},
assert: func(t *testing.T, achievements []models.UnlockedAchievement, err error) {
assert: func(t *testing.T, achievements []models.GetUserRecentAchievements, err error) {
require.NotNil(t, achievements)
require.Len(t, achievements, 1)
require.Equal(t, models.DateTime{
Expand Down

0 comments on commit 847f040

Please sign in to comment.