Skip to content

Commit

Permalink
add GetTopTenUsers endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
joshraphael committed Nov 24, 2024
1 parent bbe5d93 commit 5f2745d
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,5 @@ For convenience, the API docs and examples can be found in the tables below
|-|-|-|
|`GetRecentGameAwards()`|Gets all recently granted game awards across the site's userbase.|[docs](https://api-docs.retroachievements.org/v1/get-recent-game-awards.html) \| [example](examples/feed/getrecentgameawards/getrecentgameawards.go)|
|`GetActiveClaims()`|Gets information about all active set claims (max: 1000).|[docs](https://api-docs.retroachievements.org/v1/get-active-claims.html) \| [example](examples/feed/getactiveclaims/getactiveclaims.go)|
|`GetClaims()`|Gets information about all achievement set development claims of a specified kind: completed, dropped, or expired (max: 1000).|[docs](https://api-docs.retroachievements.org/v1/get-claims.html) \| [example](examples/feed/getclaims/getclaims.go)|
|`GetClaims()`|Gets information about all achievement set development claims of a specified kind: completed, dropped, or expired (max: 1000).|[docs](https://api-docs.retroachievements.org/v1/get-claims.html) \| [example](examples/feed/getclaims/getclaims.go)|
|`GetTopTenUsers()`|Gets the current top ten users, ranked by hardcore points, on the site.|[docs](https://api-docs.retroachievements.org/v1/get-top-ten-users.html) \| [example](examples/feed/gettoptenusers/gettoptenusers.go)|
26 changes: 26 additions & 0 deletions examples/feed/gettoptenusers/gettoptenusers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Package gettoptenusers provides an example for getting the current top ten users, ranked by hardcore points, on the site.
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 gettoptenusers.go`
*/
func main() {
secret := os.Getenv("RA_API_KEY")

client := retroachievements.NewClient(secret)

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

fmt.Printf("%+v\n", resp)
}
17 changes: 17 additions & 0 deletions feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,20 @@ func (c *Client) GetClaims(params models.GetClaimsParameters) ([]models.GetClaim
}
return resp, nil
}

// GetTopTenUsers gets the current top ten users, ranked by hardcore points, on the site.
func (c *Client) GetTopTenUsers(params models.GetTopTenUsersParameters) ([]models.GetTopTenUsers, error) {
r, err := c.do(
raHttp.Method(http.MethodGet),
raHttp.Path("/API/API_GetTopTenUsers.php"),
raHttp.APIToken(c.Secret),
)
if err != nil {
return nil, fmt.Errorf("calling endpoint: %w", err)
}
resp, err := raHttp.ResponseList[models.GetTopTenUsers](r)
if err != nil {
return nil, fmt.Errorf("parsing response list: %w", err)
}
return resp, nil
}
111 changes: 111 additions & 0 deletions feed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,3 +458,114 @@ func TestGetClaims(tt *testing.T) {
})
}
}

func TestGetTopTenUsers(tt *testing.T) {
tests := []struct {
name string
params models.GetTopTenUsersParameters
modifyURL func(url string) string
responseCode int
responseMessage []models.GetTopTenUsers
responseError models.ErrorResponse
response func(messageBytes []byte, errorBytes []byte) []byte
assert func(t *testing.T, resp []models.GetTopTenUsers, err error)
}{
{
name: "fail to call endpoint",
params: models.GetTopTenUsersParameters{},
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.GetTopTenUsers, err error) {
require.Nil(t, resp)
require.EqualError(t, err, "calling endpoint: Get \"/API/API_GetTopTenUsers.php?y=some_secret\": unsupported protocol scheme \"\"")
},
},
{
name: "error response",
params: models.GetTopTenUsersParameters{},
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.GetTopTenUsers, err error) {
require.Nil(t, resp)
require.EqualError(t, err, "parsing response list: error code 401 returned: {\"message\":\"test\",\"errors\":[{\"status\":401,\"code\":\"unauthorized\",\"title\":\"Not Authorized\"}]}")
},
},
{
name: "success",
params: models.GetTopTenUsersParameters{},
modifyURL: func(url string) string {
return url
},
responseCode: http.StatusOK,
responseMessage: []models.GetTopTenUsers{
{
Username: "MaxMilyin",
HarcordPoints: 443235,
RetroPoints: 1825214,
},
{
Username: "Sarconius",
HarcordPoints: 427984,
RetroPoints: 2913697,
},
},
response: func(messageBytes []byte, errorBytes []byte) []byte {
return messageBytes
},
assert: func(t *testing.T, resp []models.GetTopTenUsers, err error) {
require.NotNil(t, resp)
require.Len(t, resp, 2)
require.Equal(t, resp[0].Username, "MaxMilyin")
require.Equal(t, resp[0].HarcordPoints, 443235)
require.Equal(t, resp[0].RetroPoints, 1825214)
require.Equal(t, resp[1].Username, "Sarconius")
require.Equal(t, resp[1].HarcordPoints, 427984)
require.Equal(t, resp[1].RetroPoints, 2913697)
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_GetTopTenUsers.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.GetTopTenUsers(test.params)
test.assert(t, resp, err)
})
}
}
8 changes: 8 additions & 0 deletions models/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,11 @@ type GetClaims struct {
UserIsJrDev int `json:"UserIsJrDev"`
MinutesLeft int `json:"MinutesLeft"`
}

type GetTopTenUsersParameters struct{}

type GetTopTenUsers struct {
Username string `json:"1"`
HarcordPoints int `json:"2"`
RetroPoints int `json:"3"`
}

0 comments on commit 5f2745d

Please sign in to comment.