Skip to content

habibtalib/laravel-guzzle-throttle

Repository files navigation

hamburgscleanest/laravel-guzzle-throttle

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

A Laravel (5.6 upwards) wrapper for Guzzle Advanced Throttle.

Install

Via Composer

$ composer require hamburgscleanest/laravel-guzzle-throttle

Automatic Package Discovery

Everything is automatically registered for you.


Configuration

Publish the config to get the example configuration.

$ php artisan vendor:publish

Example configuration

20 requests every 1 seconds

100 requests every 2 minutes

    return [
        'cache' => [
            // Name of the configured driver in the Laravel cache config file / Also needs to be set when "no-cache" is set! Because it's used for the internal timers
            'driver'   => 'default',
            // Cache strategy: no-cache, cache, force-cache
            'strategy' => 'cache',
            // TTL in minutes
            'ttl'      => 900
        ],
        'rules' => [
            [
                // host (including scheme)
                'host'             => 'https://www.google.com',
                // maximum number of requests in the given interval
                'max_requests'     => 20,
                // interval in seconds till the limit is reset
                'request_interval' => 1
            ],
            [
                // host (including scheme)
                'host'             => 'https://www.google.com',
                // maximum number of requests in the given interval
                'max_requests'     => 100,
                // interval in seconds till the limit is reset
                'request_interval' => 120
            ]
        ]
    ];

Further details

If you want to know more about the possible configurations, head over to the middleware repository: Guzzle Advanced Throttle.


Usage

Using the preconfigured client is super easy. You just have to instantiate your client like this:

// returns an instance of GuzzleHttp\Client
$client = GuzzleThrottle::client(['base_uri' => 'https://www.google.com']);

After that you can use all off the usual GuzzleHttp\Client methods, e.g.

$client->get('/test'));

Changes

Please see CHANGELOG for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email chroma91@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.