1. Go to this page and download the library: Download yannelli/attempt 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/ */
yannelli / attempt example snippets
use Yannelli\Attempt\Facades\Attempt;
$result = Attempt::try(fn() => $api->call())->thenReturn();
use Yannelli\Attempt\Contracts\Attemptable;
class FetchUserData implements Attemptable
{
public function handle(mixed ...$input): mixed
{
[$userId] = $input;
return Http::get("https://api.example.com/users/{$userId}")->json();
}
}
use Yannelli\Attempt\Contracts\Attemptable;
use Yannelli\Attempt\Contracts\ConfiguresAttempt;
use Yannelli\Attempt\AttemptBuilder;
class ResilientApiCall implements Attemptable, ConfiguresAttempt
{
public function configureAttempt(AttemptBuilder $attempt): void
{
$attempt
->retry(3)
->exponentialBackoff(base: 100, max: 5000)
->withJitter(0.1);
}
public function handle(mixed ...$input): mixed
{
return Http::get('https://api.example.com/data')->json();
}
}
use Yannelli\Attempt\Contracts\Fallbackable;
class ApiErrorFallback implements Fallbackable
{
public function handleFallback(Throwable $e, mixed ...$input): mixed
{
Log::warning('Using fallback due to: ' . $e->getMessage());
return Cache::get('cached_response');
}
public function shouldSkip(Throwable $e): bool
{
// Skip this fallback for certain exceptions
return $e instanceof ValidationException;
}
}
// Only execute if condition is true
Attempt::try($callable)
->when($shouldRun)
->thenReturn();
// Only execute if condition is false
Attempt::try($callable)
->unless($shouldSkip)
->thenReturn();
// With closure conditions
Attempt::try($callable)
->when(fn() => Feature::active('new-api'))
->thenReturn();
use Illuminate\Support\Facades\Pipeline;
use Yannelli\Attempt\Pipes\AttemptPipe;
$result = Pipeline::send($data)
->through([
AttemptPipe::wrap(ExternalApiCall::class)
->retry(3)
->delay([100, 500, 1000]),
ProcessResponse::class,
])
->thenReturn();
$concurrent = Attempt::concurrent([
fn() => Http::get('https://api1.example.com'),
fn() => Http::get('https://api2.example.com'),
fn() => Http::get('https://api3.example.com'),
]);
// Run all and get array of AttemptResult objects
$results = $concurrent->run();
// Get only successful results
$successful = Attempt::concurrent([...])->successful();
// Get only failed results
$failed = Attempt::concurrent([...])->failed();
// Get values directly
$values = Attempt::concurrent([...])->thenReturn();
use Laravel\Ai\Embeddings;
$embeddings = Attempt::ai(fn () => Embeddings::for($chunks)->generate())
->retry(3)
->thenReturn();
use Laravel\Ai\Contracts\Agent;
use Laravel\Ai\Contracts\HasMiddleware;
use Laravel\Ai\Promptable;
use Yannelli\Attempt\Ai\RetryAiRequests;
class SupportAgent implements Agent, HasMiddleware
{
use Promptable;
public function instructions(): string
{
return 'Answer questions about our product accurately and concisely.';
}
public function middleware(): array
{
return [
RetryAiRequests::times(2),
];
}
}
$result = Attempt::try($callable)->run();
// Check status
$result->succeeded(); // bool
$result->failed(); // bool
// Get values
$result->value(); // mixed - the result value
$result->exception(); // ?Throwable - the exception if failed
$result->attempts(); // int - number of attempts made
$result->resolvedBy(); // string - 'primary', 'retry:2', 'fallback:ClassName'