bunq / sdk_php

PHP SDK for bunq API
MIT License
84 stars 55 forks source link

Unexpected API rate limit error #152

Closed Wouter0100 closed 6 years ago

Wouter0100 commented 6 years ago

It seems the docs are either incorrect, or the library sends more requests then asked for a specific endpoint. I've added this log (see screenshot) in the User::get method, as you can see - only 2 in a 3 second window. The third request errors.

image

Log added to method:


    /**
     * Get a specific user.
     *
     * @param int $userId
     * @param string[] $customHeaders
     *
     * @return BunqResponseUser
     */
    public static function get(array $customHeaders = []): BunqResponseUser
    {
        $apiClient = new ApiClient(static::getApiContext());
        $responseRaw = $apiClient->get(
            vsprintf(
                self::ENDPOINT_URL_READ,
                [static::determineUserId()]
            ),
            [],
            $customHeaders
        );

        logActivity('get user! ' . date('i:s'));

        return BunqResponseUser::castFromBunqResponse(
            static::fromJson($responseRaw)
        );
    }

Steps to reproduce:

  1. Call User::get() 3 times.

What should happen:

  1. Error on the 4th request.

What happens:

  1. Error on the 3th request.

Traceback

DevApp\Bunq\Isolated\bunq\Exception\TooManyRequestsException: HTTP Response Code: 429
The response id to help bunq debug: 43b1f8e0-dd31-4922-af5e-06b92130392d
Error message: Too many requests. You can do a maximum of 3 calls per 3 second to this endpoint. in /home/whmcs/domains/whmcs.dev.01d.eu/public_html/modules/addons/bunq/vendor/bunq/sdk_php/src/Exception/ExceptionFactory.php:55
Stack trace:
#0 /home/whmcs/domains/whmcs.dev.01d.eu/public_html/modules/addons/bunq/vendor/bunq/sdk_php/src/Http/Handler/ResponseHandlerError.php(44): DevApp\Bunq\Isolated\bunq\Exception\ExceptionFactory::createExceptionForResponse(Array, 429, '43b1f8e0-dd31-4...')
#1 /home/whmcs/domains/whmcs.dev.01d.eu/public_html/modules/addons/bunq/vendor/bunq/sdk_php/src/Http/Handler/HandlerUtil.php(40): DevApp\Bunq\Isolated\bunq\Http\Handler\ResponseHandlerError->execute(Object(DevApp\Bunq\Isolated\GuzzleHttp\Psr7\Response))
#2 /home/whmcs/domains/whmcs.dev.01d.eu/public_html/modules/addons/bunq/vendor/guzzlehttp/promises/src/FulfilledPromise.php(33): DevApp\Bunq\Isolated\bunq\Http\Handler\HandlerUtil::DevApp\Bunq\Isolated\bunq\Http\Handler\{closure}(Object(DevApp\Bunq\Isolated\GuzzleHttp\Psr7\Response))
#3 /home/whmcs/domains/whmcs.dev.01d.eu/public_html/modules/addons/bunq/vendor/guzzlehttp/promises/src/TaskQueue.php(44): DevApp\Bunq\Isolated\GuzzleHttp\Promise\FulfilledPromise::DevApp\Bunq\Isolated\GuzzleHttp\Promise\{closure}()
#4 /home/whmcs/domains/whmcs.dev.01d.eu/public_html/modules/addons/bunq/vendor/guzzlehttp/promises/src/Promise.php(200): DevApp\Bunq\Isolated\GuzzleHttp\Promise\TaskQueue->run(true)
#5 /home/whmcs/domains/whmcs.dev.01d.eu/public_html/modules/addons/bunq/vendor/guzzlehttp/promises/src/Promise.php(183): DevApp\Bunq\Isolated\GuzzleHttp\Promise\Promise->invokeWaitFn()
#6 /home/whmcs/domains/whmcs.dev.01d.eu/public_html/modules/addons/bunq/vendor/guzzlehttp/promises/src/Promise.php(219): DevApp\Bunq\Isolated\GuzzleHttp\Promise\Promise->waitIfPending()
#7 /home/whmcs/domains/whmcs.dev.01d.eu/public_html/modules/addons/bunq/vendor/guzzlehttp/promises/src/Promise.php(185): DevApp\Bunq\Isolated\GuzzleHttp\Promise\Promise->invokeWaitList()
#8 /home/whmcs/domains/whmcs.dev.01d.eu/public_html/modules/addons/bunq/vendor/guzzlehttp/promises/src/Promise.php(51): DevApp\Bunq\Isolated\GuzzleHttp\Promise\Promise->waitIfPending()
#9 /home/whmcs/domains/whmcs.dev.01d.eu/public_html/modules/addons/bunq/vendor/guzzlehttp/guzzle/src/Client.php(114): DevApp\Bunq\Isolated\GuzzleHttp\Promise\Promise->wait()
#10 /home/whmcs/domains/whmcs.dev.01d.eu/public_html/modules/addons/bunq/vendor/bunq/sdk_php/src/Http/ApiClient.php(171): DevApp\Bunq\Isolated\GuzzleHttp\Client->request('GET', Object(DevApp\Bunq\Isolated\GuzzleHttp\Psr7\Uri), Array)
#11 /home/whmcs/domains/whmcs.dev.01d.eu/public_html/modules/addons/bunq/vendor/bunq/sdk_php/src/Http/ApiClient.php(156): DevApp\Bunq\Isolated\bunq\Http\ApiClient->request('GET', 'user', Array, Array, Array)
#12 /home/whmcs/domains/whmcs.dev.01d.eu/public_html/modules/addons/bunq/vendor/bunq/sdk_php/src/Model/Generated/Endpoint/User.php(71): DevApp\Bunq\Isolated\bunq\Http\ApiClient->get('user', Array, Array)
#13 /home/whmcs/domains/whmcs.dev.01d.eu/public_html/modules/addons/bunq/vendor/bunq/sdk_php/src/Context/UserContext.php(49): DevApp\Bunq\Isolated\bunq\Model\Generated\Endpoint\User::listing()
#14 /home/whmcs/domains/whmcs.dev.01d.eu/public_html/modules/addons/bunq/vendor/bunq/sdk_php/src/Context/BunqContext.php(32): DevApp\Bunq\Isolated\bunq\Context\UserContext->__construct(406)
#15 /home/whmcs/domains/whmcs.dev.01d.eu/public_html/modules/addons/bunq/Models/Bunq.php(38): DevApp\Bunq\Isolated\bunq\Context\BunqContext::loadApiContext(Object(DevApp\Bunq\Isolated\bunq\Context\ApiContext))
#16 /home/whmcs/domains/whmcs.dev.01d.eu/public_html/modules/addons/bunq/Controllers/Config.php(30): DevApp\Bunq\Isolated\DevApp\WHMCS\Bunq\Models\Bunq->loadContext()
#17 /home/whmcs/domains/whmcs.dev.01d.eu/public_html/modules/addons/bunq/bunq.php(11): DevApp\Bunq\Isolated\DevApp\WHMCS\Bunq\Controllers\Config->options()
#18 /home/whmcs/domains/whmcs.dev.01d.eu/public_html/admin/configaddonmods.php(0): bunq_config()
#19 {main}

SDK version and environment

OGKevin commented 6 years ago

I have a feeling that you are setting up to much.

Are you re-creating, re-loading the api context every single time before you do a call ?

Also, you can get the user by doing https://github.com/bunq/tinker_php/blob/b786ac4496916f2c601d115f02612e1e288d277b/src/BunqLib.php#L150-L159

Wouter0100 commented 6 years ago

Hmm, you were right there. Didn't know the context also loaded the user.