|
4 | 4 |
|
5 | 5 | use GuzzleHttp\ClientInterface; |
6 | 6 | 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; |
9 | 9 | use Trophy\Exceptions\TrophyException; |
10 | 10 | use Trophy\Exceptions\TrophyApiException; |
11 | 11 | use Trophy\Core\Json\JsonApiRequest; |
|
15 | 15 | use JsonException; |
16 | 16 | use GuzzleHttp\Exception\RequestException; |
17 | 17 | use Psr\Http\Client\ClientExceptionInterface; |
| 18 | +use Trophy\Streaks\Requests\StreaksRankingsRequest; |
| 19 | +use Trophy\Types\StreakRankingUser; |
18 | 20 |
|
19 | 21 | class StreaksClient |
20 | 22 | { |
@@ -50,6 +52,62 @@ public function __construct( |
50 | 52 | $this->options = $options ?? []; |
51 | 53 | } |
52 | 54 |
|
| 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 | + |
53 | 111 | /** |
54 | 112 | * Get the top users by streak length (active or longest). |
55 | 113 | * |
|
0 commit comments