Skip to content

Retry any third party api call based on you custom conditions

License

Notifications You must be signed in to change notification settings

HonzaMac/conditional-retry

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Conditional retry

Conditionally retry any third party api call.

Sometimes network connection is not working properly and we would like to retry api call. Using this tiny library you can easily retry any action based on specific error condition throwed by exception or returned value.

Build Status

Usage

Reason for retry is returned via return $result. First argument is required api call, second argument is conditional callback, third argument defines how many total runs will api call do.

<?php
require __DIR__ . '/vendor/autoload.php';

retryConditional(function () {
	$result = $this->sms->send('+420800100200', 'Help, I\'m drowning!');
    return $result;
}, function ($returnValue) {
    return !$returnValue;
}, 3);

Reason for retry is returned via RuntiomeException.

<?php
require __DIR__ . '/vendor/autoload.php';

retryConditional(
    function () { 
    	// do stuff
    	throw new \RuntimeException();
    },
    function ($value, $exception) { return $exception instanceof \RuntimeException;},
    3
);

When all retries failed, last return value/exception is returned / re-throwed. For more information take a look on tests.

Requirements

  • PHP 7.0 is required but using the latest version of PHP is highly recommended

Installation

You can add this library as a local, per-project dependency to your project using Composer:

composer require honzamac/conditional-retry

Contributing

Any contributions are welcome.

About

Retry any third party api call based on you custom conditions

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages