PHP code example of apisutra / laravel

1. Go to this page and download the library: Download apisutra/laravel library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

apisutra / laravel example snippets


use App\Sdk\CatalogClient;
use App\Sdk\GetProduct;

$client = app(CatalogClient::class);
$product = $client->send(new GetProduct(42))->dataOrFail();

use ApiSutra\Laravel\Pagination;

Pagination::collect($request->paginate())
    ->filter(fn ($item) => $item->active)
    ->take(100)
    ->each(fn ($item) => $repository->save($item));

use App\Sdk\CatalogClient;

$summary = app(CatalogClient::class)
    ->pool($requests, concurrency: 5)
    ->withResponseHandler($onResult)
    ->withExceptionHandler($onError)
    ->consume();

use App\Sdk\CatalogClient;
use App\Sdk\GetProduct;
use ApiSutra\Laravel\Facades\ApiSutra;
use ApiSutra\Testing\MockResponse;

$catalog = ApiSutra::for(CatalogClient::class);
$catalog->fake([GetProduct::class => MockResponse::success(['id' => 42])]);

$this->getJson('/products/42')->assertOk();
$catalog->assertSent(GetProduct::class, times: 1);

use ApiSutra\Laravel\Queue\ReleaseOnSdkThrottle;

public function middleware(): array
{
    return [new ReleaseOnSdkThrottle()];
}

use ApiSutra\Laravel\Observability\Events\ExecutionCompleted;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Log;

Event::listen(ExecutionCompleted::class, static function (ExecutionCompleted $event): void {
    Log::info('SDK operation completed', $event->snapshot->data);
});