From 1cd564d014583bae4839a41d786568a7c7de8565 Mon Sep 17 00:00:00 2001 From: kawax Date: Mon, 11 Mar 2024 17:48:57 +0900 Subject: [PATCH] Change return type to Response --- docs/bluesky-client.md | 13 +++++++------ src/BlueskyClient.php | 28 ++++++++++------------------ src/{ => Enums}/AtProto.php | 2 +- src/Facades/Bluesky.php | 8 ++++---- src/Notifications/BlueskyChannel.php | 7 ++++++- tests/Feature/Client/ClientTest.php | 6 +++--- 6 files changed, 31 insertions(+), 33 deletions(-) rename src/{ => Enums}/AtProto.php (88%) diff --git a/docs/bluesky-client.md b/docs/bluesky-client.md index 5d835f14..d962377b 100644 --- a/docs/bluesky-client.md +++ b/docs/bluesky-client.md @@ -9,22 +9,23 @@ Only my posts and reposts. ```php use Revolution\Bluesky\Facades\Bluesky; -/** @var \Illuminate\Support\Collection $response */ +/** @var \Illuminate\Http\Client\Response $response */ $response = Bluesky::login(identifier: config('bluesky.identifier'), password: config('bluesky.password')) ->feed(); -dump($response); +dump($response->json()); ``` ## Viewing my timeline ```php use Revolution\Bluesky\Facades\Bluesky; -/** @var \Illuminate\Support\Collection $response */ +/** @var \Illuminate\Http\Client\Response $response */ $response = Bluesky::login(identifier: config('bluesky.identifier'), password: config('bluesky.password')) - ->timeline(); + ->timeline() + ->throw(); -dump($response); +dump($response->collect()); ``` ## Creating a post @@ -32,7 +33,7 @@ dump($response); ```php use Revolution\Bluesky\Facades\Bluesky; -/** @var \Illuminate\Support\Collection $response */ +/** @var \Illuminate\Http\Client\Response $response */ Bluesky::login(identifier: config('bluesky.identifier'), password: config('bluesky.password')) ->post('test'); diff --git a/src/BlueskyClient.php b/src/BlueskyClient.php index 05423ee7..bb1d728f 100644 --- a/src/BlueskyClient.php +++ b/src/BlueskyClient.php @@ -5,10 +5,12 @@ namespace Revolution\Bluesky; use Illuminate\Http\Client\RequestException; +use Illuminate\Http\Client\Response; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Http; use Illuminate\Support\Traits\Macroable; use Revolution\Bluesky\Contracts\Factory; +use Revolution\Bluesky\Enums\AtProto; class BlueskyClient implements Factory { @@ -51,41 +53,33 @@ public function login(string $identifier, string $password): static /** * My feed. - * - * @throws RequestException */ - public function feed(string $filter = 'posts_with_replies'): Collection + public function feed(string $filter = 'posts_with_replies'): Response { return Http::baseUrl($this->baseUrl()) ->withToken($this->session('accessJwt')) ->get(AtProto::getAuthorFeed->value, [ 'actor' => $this->session('did'), 'filter' => $filter, - ]) - ->throw() - ->collect(); + ]); } /** * My timeline. - * - * @throws RequestException - */ - public function timeline(string $cursor = ''): Collection + */ + public function timeline(string $cursor = ''): Response { return Http::baseUrl($this->baseUrl()) ->withToken($this->session('accessJwt')) ->get(AtProto::getTimeline->value, [ 'cursor' => $cursor, - ]) - ->throw() - ->collect(); + ]); } /** - * @throws RequestException + * Create new post. */ - public function post(string $text): Collection + public function post(string $text): Response { return Http::baseUrl($this->baseUrl()) ->withToken($this->session('accessJwt')) @@ -96,9 +90,7 @@ public function post(string $text): Collection 'text' => $text, 'createdAt' => now()->toISOString(), ] - ]) - ->throw() - ->collect(); + ]); } private function baseUrl(): string diff --git a/src/AtProto.php b/src/Enums/AtProto.php similarity index 88% rename from src/AtProto.php rename to src/Enums/AtProto.php index 2473e72e..a85a08cd 100644 --- a/src/AtProto.php +++ b/src/Enums/AtProto.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Revolution\Bluesky; +namespace Revolution\Bluesky\Enums; enum AtProto: string { diff --git a/src/Facades/Bluesky.php b/src/Facades/Bluesky.php index 959cdeb6..0a57518a 100644 --- a/src/Facades/Bluesky.php +++ b/src/Facades/Bluesky.php @@ -4,7 +4,7 @@ namespace Revolution\Bluesky\Facades; -use Illuminate\Support\Collection; +use Illuminate\Http\Client\Response; use Illuminate\Support\Facades\Facade; use Revolution\Bluesky\Contracts\Factory; @@ -12,9 +12,9 @@ * @method static static service(string $service) * @method static mixed session(string $key) * @method static static login(string $identifier, string $password) - * @method static Collection feed(string $filter = 'posts_with_replies') - * @method static Collection timeline(string $cursor = '') - * @method static Collection post(string $text) + * @method static Response feed(string $filter = 'posts_with_replies') + * @method static Response timeline(string $cursor = '') + * @method static Response post(string $text) */ class Bluesky extends Facade { diff --git a/src/Notifications/BlueskyChannel.php b/src/Notifications/BlueskyChannel.php index 9f87a74b..22777b19 100644 --- a/src/Notifications/BlueskyChannel.php +++ b/src/Notifications/BlueskyChannel.php @@ -4,11 +4,15 @@ namespace Revolution\Bluesky\Notifications; +use Illuminate\Http\Client\RequestException; use Illuminate\Notifications\Notification; use Revolution\Bluesky\Facades\Bluesky; class BlueskyChannel { + /** + * @throws RequestException + */ public function send(mixed $notifiable, Notification $notification): void { /** @var BlueskyMessage $message */ @@ -27,6 +31,7 @@ public function send(mixed $notifiable, Notification $notification): void Bluesky::service($route->service) ->login($route->identifier, $route->password) - ->post($message->text); + ->post($message->text) + ->throw(); } } diff --git a/tests/Feature/Client/ClientTest.php b/tests/Feature/Client/ClientTest.php index a510c1f6..c66c92a1 100644 --- a/tests/Feature/Client/ClientTest.php +++ b/tests/Feature/Client/ClientTest.php @@ -50,7 +50,7 @@ public function test_feed() $response = Bluesky::login(identifier: 'identifier', password: 'password') ->feed(filter: 'posts_with_replies'); - $this->assertTrue($response->has('feed')); + $this->assertTrue($response->collect()->has('feed')); } public function test_timeline() @@ -62,7 +62,7 @@ public function test_timeline() $response = Bluesky::login(identifier: 'identifier', password: 'password') ->timeline(cursor: '1'); - $this->assertTrue($response->has('feed')); + $this->assertTrue($response->collect()->has('feed')); } public function test_post() @@ -74,6 +74,6 @@ public function test_post() $response = Bluesky::login(identifier: 'identifier', password: 'password') ->post(text: 'test'); - $this->assertTrue($response->has('uri')); + $this->assertTrue($response->collect()->has('uri')); } }