Skip to content

Commit

Permalink
add GetMostRecentTickets endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
joshraphael committed Nov 24, 2024
1 parent bc93b2f commit be6b0df
Show file tree
Hide file tree
Showing 5 changed files with 261 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,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)|
|`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)|
|`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)|
|`GetMostRecentTickets()`|Gets ticket metadata information about the latest opened achievement tickets on RetroAchievements.|[docs](https://api-docs.retroachievements.org/v1/get-ticket-data/get-most-recent-tickets.html) \| [example](examples/ticket/getmostrecenttickets/getmostrecenttickets.go)|
29 changes: 29 additions & 0 deletions examples/ticket/getmostrecenttickets/getmostrecenttickets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Package getmostrecenttickets provides an example for getting ticket metadata information about the latest opened achievement tickets on RetroAchievements.
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 getmostrecenttickets.go`
*/
func main() {
secret := os.Getenv("RA_API_KEY")

client := retroachievements.NewClient(secret)

count := 100
resp, err := client.GetMostRecentTickets(models.GetMostRecentTicketsParameters{
Count: &count,
})
if err != nil {
panic(err)
}

fmt.Printf("%+v\n", resp)
}
39 changes: 39 additions & 0 deletions models/ticket.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,42 @@ type GetMostTicketedGamesMostReportedGame struct {
Console string `json:"Console"`
OpenTickets int `json:"OpenTickets"`
}

type GetMostRecentTicketsParameters 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 GetMostRecentTickets struct {
OpenTickets int `json:"OpenTickets"`
URL string `json:"URL"`
RecentTickets []GetMostRecentTicketsRecentTicket `json:"RecentTickets"`
}

type GetMostRecentTicketsRecentTicket struct {
ID int `json:"ID"`
AchievementID int `json:"AchievementID"`
AchievementTitle string `json:"AchievementTitle"`
AchievementDesc string `json:"AchievementDesc"`
AchievementType *string `json:"AchievementType"`
Points int `json:"Points"`
BadgeName string `json:"BadgeName"`
AchievementAuthor string `json:"AchievementAuthor"`
GameID int `json:"GameID"`
ConsoleName string `json:"ConsoleName"`
GameTitle string `json:"GameTitle"`
GameIcon string `json:"GameIcon"`
ReportedAt DateTime `json:"ReportedAt"`
ReportType int `json:"ReportType"`
ReportState int `json:"ReportState"`
Hardcore *int `json:"Hardcore"`
ReportNotes string `json:"ReportNotes"`
ReportedBy string `json:"ReportedBy"`
ResolvedAt *DateTime `json:"ResolvedAt"`
ResolvedBy *string `json:"ResolvedBy"`
ReportStateDescription string `json:"ReportStateDescription"`
ReportTypeDescription string `json:"ReportTypeDescription"`
}
24 changes: 24 additions & 0 deletions ticket.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,27 @@ func (c *Client) GetMostTicketedGames(params models.GetMostTicketedGamesParamete
}
return resp, nil
}

// GetMostRecentTickets gets ticket metadata information about the latest opened achievement tickets on RetroAchievements.
func (c *Client) GetMostRecentTickets(params models.GetMostRecentTicketsParameters) (*models.GetMostRecentTickets, error) {
details := []raHttp.RequestDetail{
raHttp.Method(http.MethodGet),
raHttp.Path("/API/API_GetTicketData.php"),
raHttp.APIToken(c.Secret),
}
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.GetMostRecentTickets](r)
if err != nil {
return nil, fmt.Errorf("parsing response object: %w", err)
}
return resp, nil
}
167 changes: 167 additions & 0 deletions ticket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,170 @@ func TestGetMostTicketedGames(tt *testing.T) {
})
}
}

func TestGetMostRecentTickets(tt *testing.T) {
count := 10
offset := 12
achievementType := "progression"
reportedAt, err := time.Parse(time.DateTime, "2014-02-22 23:23:53")
require.NoError(tt, err)
hardcore := 1
resolvedAt, err := time.Parse(time.DateTime, "2014-02-24 22:51:10")
require.NoError(tt, err)
resolvedBy := "Scott"
tests := []struct {
name string
params models.GetMostRecentTicketsParameters
modifyURL func(url string) string
responseCode int
responseMessage models.GetMostRecentTickets
responseError models.ErrorResponse
response func(messageBytes []byte, errorBytes []byte) []byte
assert func(t *testing.T, resp *models.GetMostRecentTickets, err error)
}{
{
name: "fail to call endpoint",
params: models.GetMostRecentTicketsParameters{
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.GetMostRecentTickets, err error) {
require.Nil(t, resp)
require.EqualError(t, err, "calling endpoint: Get \"/API/API_GetTicketData.php?c=10&o=12&y=some_secret\": unsupported protocol scheme \"\"")
},
},
{
name: "error response",
params: models.GetMostRecentTicketsParameters{
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.GetMostRecentTickets, 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.GetMostRecentTicketsParameters{
Count: &count,
Offset: &offset,
},
modifyURL: func(url string) string {
return url
},
responseCode: http.StatusOK,
responseMessage: models.GetMostRecentTickets{
OpenTickets: 1951,
URL: "https://retroachievements.org/tickets",
RecentTickets: []models.GetMostRecentTicketsRecentTicket{
{
ID: 1,
AchievementID: 3778,
AchievementTitle: "Exhibition Match",
AchievementDesc: "Complete Stage 3: Flugelheim Museum",
AchievementType: &achievementType,
Points: 10,
BadgeName: "04357",
AchievementAuthor: "Batman",
GameID: 45,
ConsoleName: "Genesis/Mega Drive",
GameTitle: "Batman: The Video Game",
GameIcon: "/Images/053393.png",
ReportedAt: models.DateTime{
Time: reportedAt,
},
ReportType: 0,
ReportState: 2,
Hardcore: &hardcore,
ReportNotes: "This achievement didn't trigger for some reason?",
ReportedBy: "qwe",
ResolvedAt: &models.DateTime{
Time: resolvedAt,
},
ResolvedBy: &resolvedBy,
ReportStateDescription: "Resolved",
ReportTypeDescription: "Invalid ticket type",
},
},
},
response: func(messageBytes []byte, errorBytes []byte) []byte {
return messageBytes
},
assert: func(t *testing.T, resp *models.GetMostRecentTickets, err error) {
require.NotNil(t, resp)
require.Equal(t, 1951, resp.OpenTickets)
require.Equal(t, "https://retroachievements.org/tickets", resp.URL)
require.Len(t, resp.RecentTickets, 1)
require.Equal(t, 1, resp.RecentTickets[0].ID)
require.Equal(t, 3778, resp.RecentTickets[0].AchievementID)
require.Equal(t, "Exhibition Match", resp.RecentTickets[0].AchievementTitle)
require.Equal(t, "Complete Stage 3: Flugelheim Museum", resp.RecentTickets[0].AchievementDesc)
require.NotNil(t, resp.RecentTickets[0].AchievementType)
require.Equal(t, achievementType, *resp.RecentTickets[0].AchievementType)
require.Equal(t, 10, resp.RecentTickets[0].Points)
require.Equal(t, "04357", resp.RecentTickets[0].BadgeName)
require.Equal(t, "Batman", resp.RecentTickets[0].AchievementAuthor)
require.Equal(t, 45, resp.RecentTickets[0].GameID)
require.Equal(t, "Genesis/Mega Drive", resp.RecentTickets[0].ConsoleName)
require.Equal(t, "Batman: The Video Game", resp.RecentTickets[0].GameTitle)
require.Equal(t, "/Images/053393.png", resp.RecentTickets[0].GameIcon)
require.Equal(t, reportedAt, resp.RecentTickets[0].ReportedAt.Time)
require.Equal(t, 0, resp.RecentTickets[0].ReportType)
require.Equal(t, 2, resp.RecentTickets[0].ReportState)
require.NotNil(t, resp.RecentTickets[0].ResolvedBy)
require.Equal(t, resolvedBy, *resp.RecentTickets[0].ResolvedBy)
require.Equal(t, "Resolved", resp.RecentTickets[0].ReportStateDescription)
require.Equal(t, "Invalid ticket type", resp.RecentTickets[0].ReportTypeDescription)
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.GetMostRecentTickets(test.params)
test.assert(t, resp, err)
})
}
}

0 comments on commit be6b0df

Please sign in to comment.