Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -408,17 +408,25 @@ HttpError
UnexpectedError
```

### Rate Limiting

Calling `Intercom.rate_limit_details` returns a dict that contains details about your app's current rate limit.

```python
Intercom.rate_limit_details
# {'limit': 500, 'reset_at': datetime.datetime(2015, 3, 28, 13, 22), 'remaining': 497}
```

## Running the Tests

Unit tests:

```bash
PYTHONPATH=. describe tests/unit
nosetests tests/unit
```

Integration tests:

```bash
PYTHONPATH=. INTERCOM_APP_ID=xxx INTERCOM_APP_API_KEY=xxx nosetests tests/integration
INTERCOM_APP_ID=xxx INTERCOM_APP_API_KEY=xxx nosetests tests/integration
```
9 changes: 9 additions & 0 deletions intercom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class IntercomType(type): # noqa
_current_endpoint = None
_target_base_url = None
_endpoint_randomized_at = 0
_rate_limit_details = {}

@property
def _auth(self):
Expand Down Expand Up @@ -95,6 +96,14 @@ def hostname(self, value):
self.current_endpoint = None
self.endpoints = None

@property
def rate_limit_details(self):
return self._rate_limit_details

@rate_limit_details.setter
def rate_limit_details(self, value):
self._rate_limit_details = value

@property
def protocol(self):
return self._protocol
Expand Down
18 changes: 18 additions & 0 deletions intercom/request.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

from . import errors
from datetime import datetime

import certifi
import json
Expand Down Expand Up @@ -33,6 +34,7 @@ def send_request_to_path(cls, method, url, auth, params=None):
auth=auth, verify=certifi.where(), **req_params)

cls.raise_errors_on_failure(resp)
cls.set_rate_limit_details(resp)

if resp.content:
return cls.parse_body(resp)
Expand All @@ -47,6 +49,22 @@ def parse_body(cls, resp):
cls.raise_application_errors_on_failure(body, resp.status_code)
return body

@classmethod
def set_rate_limit_details(cls, resp):
rate_limit_details = {}
headers = resp.headers
limit = headers.get('x-ratelimit-limit', None)
remaining = headers.get('x-ratelimit-remaining', None)
reset = headers.get('x-ratelimit-reset', None)
if limit:
rate_limit_details['limit'] = int(limit)
if remaining:
rate_limit_details['remaining'] = int(remaining)
if reset:
rate_limit_details['reset_at'] = datetime.fromtimestamp(int(reset))
from intercom import Intercom
Intercom.rate_limit_details = rate_limit_details

@classmethod
def raise_errors_on_failure(cls, resp):
if resp.status_code == 404:
Expand Down
1 change: 1 addition & 0 deletions tests/integration/test_count.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def setup_class(cls):
def teardown_class(cls):
delete(cls.company)
delete(cls.user)
print(Intercom.rate_limit_details)

def test_user_counts_for_each_tag(self):
# Get User Tag Count Object
Expand Down