Skip to content

Commit

Permalink
add GetMostTicketedGames endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
joshraphael committed Nov 24, 2024
1 parent c42612c commit bc93b2f
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,5 @@ For convenience, the API docs and examples can be found in the tables below

|Function|Description|Links|
|-|-|-|
|`GetTicketByID()`|Gets ticket metadata information about a single achievement ticket, targeted by its ticket ID.|[docs](https://api-docs.retroachievements.org/v1/get-ticket-data/get-ticket-by-id.html) \| [example](examples/ticket/getticketbyid/getticketbyid.go)|
|`GetTicketByID()`|Gets ticket metadata information about a single achievement ticket, targeted by its ticket ID.|[docs](https://api-docs.retroachievements.org/v1/get-ticket-data/get-ticket-by-id.html) \| [example](examples/ticket/getticketbyid/getticketbyid.go)|
|`GetMostTicketedGames()`|Gets the games on the site with the highest count of opened achievement tickets.|[docs](https://api-docs.retroachievements.org/v1/get-ticket-data/get-most-ticketed-games.html) \| [example](examples/ticket/getmostticketedgames/getmostticketedgames.go)|
26 changes: 26 additions & 0 deletions examples/ticket/getmostticketedgames/getmostticketedgames.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Package getmostticketedgames provides an example for getting the games on the site with the highest count of opened achievement tickets.
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 getmostticketedgames.go`
*/
func main() {
secret := os.Getenv("RA_API_KEY")

client := retroachievements.NewClient(secret)

resp, err := client.GetMostTicketedGames(models.GetMostTicketedGamesParameters{})
if err != nil {
panic(err)
}

fmt.Printf("%+v\n", resp)
}
21 changes: 21 additions & 0 deletions models/ticket.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,24 @@ type GetTicketByID struct {
ReportTypeDescription string `json:"ReportTypeDescription"`
URL string `json:"URL"`
}

type GetMostTicketedGamesParameters struct {
// [Optional] The number of records to return (default: 10, max: 100).
Count *int

// [Optional] The number of entries to skip (default: 0).
Offset *int
}

type GetMostTicketedGames struct {
MostReportedGames []GetMostTicketedGamesMostReportedGame `json:"MostReportedGames"`
URL string `json:"URL"`
}

type GetMostTicketedGamesMostReportedGame struct {
GameID int `json:"GameID"`
GameTitle string `json:"GameTitle"`
GameIcon string `json:"GameIcon"`
Console string `json:"Console"`
OpenTickets int `json:"OpenTickets"`
}
25 changes: 25 additions & 0 deletions ticket.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,28 @@ func (c *Client) GetTicketByID(params models.GetTicketByIDParameters) (*models.G
}
return resp, nil
}

// GetMostTicketedGames gets the games on the site with the highest count of opened achievement tickets.
func (c *Client) GetMostTicketedGames(params models.GetMostTicketedGamesParameters) (*models.GetMostTicketedGames, error) {
details := []raHttp.RequestDetail{
raHttp.Method(http.MethodGet),
raHttp.Path("/API/API_GetTicketData.php"),
raHttp.APIToken(c.Secret),
raHttp.F(1),
}
if params.Count != nil {
details = append(details, raHttp.C(*params.Count))
}
if params.Offset != nil {
details = append(details, raHttp.O(*params.Offset))
}
r, err := c.do(details...)
if err != nil {
return nil, fmt.Errorf("calling endpoint: %w", err)
}
resp, err := raHttp.ResponseObject[models.GetMostTicketedGames](r)
if err != nil {
return nil, fmt.Errorf("parsing response object: %w", err)
}
return resp, nil
}
122 changes: 122 additions & 0 deletions ticket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,125 @@ func TestGetTicketByID(tt *testing.T) {
})
}
}

func TestGetMostTicketedGames(tt *testing.T) {
count := 10
offset := 12
tests := []struct {
name string
params models.GetMostTicketedGamesParameters
modifyURL func(url string) string
responseCode int
responseMessage models.GetMostTicketedGames
responseError models.ErrorResponse
response func(messageBytes []byte, errorBytes []byte) []byte
assert func(t *testing.T, resp *models.GetMostTicketedGames, err error)
}{
{
name: "fail to call endpoint",
params: models.GetMostTicketedGamesParameters{
Count: &count,
Offset: &offset,
},
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.GetMostTicketedGames, err error) {
require.Nil(t, resp)
require.EqualError(t, err, "calling endpoint: Get \"/API/API_GetTicketData.php?c=10&f=1&o=12&y=some_secret\": unsupported protocol scheme \"\"")
},
},
{
name: "error response",
params: models.GetMostTicketedGamesParameters{
Count: &count,
Offset: &offset,
},
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.GetMostTicketedGames, 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.GetMostTicketedGamesParameters{
Count: &count,
Offset: &offset,
},
modifyURL: func(url string) string {
return url
},
responseCode: http.StatusOK,
responseMessage: models.GetMostTicketedGames{
MostReportedGames: []models.GetMostTicketedGamesMostReportedGame{
{
GameID: 8301,
GameTitle: "Magical Vacation",
GameIcon: "/Images/080447.png",
Console: "Game Boy Advance",
OpenTickets: 4,
},
},
URL: "https://retroachievements.org/manage/most-reported-games",
},
response: func(messageBytes []byte, errorBytes []byte) []byte {
return messageBytes
},
assert: func(t *testing.T, resp *models.GetMostTicketedGames, err error) {
require.NotNil(t, resp)
require.Len(t, resp.MostReportedGames, 1)
require.Equal(t, 8301, resp.MostReportedGames[0].GameID)
require.Equal(t, "Magical Vacation", resp.MostReportedGames[0].GameTitle)
require.Equal(t, "/Images/080447.png", resp.MostReportedGames[0].GameIcon)
require.Equal(t, "Game Boy Advance", resp.MostReportedGames[0].Console)
require.Equal(t, 4, resp.MostReportedGames[0].OpenTickets)
require.Equal(t, "https://retroachievements.org/manage/most-reported-games", resp.URL)
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_GetTicketData.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.GetMostTicketedGames(test.params)
test.assert(t, resp, err)
})
}
}

0 comments on commit bc93b2f

Please sign in to comment.