Skip to content

Commit

Permalink
Merge pull request #21 from boryn/api-limits
Browse files Browse the repository at this point in the history
Getting API limits from response headers
  • Loading branch information
RichieMcMullen authored Feb 21, 2021
2 parents 6701b3a + 9fabef1 commit 9a60c7f
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 39 deletions.
55 changes: 53 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -396,11 +396,62 @@ List of the authenticated athlete's starred segments.
Strava::starredSegments($token, $page, $perPage);
```

## Getting API Limits

Strava returns information about API calls allowance and usage in response headers.

The methods listed below will return this information upon a call which uses up the API limit (like fetching activities). Some calls like refreshing access tokens seem not to use up the API call limit, that's why you will get nulls in the resulting array.

As well when you try to get the limits at the very beginning, before any API call using up the limits , you will receive nulls. The default allowance limits are not hardcoded as different accounts may have different limits.

#### All API Limits
Returns all limits in a multidimensional array, eg.:

```php
[
['allowance']['15minutes'] => "100",
['allowance']['daily'] => "1000",
['usage']['15minutes'] => "7",
['usage']['daily'] => "352",
]
```

```php
Strava::getApiLimits();
```
#### Allocated API Limits
Returns daily and 15-minute request limits available for the Strava account , eg.:

```php
[
['15minutes'] => '100',
['daily'] => '1000',
]
```

```php
Strava::getApiAllowanceLimits();
```

#### Used API Calls
Returns number of daily and 15-minute calls used up at the Strava account , eg.:

```php
[
['15minutes'] => '7',
['daily'] => '352',
]
```

```php
Strava::getApiUsageLimits();
```


## Parameter Types

```php
$token = String
$token = string
$activityID = integer
$athleteID = integer
$clubID = integer
Expand All @@ -419,7 +470,7 @@ It's highly recommended that you cache your requests made to Strava for 2 reason

#### (1) Rate Limiting

Strava have quite a good API Rate Limit, 600 requests every 15 minutes, 30,000 daily. If your website has high traffic you might want to consider caching your Strava response data so you don't exceed these limits.
Strava have API Rate Limit of 100 requests every 15 minutes and 10,000 daily. If your website has high traffic you might want to consider caching your Strava response data so you don't exceed these limits.

#### (2) Website Loading Speed

Expand Down
101 changes: 73 additions & 28 deletions src/Strava.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class Strava
private $client_secret;
private $redirect_uri;

private $api_limits = [];
private const HEADER_API_ALLOWANCE = 'X-RateLimit-Limit';
private const HEADER_API_USAGE = 'X-RateLimit-Usage';


