Skip to content

Commit 66d14ee

Browse files
Automatically update PHP SDK
1 parent 0b7914e commit 66d14ee

6 files changed

Lines changed: 213 additions & 1 deletion

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "trophy/php",
3-
"version": "1.0.18",
3+
"version": "1.0.19",
44
"description": "Trophy PHP Library",
55
"keywords": [
66
"trophy",
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Trophy\Streaks\Requests;
4+
5+
use Trophy\Core\Json\JsonSerializableType;
6+
use Trophy\Streaks\Types\StreaksRankingsRequestType;
7+
8+
class StreaksRankingsRequest extends JsonSerializableType
9+
{
10+
/**
11+
* @var ?int $limit Number of users to return. Must be between 1 and 100.
12+
*/
13+
public ?int $limit;
14+
15+
/**
16+
* @var ?value-of<StreaksRankingsRequestType> $type Whether to rank users by active streaks or longest streaks ever achieved.
17+
*/
18+
public ?string $type;
19+
20+
/**
21+
* @param array{
22+
* limit?: ?int,
23+
* type?: ?value-of<StreaksRankingsRequestType>,
24+
* } $values
25+
*/
26+
public function __construct(
27+
array $values = [],
28+
) {
29+
$this->limit = $values['limit'] ?? null;
30+
$this->type = $values['type'] ?? null;
31+
}
32+
}

src/Streaks/StreaksClient.php

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
3+
namespace Trophy\Streaks;
4+
5+
use GuzzleHttp\ClientInterface;
6+
use Trophy\Core\Client\RawClient;
7+
use Trophy\Streaks\Requests\StreaksRankingsRequest;
8+
use Trophy\Types\StreakRankingUser;
9+
use Trophy\Exceptions\TrophyException;
10+
use Trophy\Exceptions\TrophyApiException;
11+
use Trophy\Core\Json\JsonApiRequest;
12+
use Trophy\Environments;
13+
use Trophy\Core\Client\HttpMethod;
14+
use Trophy\Core\Json\JsonDecoder;
15+
use JsonException;
16+
use GuzzleHttp\Exception\RequestException;
17+
use Psr\Http\Client\ClientExceptionInterface;
18+
19+
class StreaksClient
20+
{
21+
/**
22+
* @var array{
23+
* baseUrl?: string,
24+
* client?: ClientInterface,
25+
* headers?: array<string, string>,
26+
* maxRetries?: int,
27+
* } $options
28+
*/
29+
private array $options;
30+
31+
/**
32+
* @var RawClient $client
33+
*/
34+
private RawClient $client;
35+
36+
/**
37+
* @param RawClient $client
38+
* @param ?array{
39+
* baseUrl?: string,
40+
* client?: ClientInterface,
41+
* headers?: array<string, string>,
42+
* maxRetries?: int,
43+
* } $options
44+
*/
45+
public function __construct(
46+
RawClient $client,
47+
?array $options = null,
48+
) {
49+
$this->client = $client;
50+
$this->options = $options ?? [];
51+
}
52+
53+
/**
54+
* Get the top users by streak length (active or longest).
55+
*
56+
* @param StreaksRankingsRequest $request
57+
* @param ?array{
58+
* baseUrl?: string,
59+
* maxRetries?: int,
60+
* } $options
61+
* @return array<StreakRankingUser>
62+
* @throws TrophyException
63+
* @throws TrophyApiException
64+
*/
65+
public function rankings(StreaksRankingsRequest $request, ?array $options = null): array
66+
{
67+
$options = array_merge($this->options, $options ?? []);
68+
$query = [];
69+
if ($request->limit != null) {
70+
$query['limit'] = $request->limit;
71+
}
72+
if ($request->type != null) {
73+
$query['type'] = $request->type;
74+
}
75+
try {
76+
$response = $this->client->sendRequest(
77+
new JsonApiRequest(
78+
baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Default_->value,
79+
path: "streaks/rankings",
80+
method: HttpMethod::GET,
81+
query: $query,
82+
),
83+
$options,
84+
);
85+
$statusCode = $response->getStatusCode();
86+
if ($statusCode >= 200 && $statusCode < 400) {
87+
$json = $response->getBody()->getContents();
88+
return JsonDecoder::decodeArray($json, [StreakRankingUser::class]); // @phpstan-ignore-line
89+
}
90+
} catch (JsonException $e) {
91+
throw new TrophyException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e);
92+
} catch (RequestException $e) {
93+
$response = $e->getResponse();
94+
if ($response === null) {
95+
throw new TrophyException(message: $e->getMessage(), previous: $e);
96+
}
97+
throw new TrophyApiException(
98+
message: "API request failed",
99+
statusCode: $response->getStatusCode(),
100+
body: $response->getBody()->getContents(),
101+
);
102+
} catch (ClientExceptionInterface $e) {
103+
throw new TrophyException(message: $e->getMessage(), previous: $e);
104+
}
105+
throw new TrophyApiException(
106+
message: 'API request failed',
107+
statusCode: $statusCode,
108+
body: $response->getBody()->getContents(),
109+
);
110+
}
111+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Trophy\Streaks\Types;
4+
5+
enum StreaksRankingsRequestType: string
6+
{
7+
case Active = "active";
8+
case Longest = "longest";
9+
}

src/TrophyClient.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Trophy\Achievements\AchievementsClient;
66
use Trophy\Metrics\MetricsClient;
77
use Trophy\Users\UsersClient;
8+
use Trophy\Streaks\StreaksClient;
89
use Trophy\Points\PointsClient;
910
use GuzzleHttp\ClientInterface;
1011
use Trophy\Core\Client\RawClient;
@@ -26,6 +27,11 @@ class TrophyClient
2627
*/
2728
public UsersClient $users;
2829

30+
/**
31+
* @var StreaksClient $streaks
32+
*/
33+
public StreaksClient $streaks;
34+
2935
/**
3036
* @var PointsClient $points
3137
*/
@@ -78,6 +84,7 @@ public function __construct(
7884
$this->achievements = new AchievementsClient($this->client, $this->options);
7985
$this->metrics = new MetricsClient($this->client, $this->options);
8086
$this->users = new UsersClient($this->client, $this->options);
87+
$this->streaks = new StreaksClient($this->client, $this->options);
8188
$this->points = new PointsClient($this->client, $this->options);
8289
}
8390
}

