Skip to content

Commit

Permalink
Merge pull request #70 from mollie/timeout-on-client
Browse files Browse the repository at this point in the history
Add possibility to define timeout on client
  • Loading branch information
whyscream authored Oct 25, 2018
2 parents 9ea245a + 8581574 commit da4f8b8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
9 changes: 7 additions & 2 deletions mollie/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ def validate_api_key(api_key):
"Invalid API key: '{api_key}'. An API key must start with 'test_' or 'live_'.".format(api_key=api_key))
return api_key

def __init__(self, api_key=None, api_endpoint=None):
def __init__(self, api_key=None, api_endpoint=None, timeout=10):
self.api_endpoint = self.validate_api_endpoint(api_endpoint or self.API_ENDPOINT)
self.api_version = self.API_VERSION
self.api_key = self.validate_api_key(api_key) if api_key else None
self.timeout = timeout
self.payments = Payments(self)
self.payment_refunds = PaymentRefunds(self)
self.payment_chargebacks = PaymentChargebacks(self)
Expand All @@ -71,6 +72,9 @@ def set_api_endpoint(self, api_endpoint):
def set_api_key(self, api_key):
self.api_key = self.validate_api_key(api_key)

def set_timeout(self, timeout):
self.timeout = timeout

def get_cacert(self):
cacert = pkg_resources.resource_filename('mollie.api', 'cacert.pem')
if not cacert or len(cacert) < 1:
Expand Down Expand Up @@ -109,7 +113,8 @@ def perform_http_call(self, http_method, path, data=None, params=None):
'X-Mollie-Client-Info': self.UNAME,
},
params=params,
data=data
data=data,
timeout=self.timeout,
)
except Exception as err:
raise RequestError('Unable to communicate with Mollie: {error}'.format(error=err))
Expand Down
32 changes: 30 additions & 2 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import sys
from datetime import datetime

import mock
import pkg_resources
import pytest
import requests

from mollie.api.client import Client, generate_querystring
from mollie.api.error import (
Expand Down Expand Up @@ -64,8 +66,10 @@ def test_client_invalid_api_key():

def test_client_no_cert_bundle(monkeypatch):
"""A request should raise an error when the certificate bundle is not available."""

def mockreturn(modulepath, file):
return ''

monkeypatch.setattr(pkg_resources, 'resource_filename', mockreturn)

client = Client()
Expand Down Expand Up @@ -225,7 +229,7 @@ def test_client_unicode_error_py2(client, response):
# handling the error should work even when utf-8 characters (€) are in the response.
exception = err.value
expected = 'Order line 1 is invalid. VAT amount is off. ' \
'Expected VAT amount to be 3.47 (21.00% over 20.00), got 3.10'
'Expected VAT amount to be 3.47 (21.00% over 20.00), got 3.10'
assert str(exception) == expected


Expand All @@ -240,5 +244,29 @@ def test_client_unicode_error_py3(client, response):
# handling the error should work even when utf-8 characters (€) are in the response.
exception = err.value
expected = 'Order line 1 is invalid. VAT amount is off. ' \
'Expected VAT amount to be €3.47 (21.00% over €20.00), got €3.10'
'Expected VAT amount to be €3.47 (21.00% over €20.00), got €3.10'
assert str(exception) == expected


@mock.patch('mollie.api.client.requests.request')
def test_client_request_timeout(request_mock, client):
"""Mock requests.request in the client to be able to read if the timeout is in the request call args."""
response = mock.Mock(status_code=200)
response.json.return_value = {}
response.headers.get.return_value = 'application/hal+json'
request_mock.return_value = response

client.set_timeout(300)
client.payments.list()
assert request_mock.call_args[1]['timeout'] == 300


@mock.patch('mollie.api.client.requests.request')
def test_client_request_timed_out(request_mock, client):
"""Timeout should raise a RequestError."""
request_mock.side_effect = requests.exceptions.ReadTimeout(
"HTTPSConnectionPool(host='api.mollie.com', port=443): Read timed out. (read timeout=10)")

with pytest.raises(RequestError) as err:
client.payments.list()
assert "Read timed out." in str(err.value)

0 comments on commit da4f8b8

Please sign in to comment.