Thanks to visit codestin.com
Credit goes to github.com

Skip to content

sailthru/sailthru-php5-client

 
 

Repository files navigation

sailthru-php5-client

Build Status Coverage Status

A simple client library to remotely access the Sailthru REST API. By default, it will make requests in JSON format.

Documentation

Installation

You can clone via GitHub or install via composer.

git clone [email protected]:sailthru/sailthru-php5-client.git

or

composer require sailthru/sailthru-php5-client

Usage

Default initialization

For basic usage, you can initialize with just API Key and Secret

    $client = new Sailthru_Client($api_key, $api_secret);

Exception Handling

As of 2.0.0, the client library will throw a Sailthru_Client_Exception on API and IO errors, which should be properly handled.

Error codes 1000, 1001, 10002 are IO-related, while 0-99 and XX are API errors.

try { 
    $client->apiPost('user', [..]);
} catch (Sailthru_Client_Exception $e) {
    $code = $e->getCode();
    $message = $->getMessage();
    // process error
}

Optional parameters for connection/read timeout settings

Increase timeout from 10 (default) to 30 seconds.

$client = new Sailthru_Client($this->api_key, $this->secret, $this->api_url, array('timeout' => 30000, 'connect_timeout' => 30000));

API Rate Limiting

Below shows an example of how to check for Sailthru API rate limiting and throttle requests based on that information.

// get last rate limit info
$rate_limit_info = $sailthru_client->getLastRateLimitInfo("user", "POST");

// getRateLimitInfo returns null if given endpoint/method wasn't triggered previously
if ($rate_limit_info) {
    $limit = $rate_limit_info['limit'];
    $remaining = $rate_limit_info['remaining'];
    $reset_timestamp = $rate_limit_info['reset'];

    // throttle api calls based on last rate limit info
    if ($remaining <= 0) {
        $seconds_till_reset = $reset_timestamp - time();

        // sleep or perform other business logic before next user api call
        sleep($seconds_till_reset);
    }
}

Tests

You can run the tests locally with composer and phpunit:

cd sailthru-php5-client
composer install
vendor/bin/phpunit

About

Sailthru PHP5 Client

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 31

Languages