diff --git a/README.md b/README.md index b42279f..14f5713 100644 --- a/README.md +++ b/README.md @@ -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)| \ No newline at end of file +|`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)| \ No newline at end of file diff --git a/examples/ticket/getmostticketedgames/getmostticketedgames.go b/examples/ticket/getmostticketedgames/getmostticketedgames.go new file mode 100644 index 0000000..8a258c8 --- /dev/null +++ b/examples/ticket/getmostticketedgames/getmostticketedgames.go @@ -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) +} diff --git a/models/ticket.go b/models/ticket.go index 521de53..abd77a0 100644 --- a/models/ticket.go +++ b/models/ticket.go @@ -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"` +} diff --git a/ticket.go b/ticket.go index 84200ac..f4045a1 100644 --- a/ticket.go +++ b/ticket.go @@ -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 +} diff --git a/ticket_test.go b/ticket_test.go index dd7cc6b..fd42e87 100644 --- a/ticket_test.go +++ b/ticket_test.go @@ -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) + }) + } +}