#
# Constructor
Expand All @@ -34,7 +38,7 @@ public function __construct($CLIENT_ID, $CLIENT_SECRET, $REDIRECT_URI, $GUZZLE_C
#
public function authenticate($scope='read_all,profile:read_all,activity:read_all')
{
return redirect('https://www.strava.com/oauth/authorize?client_id='. $this->client_id .'&response_type=code&redirect_uri='. $this->redirect_uri . '&scope=' . $scope . '&state=strava');
return redirect('https://www.strava.com/oauth/authorize?client_id='. $this->client_id .'&response_type=code&redirect_uri='. $this->redirect_uri . '&scope=' . $scope . '&state=strava');
}


Expand All @@ -43,14 +47,14 @@ public function authenticate($scope='read_all,profile:read_all,activity:read_all
#
public function unauthenticate($token)
{
$url = 'https://www.strava.com/oauth/deauthorize';
$config = [
'form_params' => [
'access_token' => $token
]
];
$res = $this->post($url, $config);
return $res;
$url = 'https://www.strava.com/oauth/deauthorize';
$config = [
'form_params' => [
'access_token' => $token
]
];
$res = $this->post($url, $config);
return $res;
}


Expand All @@ -61,12 +65,12 @@ public function token($code)
{
$url = 'https://www.strava.com/oauth/token';
$config = [
'form_params' => [
'client_id' => $this->client_id,
'client_secret' => $this->client_secret,
'code' => $code,
'grant_type' => 'authorization_code'
]
'form_params' => [
'client_id' => $this->client_id,
'client_secret' => $this->client_secret,
'code' => $code,
'grant_type' => 'authorization_code'
]
];
$res = $this->post($url, $config);
return $res;
Expand All @@ -80,12 +84,12 @@ public function refreshToken($refreshToken)
{
$url = 'https://www.strava.com/oauth/token';
$config = [
'form_params' => [
'client_id' => $this->client_id,
'client_secret' => $this->client_secret,
'refresh_token' => $refreshToken,
'grant_type' => 'refresh_token'
]
'form_params' => [
'client_id' => $this->client_id,
'client_secret' => $this->client_secret,
'refresh_token' => $refreshToken,
'grant_type' => 'refresh_token'
]
];
$res = $this->post($url, $config);
return $res;
Expand Down Expand Up @@ -144,7 +148,7 @@ public function activityStream($token, $activityID, $keys = '', $keyByType = tru
{
if ($keys != '')
$keys = join(",", $keys);

$url = $this->strava_uri . '/activities/'. $activityID .'/streams?keys='. $keys .'&key_by_type'. $keyByType;
$config = $this->bearer($token);
$res = $this->get($url, $config);
Expand Down Expand Up @@ -362,6 +366,7 @@ public function starredSegments($token, $page = 1, $perPage = 10)
public function post($url, $config)
{
$res = $this->client->post( $url, $config );
$this->parseApiLimits($res->getHeader(self::HEADER_API_ALLOWANCE), $res->getHeader(self::HEADER_API_USAGE));
$result = json_decode($res->getBody()->getContents());
return $result;
}
Expand All @@ -373,6 +378,7 @@ public function post($url, $config)
public function get($url, $config)
{
$res = $this->client->get( $url, $config );
$this->parseApiLimits($res->getHeader(self::HEADER_API_ALLOWANCE), $res->getHeader(self::HEADER_API_USAGE));
$result = json_decode($res->getBody()->getContents());
return $result;
}
Expand All @@ -383,13 +389,52 @@ public function get($url, $config)
#
private function bearer($token)
{
$config = [
'headers' => [
'Authorization' => 'Bearer '.$token.''
],
];
return $config;
$config = [
'headers' => [
'Authorization' => 'Bearer '.$token.''
],
];
return $config;
}


#
# Return API limits
#
public function getApiLimits()
{
return $this->api_limits;
}

public function getApiAllowanceLimits()
{
return $this->api_limits['allowance'];
}

public function getApiUsageLimits()
{
return $this->api_limits['usage'];
}

private function parseApiLimits($allowance, $usage)
{
if (isset($allowance[0])) {
$allowance = explode(',', $allowance[0]);

$this->api_limits['allowance']['15minutes'] = isset($allowance[0]) ? trim($allowance[0]) : null;
$this->api_limits['allowance']['daily'] = isset($allowance[1]) ? trim($allowance[1]) : null;
} else {
$this->api_limits['allowance']['15minutes'] = null;
$this->api_limits['allowance']['daily'] = null;
}

if (isset($usage[0])) {
$usage = explode(',', $usage[0]);
$this->api_limits['usage']['15minutes'] = isset($usage[0]) ? trim($usage[0]) : null;
$this->api_limits['usage']['daily'] = isset($usage[1]) ? trim($usage[1]) : null;
} else {
$this->api_limits['usage']['15minutes'] = null;
$this->api_limits['usage']['daily'] = null;
}
}
}
12 changes: 6 additions & 6 deletions src/StravaServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ class StravaServiceProvider extends ServiceProvider
public function boot()
{
$this->mergeConfigFrom(
__DIR__ . '/config/strava.php', 'ct_strava'
__DIR__ . '/config/strava.php', 'ct_strava'
);

$this->publishes([
__DIR__ . '/config/strava.php' => config_path('ct_strava.php')
__DIR__ . '/config/strava.php' => config_path('ct_strava.php')
]);
}

Expand All @@ -39,10 +39,10 @@ public function register()
$client = new Client();

return new Strava(
config('ct_strava.client_id'),
config('ct_strava.client_secret'),
config('ct_strava.redirect_uri'),
$client
config('ct_strava.client_id'),
config('ct_strava.client_secret'),
config('ct_strava.redirect_uri'),
$client
);

});
Expand Down
6 changes: 3 additions & 3 deletions src/config/strava.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

return [

'client_id' => env('CT_STRAVA_CLIENT_ID', ''),
'client_secret' => env('CT_STRAVA_SECRET_ID', ''),
'redirect_uri' => env('CT_STRAVA_REDIRECT_URI', ''),
'client_id' => env('CT_STRAVA_CLIENT_ID', ''),
'client_secret' => env('CT_STRAVA_SECRET_ID', ''),
'redirect_uri' => env('CT_STRAVA_REDIRECT_URI', ''),

];

0 comments on commit 9a60c7f

Please sign in to comment.