diff --git a/README.md b/README.md index c7c9fbe..b8ffa13 100644 --- a/README.md +++ b/README.md @@ -124,4 +124,5 @@ For convenience, the API docs and examples can be found in the tables below |`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)| |`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)| -|`GetGameTicketStats()`|Gets ticket stats for a game, targeted by that game's unique ID.|[docs](https://api-docs.retroachievements.org/v1/get-ticket-data/get-game-ticket-stats.html) \| [example](examples/ticket/getgameticketstats/getgameticketstats.go)| \ No newline at end of file +|`GetGameTicketStats()`|Gets ticket stats for a game, targeted by that game's unique ID.|[docs](https://api-docs.retroachievements.org/v1/get-ticket-data/get-game-ticket-stats.html) \| [example](examples/ticket/getgameticketstats/getgameticketstats.go)| +|`GetDeveloperTicketStats()`|Gets ticket stats for a developer, targeted by that developer's site username.|[docs](https://api-docs.retroachievements.org/v1/get-ticket-data/get-developer-ticket-stats.html) \| [example](examples/ticket/getdeveloperticketstats/getdeveloperticketstats.go)| \ No newline at end of file diff --git a/examples/ticket/getdeveloperticketstats/getdeveloperticketstats.go b/examples/ticket/getdeveloperticketstats/getdeveloperticketstats.go new file mode 100644 index 0000000..e863fcd --- /dev/null +++ b/examples/ticket/getdeveloperticketstats/getdeveloperticketstats.go @@ -0,0 +1,30 @@ +// getdeveloperticketstats + +// Package getdeveloperticketstats provides an example for getting ticket stats for a developer, targeted by that developer's site username. +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 getdeveloperticketstats.go` +*/ +func main() { + secret := os.Getenv("RA_API_KEY") + + client := retroachievements.NewClient(secret) + + resp, err := client.GetDeveloperTicketStats(models.GetDeveloperTicketStatsParameters{ + Username: "jamiras", + }) + if err != nil { + panic(err) + } + + fmt.Printf("%+v\n", resp) +} diff --git a/models/ticket.go b/models/ticket.go index 2a894db..756225f 100644 --- a/models/ticket.go +++ b/models/ticket.go @@ -135,3 +135,17 @@ type GetGameTicketStatsTicket struct { ReportStateDescription string `json:"ReportStateDescription"` ReportTypeDescription string `json:"ReportTypeDescription"` } + +type GetDeveloperTicketStatsParameters struct { + // The target username + Username string +} + +type GetDeveloperTicketStats struct { + User string `json:"User"` + Open int `json:"Open"` + Closed int `json:"Closed"` + Resolved int `json:"Resolved"` + Total int `json:"Total"` + URL string `json:"URL"` +} diff --git a/ticket.go b/ticket.go index f8a24ae..e575855 100644 --- a/ticket.go +++ b/ticket.go @@ -102,3 +102,21 @@ func (c *Client) GetGameTicketStats(params models.GetGameTicketStatsParameters) } return resp, nil } + +// GetDeveloperTicketStats gets ticket stats for a developer, targeted by that developer's site username. +func (c *Client) GetDeveloperTicketStats(params models.GetDeveloperTicketStatsParameters) (*models.GetDeveloperTicketStats, error) { + r, err := c.do( + raHttp.Method(http.MethodGet), + raHttp.Path("/API/API_GetTicketData.php"), + raHttp.APIToken(c.Secret), + raHttp.U(params.Username), + ) + if err != nil { + return nil, fmt.Errorf("calling endpoint: %w", err) + } + resp, err := raHttp.ResponseObject[models.GetDeveloperTicketStats](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 93eb4da..ebd0739 100644 --- a/ticket_test.go +++ b/ticket_test.go @@ -639,3 +639,115 @@ func TestGetGameTicketStats(tt *testing.T) { }) } } + +func TestGetDeveloperTicketStats(tt *testing.T) { + tests := []struct { + name string + params models.GetDeveloperTicketStatsParameters + modifyURL func(url string) string + responseCode int + responseMessage models.GetDeveloperTicketStats + responseError models.ErrorResponse + response func(messageBytes []byte, errorBytes []byte) []byte + assert func(t *testing.T, resp *models.GetDeveloperTicketStats, err error) + }{ + { + name: "fail to call endpoint", + params: models.GetDeveloperTicketStatsParameters{ + Username: "jamiras", + }, + 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.GetDeveloperTicketStats, err error) { + require.Nil(t, resp) + require.EqualError(t, err, "calling endpoint: Get \"/API/API_GetTicketData.php?u=jamiras&y=some_secret\": unsupported protocol scheme \"\"") + }, + }, + { + name: "error response", + params: models.GetDeveloperTicketStatsParameters{ + Username: "jamiras", + }, + 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.GetDeveloperTicketStats, 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.GetDeveloperTicketStatsParameters{ + Username: "jamiras", + }, + modifyURL: func(url string) string { + return url + }, + responseCode: http.StatusOK, + responseMessage: models.GetDeveloperTicketStats{ + User: "jamiras", + Open: 0, + Closed: 46, + Resolved: 68, + Total: 114, + URL: "https://retroachievements.org/user/jamiras/tickets", + }, + response: func(messageBytes []byte, errorBytes []byte) []byte { + return messageBytes + }, + assert: func(t *testing.T, resp *models.GetDeveloperTicketStats, err error) { + require.NotNil(t, resp) + require.Equal(t, "jamiras", resp.User) + require.Equal(t, 0, resp.Open) + require.Equal(t, 46, resp.Closed) + require.Equal(t, 68, resp.Resolved) + require.Equal(t, 114, resp.Total) + require.Equal(t, "https://retroachievements.org/user/jamiras/tickets", 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.GetDeveloperTicketStats(test.params) + test.assert(t, resp, err) + }) + } +}