generated from cerbero90/skeleton
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrate Laravel HTTP client events
- Loading branch information
Showing
5 changed files
with
152 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cerbero\LazyJsonPages\Providers; | ||
|
||
use Cerbero\LazyJsonPages\Services\Client; | ||
use GuzzleHttp\Middleware; | ||
use GuzzleHttp\Promise\PromiseInterface; | ||
use Illuminate\Http\Client\Events\ConnectionFailed; | ||
use Illuminate\Http\Client\Events\RequestSending; | ||
use Illuminate\Http\Client\Events\ResponseReceived; | ||
use Illuminate\Http\Client\Request; | ||
use Illuminate\Http\Client\Response; | ||
use Illuminate\Support\ServiceProvider; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
/** | ||
* The service provider to integrate with Laravel. | ||
*/ | ||
final class LazyJsonPagesServiceProvider extends ServiceProvider | ||
{ | ||
/** | ||
* Bootstrap the services. | ||
*/ | ||
public function boot(): void | ||
{ | ||
Client::middleware('laravel_events', Middleware::tap($this->sending(...), $this->sent(...))); | ||
} | ||
|
||
/** | ||
* Handle HTTP requests before they are sent. | ||
*/ | ||
private function sending(RequestInterface $request): void | ||
{ | ||
event(new RequestSending(new Request($request))); | ||
} | ||
|
||
/** | ||
* Handle HTTP requests after they are sent. | ||
*/ | ||
private function sent(RequestInterface $request, array $options, PromiseInterface $promise): void | ||
{ | ||
$clientRequest = new Request($request); | ||
|
||
$promise->then( | ||
fn(ResponseInterface $response) => event(new ResponseReceived($clientRequest, new Response($response))), | ||
fn() => event(new ConnectionFailed($clientRequest)), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
use Cerbero\LazyJsonPages\LazyJsonPages; | ||
use Illuminate\Http\Client\Events\ConnectionFailed; | ||
use Illuminate\Http\Client\Events\RequestSending; | ||
use Illuminate\Http\Client\Events\ResponseReceived; | ||
use Illuminate\Support\Facades\Event; | ||
|
||
it('fires Laravel HTTP client events on success', function() { | ||
Event::fake(); | ||
|
||
$lazyCollection = LazyJsonPages::from('https://example.com/api/v1/users') | ||
->totalPages('meta.total_pages') | ||
->collect('data.*'); | ||
|
||
expect($lazyCollection)->toLoadItemsViaRequests([ | ||
'https://example.com/api/v1/users' => 'pagination/page1.json', | ||
'https://example.com/api/v1/users?page=2' => 'pagination/page2.json', | ||
'https://example.com/api/v1/users?page=3' => 'pagination/page3.json', | ||
]); | ||
|
||
Event::assertDispatched(RequestSending::class, 3); | ||
Event::assertDispatched(ResponseReceived::class, 3); | ||
}); | ||
|
||
it('fires Laravel HTTP client events on failure', function() { | ||
Event::fake(); | ||
|
||
$lazyCollection = LazyJsonPages::from('https://example.com/api/v1/users') | ||
->totalPages('meta.total_pages') | ||
->collect('data.*'); | ||
|
||
expect($lazyCollection)->toFailRequest('https://example.com/api/v1/users'); | ||
|
||
Event::assertDispatched(RequestSending::class, 1); | ||
Event::assertDispatched(ConnectionFailed::class, 1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters