-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add GetAchievementOfTheWeek endpoint
- Loading branch information
1 parent
5f2745d
commit 65301b4
Showing
5 changed files
with
276 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package retroachievements | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
raHttp "github.com/joshraphael/go-retroachievements/http" | ||
"github.com/joshraphael/go-retroachievements/models" | ||
) | ||
|
||
// GetAchievementOfTheWeek gets comprehensive metadata about the current Achievement of the Week. | ||
func (c *Client) GetAchievementOfTheWeek(params models.GetAchievementOfTheWeekParameters) (*models.GetAchievementOfTheWeek, error) { | ||
r, err := c.do( | ||
raHttp.Method(http.MethodGet), | ||
raHttp.Path("/API/API_GetAchievementOfTheWeek.php"), | ||
raHttp.APIToken(c.Secret), | ||
) | ||
if err != nil { | ||
return nil, fmt.Errorf("calling endpoint: %w", err) | ||
} | ||
resp, err := raHttp.ResponseObject[models.GetAchievementOfTheWeek](r) | ||
if err != nil { | ||
return nil, fmt.Errorf("parsing response object: %w", err) | ||
} | ||
return resp, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
package retroachievements_test | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
|
||
"github.com/joshraphael/go-retroachievements" | ||
"github.com/joshraphael/go-retroachievements/models" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGetAchievementOfTheWeek(tt *testing.T) { | ||
achievementType := "progression" | ||
dateCreated, err := time.Parse(time.DateTime, "2012-11-02 00:03:12") | ||
require.NoError(tt, err) | ||
dateModified, err := time.Parse(time.DateTime, "2023-09-30 02:00:49") | ||
require.NoError(tt, err) | ||
dateAwarded, err := time.Parse(time.RFC3339Nano, "2024-11-22T17:25:17.000000Z") | ||
require.NoError(tt, err) | ||
tests := []struct { | ||
name string | ||
params models.GetAchievementOfTheWeekParameters | ||
modifyURL func(url string) string | ||
responseCode int | ||
responseMessage models.GetAchievementOfTheWeek | ||
responseError models.ErrorResponse | ||
response func(messageBytes []byte, errorBytes []byte) []byte | ||
assert func(t *testing.T, resp *models.GetAchievementOfTheWeek, err error) | ||
}{ | ||
{ | ||
name: "fail to call endpoint", | ||
params: models.GetAchievementOfTheWeekParameters{}, | ||
modifyURL: func(url string) string { | ||
return "" | ||
}, | ||
responseCode: http.StatusOK, | ||
response: func(messageBytes []byte, errorBytes []byte) []byte { | ||
return messageBytes | ||
}, | ||
assert: func(t *testing.T, resp *models.GetAchievementOfTheWeek, err error) { | ||
require.Nil(t, resp) | ||
require.EqualError(t, err, "calling endpoint: Get \"/API/API_GetAchievementOfTheWeek.php?y=some_secret\": unsupported protocol scheme \"\"") | ||
}, | ||
}, | ||
{ | ||
name: "error response", | ||
params: models.GetAchievementOfTheWeekParameters{}, | ||
modifyURL: func(url string) string { | ||
return url | ||
}, | ||
responseCode: http.StatusUnauthorized, | ||
responseError: models.ErrorResponse{ | ||
Message: "test", | ||
Errors: []models.ErrorDetail{ | ||
{ | ||
Status: http.StatusUnauthorized, | ||
Code: "unauthorized", | ||
Title: "Not Authorized", | ||
}, | ||
}, | ||
}, | ||
response: func(messageBytes []byte, errorBytes []byte) []byte { | ||
return errorBytes | ||
}, | ||
assert: func(t *testing.T, resp *models.GetAchievementOfTheWeek, err error) { | ||
require.Nil(t, resp) | ||
require.EqualError(t, err, "parsing response object: error code 401 returned: {\"message\":\"test\",\"errors\":[{\"status\":401,\"code\":\"unauthorized\",\"title\":\"Not Authorized\"}]}") | ||
}, | ||
}, | ||
{ | ||
name: "success", | ||
params: models.GetAchievementOfTheWeekParameters{}, | ||
modifyURL: func(url string) string { | ||
return url | ||
}, | ||
responseCode: http.StatusOK, | ||
responseMessage: models.GetAchievementOfTheWeek{ | ||
UnlocksCount: 15906, | ||
UnlocksHardcoreCount: 8223, | ||
TotalPlayers: 38104, | ||
Achievement: models.GetAchievementOfTheWeekAchievement{ | ||
ID: 1, | ||
Title: "Ring Collector", | ||
Description: "Collect 100 rings", | ||
Points: 5, | ||
TrueRatio: 7, | ||
Author: "Scott", | ||
DateCreated: models.DateTime{ | ||
Time: dateCreated, | ||
}, | ||
DateModified: models.DateTime{ | ||
Time: dateModified, | ||
}, | ||
Type: &achievementType, | ||
}, | ||
Console: models.GetAchievementOfTheWeekConsole{ | ||
ID: 1, | ||
Title: "Genesis/Mega Drive", | ||
}, | ||
Game: models.GetAchievementOfTheWeekGame{ | ||
ID: 1, | ||
Title: "Sonic the Hedgehog", | ||
}, | ||
Unlocks: []models.GetAchievementOfTheWeekUnlock{ | ||
{ | ||
User: "redjedia", | ||
RAPoints: 524, | ||
RASoftcorePoints: 1615, | ||
DateAwarded: dateAwarded, | ||
HardcoreMode: 0, | ||
}, | ||
}, | ||
}, | ||
response: func(messageBytes []byte, errorBytes []byte) []byte { | ||
return messageBytes | ||
}, | ||
assert: func(t *testing.T, resp *models.GetAchievementOfTheWeek, err error) { | ||
require.NotNil(t, resp) | ||
require.Equal(t, 15906, resp.UnlocksCount) | ||
require.Equal(t, 8223, resp.UnlocksHardcoreCount) | ||
require.Equal(t, 38104, resp.TotalPlayers) | ||
require.Equal(t, 1, resp.Achievement.ID) | ||
require.Equal(t, "Ring Collector", resp.Achievement.Title) | ||
require.Equal(t, "Collect 100 rings", resp.Achievement.Description) | ||
require.Equal(t, 5, resp.Achievement.Points) | ||
require.Equal(t, 7, resp.Achievement.TrueRatio) | ||
require.Equal(t, "Scott", resp.Achievement.Author) | ||
require.Equal(t, dateCreated, resp.Achievement.DateCreated.Time) | ||
require.Equal(t, dateModified, resp.Achievement.DateModified.Time) | ||
require.NotNil(t, resp.Achievement.Type) | ||
require.Equal(t, achievementType, *resp.Achievement.Type) | ||
require.Equal(t, 1, resp.Console.ID) | ||
require.Equal(t, "Genesis/Mega Drive", resp.Console.Title) | ||
require.Equal(t, 1, resp.Game.ID) | ||
require.Equal(t, "Sonic the Hedgehog", resp.Game.Title) | ||
require.Len(t, resp.Unlocks, 1) | ||
require.Equal(t, "redjedia", resp.Unlocks[0].User) | ||
require.Equal(t, 524, resp.Unlocks[0].RAPoints) | ||
require.Equal(t, 1615, resp.Unlocks[0].RASoftcorePoints) | ||
require.Equal(t, dateAwarded, resp.Unlocks[0].DateAwarded) | ||
require.Equal(t, 0, resp.Unlocks[0].HardcoreMode) | ||
require.NoError(t, err) | ||
}, | ||
}, | ||
} | ||
for _, test := range tests { | ||
tt.Run(test.name, func(t *testing.T) { | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
expectedPath := "/API/API_GetAchievementOfTheWeek.php" | ||
if r.URL.Path != expectedPath { | ||
t.Errorf("Expected to request '%s', got: %s", expectedPath, r.URL.Path) | ||
} | ||
w.WriteHeader(test.responseCode) | ||
messageBytes, err := json.Marshal(test.responseMessage) | ||
require.NoError(t, err) | ||
errBytes, err := json.Marshal(test.responseError) | ||
require.NoError(t, err) | ||
resp := test.response(messageBytes, errBytes) | ||
num, err := w.Write(resp) | ||
require.NoError(t, err) | ||
require.Equal(t, num, len(resp)) | ||
})) | ||
defer server.Close() | ||
client := retroachievements.New(test.modifyURL(server.URL), "go-retroachievements/v0.0.0", "some_secret") | ||
resp, err := client.GetAchievementOfTheWeek(test.params) | ||
test.assert(t, resp, err) | ||
}) | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
examples/event/getachievementoftheweek/getachievementoftheweek.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Package getachievementoftheweek provides an example for getting comprehensive metadata about the current Achievement of the Week. | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/joshraphael/go-retroachievements" | ||
"github.com/joshraphael/go-retroachievements/models" | ||
) | ||
|
||
/* | ||
Test script, add RA_API_KEY to your env and use `go run getachievementoftheweek.go` | ||
*/ | ||
func main() { | ||
secret := os.Getenv("RA_API_KEY") | ||
|
||
client := retroachievements.NewClient(secret) | ||
|
||
resp, err := client.GetAchievementOfTheWeek(models.GetAchievementOfTheWeekParameters{}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Printf("%+v\n", resp) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package models | ||
|
||
import "time" | ||
|
||
type GetAchievementOfTheWeekParameters struct{} | ||
|
||
type GetAchievementOfTheWeek struct { | ||
Achievement GetAchievementOfTheWeekAchievement `json:"Achievement"` | ||
Console GetAchievementOfTheWeekConsole `json:"Console"` | ||
Game GetAchievementOfTheWeekGame `json:"Game"` | ||
UnlocksCount int `json:"UnlocksCount"` | ||
UnlocksHardcoreCount int `json:"UnlocksHardcoreCount"` | ||
TotalPlayers int `json:"TotalPlayers"` | ||
Unlocks []GetAchievementOfTheWeekUnlock `json:"Unlocks"` | ||
} | ||
|
||
type GetAchievementOfTheWeekAchievement struct { | ||
ID int `json:"ID"` | ||
Title string `json:"Title"` | ||
Description string `json:"Description"` | ||
Points int `json:"Points"` | ||
TrueRatio int `json:"TrueRatio"` | ||
Author string `json:"Author"` | ||
DateCreated DateTime `json:"DateCreated"` | ||
DateModified DateTime `json:"DateModified"` | ||
Type *string `json:"Type"` | ||
} | ||
|
||
type GetAchievementOfTheWeekConsole struct { | ||
ID int `json:"ID"` | ||
Title string `json:"Title"` | ||
} | ||
|
||
type GetAchievementOfTheWeekGame struct { | ||
ID int `json:"ID"` | ||
Title string `json:"Title"` | ||
} | ||
|
||
type GetAchievementOfTheWeekUnlock struct { | ||
User string `json:"User"` | ||
RAPoints int `json:"RAPoints"` | ||
RASoftcorePoints int `json:"RASoftcorePoints"` | ||
DateAwarded time.Time `json:"DateAwarded"` | ||
HardcoreMode int `json:"HardcoreMode"` | ||
} |