guzzlehttp/command
Build higher-level web service clients on top of Guzzle by modeling operations as Commands and responses as Results. Includes a generic ServiceClient plus command middleware to map commands to PSR-7 requests and responses to structured results.
illuminate/http uses PSR-7 under the hood).ServiceClient can be instantiated in a Laravel Service Provider, injected via the container, and configured with Guzzle’s HTTP client (already used in Laravel for HTTP requests).guzzlehttp/guzzle (already in Laravel’s vendor), spatie/laravel-activitylog (for auditing), or spatie/laravel-queue-priority (for async commands).executeAsync() and executeAllAsync() are powerful, Laravel’s queue system (e.g., Illuminate\Queue) might overlap, requiring careful design to avoid redundancy.App\Exceptions\Handler) must be extended to map CommandException to Laravel’s HttpResponseException or custom exceptions.executeAll) could strain Laravel’s event loop if not throttled (e.g., via concurrency option).kernel.php) can complement this.HttpClient configurations?Illuminate\Support\Retry or queue retries could integrate.CommandException be translated to Laravel’s ProblemException or custom DTOs?CommandInterface/ResultInterface work in Laravel’s testing tools (e.g., Mockery, Pest)?executeAll) require Laravel’s queue workers or a separate process (e.g., reactphp)?Http facade with ServiceClient for API-heavy applications. The underlying GuzzleHttp\Client is already used by Laravel’s Http client.ServiceClient as a singleton or context-bound instance in AppServiceProvider:
$this->app->singleton('api.client', function ($app) {
return new ServiceClient(
$app->make(GuzzleHttp\Client::class),
// Command-to-Request transformer...
// Response-to-Result transformer...
);
});
$client->executeAsync($command)->then(function ($result) {
dispatch(new ProcessResultJob($result));
});
ServiceClient middleware for command-level logic (e.g., input validation).ServiceClient.Http::post() calls with structured commands.// Before
$response = Http::post('api/users', ['name' => 'John']);
// After
$result = $client->createUser(['name' => 'John']);
Http client to Guzzle’s HandlerStack.executeAsync for long-running operations.dispatchSync for testing async flows.ServiceClient.guzzlehttp/guzzle and guzzlehttp/promises (used by Laravel’s queue system).illuminate/http uses PSR-7 under the hood, so no conflicts.User::create($result->toArray())).guzzlehttp/guzzle and guzzlehttp/promises are pinned to compatible versions (e.g., ^7.10).GuzzleHttp\Command\* to composer.json:
composer require guzzlehttp/command
ServiceClient in a service provider.getUser).createOrder).assertEquals($result['id'], 123)).CommandInterface in unit tests:
$mockCommand = Mockery::mock(CommandInterface::class);
$mockCommand->shouldReceive('getName')->andReturn('createUser');
debugbar to inspect request/response cycles.CommandInterface, ResultInterface) improve IDE support.ServiceClient adjustments (e.g., PSR-7 changes).tap() or dd() to inspect commands/results:
$result = $client->createUser(['name' => 'John'])->tap(function ($result) {
Log::debug('Command result:', $result->toArray());
});
HandlerStack.@http in user input (mitigate via input validation).csrf_token().executeAll concurrency to avoid overwhelming the API or Laravel’s workers.concurrency: 5 for external APIs with rate limits.Cache::remember) for idempotent operations.executeAll for bulk operations (e.g., syncing users).How can I help you explore Laravel packages today?