src/Types/StreakRankingUser.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace Trophy\Types;
4+
5+
use Trophy\Core\Json\JsonSerializableType;
6+
use Trophy\Core\Json\JsonProperty;
7+
8+
/**
9+
* A user with their streak length in the rankings.
10+
*/
11+
class StreakRankingUser extends JsonSerializableType
12+
{
13+
/**
14+
* @var string $userId The ID of the user.
15+
*/
16+
#[JsonProperty('userId')]
17+
public string $userId;
18+
19+
/**
20+
* @var ?string $name The name of the user. May be null if no name is set.
21+
*/
22+
#[JsonProperty('name')]
23+
public ?string $name;
24+
25+
/**
26+
* @var int $streakLength The user's streak length (active or longest depending on query parameter).
27+
*/
28+
#[JsonProperty('streakLength')]
29+
public int $streakLength;
30+
31+
/**
32+
* @param array{
33+
* userId: string,
34+
* name?: ?string,
35+
* streakLength: int,
36+
* } $values
37+
*/
38+
public function __construct(
39+
array $values,
40+
) {
41+
$this->userId = $values['userId'];
42+
$this->name = $values['name'] ?? null;
43+
$this->streakLength = $values['streakLength'];
44+
}
45+
46+
/**
47+
* @return string
48+
*/
49+
public function __toString(): string
50+
{
51+
return $this->toJson();
52+
}
53+
}

0 commit comments

Comments
 (0)