Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RATE LIMITING HEADERS #93

Open
AnonDev1312 opened this issue Mar 9, 2018 · 1 comment
Open

RATE LIMITING HEADERS #93

AnonDev1312 opened this issue Mar 9, 2018 · 1 comment

Comments

@AnonDev1312
Copy link

Using this library how can I get the rate limiting headers

x_ratelimit_api_followers_limit
x_ratelimit_api_followers_remaining
x_ratelimit_api_followers_reset

thanks

@jasonpenny
Copy link
Contributor

It doesn't look like there's a built-in way to get these values, but you can get them by overriding the requestHandler

<?php
require_once 'vendor/autoload.php';

class RequestHandlerInterceptor extends Tumblr\API\RequestHandler {
    public $rateLimits;

    public function request($method, $path, $options) {
        $result = parent::request($method, $path, $options);

        $this->rateLimits = array();
        foreach ($result->headers as $key => $value) {
            if (substr($key, 0, 6) === 'X-Rate') {
                $this->rateLimits[$key] = $value[0];
            }
        }

        return $result;
    }
}

list($consumerKey, $consumerSecret, $token, $secret) = array(
    '...',
    '...',
    '...',
    '...',
);
$client = new Tumblr\API\Client($consumerKey, $consumerSecret, $token, $secret);
$client->requestHandler = new RequestHandlerInterceptor();
$client->requestHandler->setConsumer($consumerKey, $consumerSecret);
$client->requestHandler->setToken($token, $secret);

$unused = $client->getFollowedBlogs();
//var_dump($unused);
//echo "\n";

echo "---------------------------------------\n";
printf(
    "API Requests Daily Limit:                   %s\n",
    $client->requestHandler->rateLimits['X-Ratelimit-Perday-Limit']
);
printf(
    "API Requests Remaining Today:               %s\n",
    $client->requestHandler->rateLimits['X-Ratelimit-Perday-Remaining']
);
echo "---------------------------------------\n";
printf(
    "API Requests Hourly Limit:                  %s\n",
    $client->requestHandler->rateLimits['X-Ratelimit-Perday-Limit']
);
printf(
    "API Requests Remaining This Hour:           %s\n",
    $client->requestHandler->rateLimits['X-Ratelimit-Perday-Remaining']
);
echo "---------------------------------------\n";
printf(
    "Following API Requests Hourly Limit:        %s\n",
    $client->requestHandler->rateLimits['X-Ratelimit-Api-Following-Limit']
);
printf(
    "Following API Requests Remaining This Hour: %s\n",
    $client->requestHandler->rateLimits['X-Ratelimit-Api-Following-Remaining']
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants