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

Template changes #89

Merged
merged 2 commits into from
Sep 19, 2023
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<!-- @format -->

# 1.47.0

- Add rate limit response headers as properties on the client

# 1.46.2

- Remove nose in favour of pytest
Expand Down
10 changes: 10 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ Access API endpoints using the corresponding methods on the client object:
headers={'Accept-Language': 'fr'}
)
Rate limit response headers can be read:

.. code:: python
# Note these properties will be None until you make an API request with the client
client.rate_limit.limit
client.rate_limit.remaining
client.rate_limit.reset
For full documentation, see our `API reference`_.

.. _API reference: https://developer.gocardless.com/api-reference
Expand Down
2 changes: 1 addition & 1 deletion gocardless_pro/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

from .client import Client

__version__ = '1.46.2'
__version__ = '1.47.0'

10 changes: 8 additions & 2 deletions gocardless_pro/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import requests

from . import errors
from .rate_limit import RateLimit, update_rate_limit

class ApiClient(object):
"""Client for interacting with a JSON HTTP API, using OAuth2-style auth.
Expand All @@ -26,7 +27,9 @@ class ApiClient(object):
def __init__(self, base_url, access_token):
self.base_url = base_url
self.access_token = access_token
self.rate_limit = RateLimit()

@update_rate_limit
def get(self, path, params=None, headers=None):
"""Perform a GET request, optionally providing query-string params.

Expand All @@ -48,6 +51,7 @@ def get(self, path, params=None, headers=None):
self._handle_errors(response)
return response

@update_rate_limit
def post(self, path, body, headers=None):
"""Perform a POST request, providing a body, which will be JSON-encoded.

Expand All @@ -70,6 +74,7 @@ def post(self, path, body, headers=None):
self._handle_errors(response)
return response

@update_rate_limit
def put(self, path, body, headers=None):
"""Perform a PUT request, providing a body, which will be JSON-encoded.

Expand All @@ -91,6 +96,7 @@ def put(self, path, body, headers=None):
self._handle_errors(response)
return response

@update_rate_limit
def delete(self, path, body, headers=None):
"""Perform a DELETE request, providing a body, which will be JSON-encoded.

Expand Down Expand Up @@ -144,7 +150,7 @@ def _default_headers(self):
'Authorization': 'Bearer {0}'.format(self.access_token),
'Content-Type': 'application/json',
'GoCardless-Client-Library': 'gocardless-pro-python',
'GoCardless-Client-Version': '1.46.2',
'GoCardless-Client-Version': '1.47.0',
'User-Agent': self._user_agent(),
'GoCardless-Version': '2015-07-06',
}
Expand All @@ -153,7 +159,7 @@ def _user_agent(self):
python_version = '.'.join(platform.python_version_tuple()[0:2])
vm_version = '{}.{}.{}-{}{}'.format(*sys.version_info)
return ' '.join([
'gocardless-pro-python/1.46.2',
'gocardless-pro-python/1.47.0',
'python/{0}'.format(python_version),
'{0}/{1}'.format(platform.python_implementation(), vm_version),
'{0}/{1}'.format(platform.system(), platform.release()),
Expand Down
9 changes: 9 additions & 0 deletions gocardless_pro/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,15 @@ def verification_details(self):
def webhooks(self):
return services.WebhooksService(self._api_client, 3, 0.5, self._raise_on_idempotency_conflict)


@property
def rate_limit(self):
return {
"ratelimit-limit": self._api_client.rate_limit.limit,
"ratelimit-remaining": self._api_client.rate_limit.remaining,
"ratelimit-reset": self._api_client.rate_limit.reset
}

def _environment_url(self, environment):
environment_urls = {
'live': 'https://api.gocardless.com',
Expand Down
37 changes: 37 additions & 0 deletions gocardless_pro/rate_limit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from functools import wraps


def update_rate_limit(method):
"""Wrap all fetch methods in this decorator to update the client's
ratelimit object with remaining requests available.
"""
@wraps(method)
def wrapper(self, *args, **kwargs):
response = method(self, *args, **kwargs)
self.rate_limit.update_from_response(response)
return response
return wrapper


class RateLimit:

def __init__(self):
self.limit = None
self.remaining = None
self.reset = None

def update_from_response(self, response):
"""Reads the remaining ratelimit from the response and updates
the remaining attribute.
Args:
response (requests.Response): A requests ``Response`` object.
"""
remaining = response.headers.get('ratelimit-remaining')
if remaining:
self.remaining = int(remaining)

limit = response.headers.get('ratelimit-limit')
if limit:
self.limit = int(limit)

self.reset = response.headers.get('ratelimit-reset')
4 changes: 2 additions & 2 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
responses>=0.10.16;python_version>"3.4"
responses<0.10.16;python_version<="3.4"
pytest
requests
responses
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setup(
name = 'gocardless_pro',
version = '1.46.2',
version = '1.47.0',
packages = find_packages(exclude=['tests']),
install_requires = ['requests>=2.6', 'six'],
author = 'GoCardless',
Expand Down
1 change: 0 additions & 1 deletion tests/api_client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import json

import pytest
import requests
import responses

from gocardless_pro import api_client
Expand Down
1 change: 0 additions & 1 deletion tests/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#

import pytest
import requests
import responses

from gocardless_pro import Client
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/bank_authorisations.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
"method": "POST",
"path_template": "/bank_authorisations",
"url_params": [],
"body": {"bank_authorisations":{"authorisation_type":"example authorisation_type 8081","authorised_at":"2020-01-01T12:00:00.000Z","created_at":"2023-09-19T14:17:04.929Z","expires_at":"2023-09-19T14:17:04.929Z","id":"BAU123","last_visited_at":"2020-01-01T12:00:00.000Z","links":{"billing_request":"BRQ123","institution":"monzo"},"qr_code_url":"https://pay.gocardless.com/obauth/BAU123/qr_code","redirect_uri":"https://my-website.com/abc/callback","url":"https://pay.gocardless.com/obauth/BAU123"}}
"body": {"bank_authorisations":{"authorisation_type":"example authorisation_type 8081","authorised_at":"2020-01-01T12:00:00.000Z","created_at":"2023-09-19T15:52:21.239Z","expires_at":"2023-09-19T15:52:21.239Z","id":"BAU123","last_visited_at":"2020-01-01T12:00:00.000Z","links":{"billing_request":"BRQ123","institution":"monzo"},"qr_code_url":"https://pay.gocardless.com/obauth/BAU123/qr_code","redirect_uri":"https://my-website.com/abc/callback","url":"https://pay.gocardless.com/obauth/BAU123"}}
},
"get": {
"method": "GET",
"path_template": "/bank_authorisations/:identity",
"url_params": ["BAU123"],
"body": {"bank_authorisations":{"authorisation_type":"example authorisation_type 7887","authorised_at":"2020-01-01T12:00:00.000Z","created_at":"2023-09-19T14:17:04.929Z","expires_at":"2023-09-19T14:17:04.929Z","id":"BAU123","last_visited_at":"2020-01-01T12:00:00.000Z","links":{"billing_request":"BRQ123","institution":"monzo"},"qr_code_url":"https://pay.gocardless.com/obauth/BAU123/qr_code","redirect_uri":"https://my-website.com/abc/callback","url":"https://pay.gocardless.com/obauth/BAU123"}}
"body": {"bank_authorisations":{"authorisation_type":"example authorisation_type 7887","authorised_at":"2020-01-01T12:00:00.000Z","created_at":"2023-09-19T15:52:21.239Z","expires_at":"2023-09-19T15:52:21.239Z","id":"BAU123","last_visited_at":"2020-01-01T12:00:00.000Z","links":{"billing_request":"BRQ123","institution":"monzo"},"qr_code_url":"https://pay.gocardless.com/obauth/BAU123/qr_code","redirect_uri":"https://my-website.com/abc/callback","url":"https://pay.gocardless.com/obauth/BAU123"}}
}
}
4 changes: 2 additions & 2 deletions tests/fixtures/billing_request_flows.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
"method": "POST",
"path_template": "/billing_request_flows",
"url_params": [],
"body": {"billing_request_flows":{"authorisation_url":"https://monzo.com/abc-123-things","auto_fulfil":false,"created_at":"2023-09-19T14:17:04.934Z","exit_uri":"https://my-website.com/abc/callback","expires_at":"2023-09-19T14:17:04.934Z","id":"BRF123","language":"en","links":{"billing_request":"BRQ123"},"lock_bank_account":false,"lock_currency":true,"lock_customer_details":false,"prefilled_bank_account":{"account_type":"savings"},"prefilled_customer":{"address_line1":"221B Baker Street","address_line2":"Marylebone","address_line3":"City of Westminster","city":"London","company_name":"Hamilton Trading Ltd.","country_code":"GB","danish_identity_number":"220550-6218","email":"[email protected]","family_name":"Osborne","given_name":"Frank","postal_code":"NW1 6XE","region":"Greater London","swedish_identity_number":"556564-5404"},"redirect_uri":"https://my-website.com/abc/callback","session_token":"sesh_123","show_redirect_buttons":true,"show_success_redirect_button":false}}
"body": {"billing_request_flows":{"authorisation_url":"https://monzo.com/abc-123-things","auto_fulfil":false,"created_at":"2023-09-19T15:52:21.244Z","exit_uri":"https://my-website.com/abc/callback","expires_at":"2023-09-19T15:52:21.244Z","id":"BRF123","language":"en","links":{"billing_request":"BRQ123"},"lock_bank_account":false,"lock_currency":true,"lock_customer_details":false,"prefilled_bank_account":{"account_type":"savings"},"prefilled_customer":{"address_line1":"221B Baker Street","address_line2":"Marylebone","address_line3":"City of Westminster","city":"London","company_name":"Hamilton Trading Ltd.","country_code":"GB","danish_identity_number":"220550-6218","email":"[email protected]","family_name":"Osborne","given_name":"Frank","postal_code":"NW1 6XE","region":"Greater London","swedish_identity_number":"556564-5404"},"redirect_uri":"https://my-website.com/abc/callback","session_token":"sesh_123","show_redirect_buttons":false,"show_success_redirect_button":true}}
},
"initialise": {
"method": "POST",
"path_template": "/billing_request_flows/:identity/actions/initialise",
"url_params": ["BRF123"],
"body": {"billing_request_flows":{"authorisation_url":"https://monzo.com/abc-123-things","auto_fulfil":true,"created_at":"2023-09-19T14:17:04.934Z","exit_uri":"https://my-website.com/abc/callback","expires_at":"2023-09-19T14:17:04.934Z","id":"BRF123","language":"en","links":{"billing_request":"BRQ123"},"lock_bank_account":true,"lock_currency":false,"lock_customer_details":false,"prefilled_bank_account":{"account_type":"savings"},"prefilled_customer":{"address_line1":"221B Baker Street","address_line2":"Marylebone","address_line3":"City of Westminster","city":"London","company_name":"Hamilton Trading Ltd.","country_code":"GB","danish_identity_number":"220550-6218","email":"[email protected]","family_name":"Osborne","given_name":"Frank","postal_code":"NW1 6XE","region":"Greater London","swedish_identity_number":"556564-5404"},"redirect_uri":"https://my-website.com/abc/callback","session_token":"sesh_123","show_redirect_buttons":true,"show_success_redirect_button":false}}
"body": {"billing_request_flows":{"authorisation_url":"https://monzo.com/abc-123-things","auto_fulfil":true,"created_at":"2023-09-19T15:52:21.244Z","exit_uri":"https://my-website.com/abc/callback","expires_at":"2023-09-19T15:52:21.244Z","id":"BRF123","language":"en","links":{"billing_request":"BRQ123"},"lock_bank_account":false,"lock_currency":false,"lock_customer_details":false,"prefilled_bank_account":{"account_type":"savings"},"prefilled_customer":{"address_line1":"221B Baker Street","address_line2":"Marylebone","address_line3":"City of Westminster","city":"London","company_name":"Hamilton Trading Ltd.","country_code":"GB","danish_identity_number":"220550-6218","email":"[email protected]","family_name":"Osborne","given_name":"Frank","postal_code":"NW1 6XE","region":"Greater London","swedish_identity_number":"556564-5404"},"redirect_uri":"https://my-website.com/abc/callback","session_token":"sesh_123","show_redirect_buttons":true,"show_success_redirect_button":true}}
}
}
Loading