Skip to content

Commit dd6c09b

Browse files
Automatically update PHP SDK
1 parent 77bbf19 commit dd6c09b

4 files changed

Lines changed: 135 additions & 3 deletions

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.20",
3+
"version": "1.0.21",
44
"description": "Trophy PHP Library",
55
"keywords": [
66
"trophy",
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Trophy\Streaks\Requests;
4+
5+
use Trophy\Core\Json\JsonSerializableType;
6+
7+
class StreaksListRequest extends JsonSerializableType
8+
{
9+
/**
10+
* @var array<?string> $userIds A list of up to 100 user IDs.
11+
*/
12+
public array $userIds;
13+
14+
/**
15+
* @param array{
16+
* userIds: array<?string>,
17+
* } $values
18+
*/
19+
public function __construct(
20+
array $values,
21+
) {
22+
$this->userIds = $values['userIds'];
23+
}
24+
}

src/Streaks/StreaksClient.php

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
use GuzzleHttp\ClientInterface;
66
use Trophy\Core\Client\RawClient;
7-
use Trophy\Streaks\Requests\StreaksRankingsRequest;
8-
use Trophy\Types\StreakRankingUser;
7+
use Trophy\Streaks\Requests\StreaksListRequest;
8+
use Trophy\Types\BulkStreakResponseItem;
99
use Trophy\Exceptions\TrophyException;
1010
use Trophy\Exceptions\TrophyApiException;
1111
use Trophy\Core\Json\JsonApiRequest;
@@ -15,6 +15,8 @@
1515
use JsonException;
1616
use GuzzleHttp\Exception\RequestException;
1717
use Psr\Http\Client\ClientExceptionInterface;
18+
use Trophy\Streaks\Requests\StreaksRankingsRequest;
19+
use Trophy\Types\StreakRankingUser;
1820

1921
class StreaksClient
2022
{
@@ -50,6 +52,62 @@ public function __construct(
5052
$this->options = $options ?? [];
5153
}
5254

55+
/**
56+
* Get the streak lengths of a list of users, ranked by streak length from longest to shortest.
57+
*
58+
* @param StreaksListRequest $request
59+
* @param ?array{
60+
* baseUrl?: string,
61+
* maxRetries?: int,
62+
* } $options
63+
* @return array<BulkStreakResponseItem>
64+
* @throws TrophyException
65+
* @throws TrophyApiException
66+
*/
67+
public function list(StreaksListRequest $request, ?array $options = null): array
68+
{
69+
$options = array_merge($this->options, $options ?? []);
70+
$query = [];
71+
if ($request->userIds != null) {
72+
$query['userIds'] = $request->userIds;
73+
}
74+
try {
75+
$response = $this->client->sendRequest(
76+
new JsonApiRequest(
77+
baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Default_->value,
78+
path: "streaks",
79+
method: HttpMethod::GET,
80+
query: $query,
81+
),
82+
$options,
83+
);
84+
$statusCode = $response->getStatusCode();
85+
if ($statusCode >= 200 && $statusCode < 400) {
86+
$json = $response->getBody()->getContents();
87+
return JsonDecoder::decodeArray($json, [BulkStreakResponseItem::class]); // @phpstan-ignore-line
88+
}
89+
} catch (JsonException $e) {
90+
throw new TrophyException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e);
91+
} catch (RequestException $e) {
92+
$response = $e->getResponse();
93+
if ($response === null) {
94+
throw new TrophyException(message: $e->getMessage(), previous: $e);
95+
}
96+
throw new TrophyApiException(
97+
message: "API request failed",
98+
statusCode: $response->getStatusCode(),
99+
body: $response->getBody()->getContents(),
100+
);
101+
} catch (ClientExceptionInterface $e) {
102+
throw new TrophyException(message: $e->getMessage(), previous: $e);
103+
}
104+
throw new TrophyApiException(
105+
message: 'API request failed',
106+
statusCode: $statusCode,
107+
body: $response->getBody()->getContents(),
108+
);
109+
}
110+
53111
/**
54112
* Get the top users by streak length (active or longest).
55113
*
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Trophy\Types;
4+
5+
use Trophy\Core\Json\JsonSerializableType;
6+
use Trophy\Core\Json\JsonProperty;
7+
8+
class BulkStreakResponseItem extends JsonSerializableType
9+
{
10+
/**
11+
* @var string $userId The ID of the user.
12+
*/
13+
#[JsonProperty('userId')]
14+
public string $userId;
15+
16+
/**
17+
* @var int $streakLength The length of the user's streak.
18+
*/
19+
#[JsonProperty('streakLength')]
20+
public int $streakLength;
21+
22+
/**
23+
* @var ?string $extended The timestamp the streak was extended, as a string.
24+
*/
25+
#[JsonProperty('extended')]
26+
public ?string $extended;
27+
28+
/**
29+
* @param array{
30+
* userId: string,
31+
* streakLength: int,
32+
* extended?: ?string,
33+
* } $values
34+
*/
35+
public function __construct(
36+
array $values,
37+
) {
38+
$this->userId = $values['userId'];
39+
$this->streakLength = $values['streakLength'];
40+
$this->extended = $values['extended'] ?? null;
41+
}
42+
43+
/**
44+
* @return string
45+
*/
46+
public function __toString(): string
47+
{
48+
return $this->toJson();
49+
}
50+
}

0 commit comments

Comments
 (0)