Skip to content

Commit 55baa76

Browse files
authored
Merge pull request #72 from maxmind/greg/proxy
Allow setting a proxy when creating a client
2 parents a7571fa + 059d55c commit 55baa76

3 files changed

Lines changed: 29 additions & 5 deletions

File tree

HISTORY.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
History
44
-------
55

6+
2.3.0
7+
++++++++++++++++++
8+
9+
* You may now set a proxy to use when making web service requests by passing
10+
the ``proxy`` parameter to the ``AsyncClient`` or ``Client`` constructor.
11+
612
2.2.0 (2020-10-13)
713
++++++++++++++++++
814

minfraud/webservice.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ class AsyncClient(BaseClient):
228228
"""Async client for accessing the minFraud web services."""
229229

230230
_existing_session: aiohttp.ClientSession
231+
_proxy: Optional[str]
231232

232233
def __init__( # pylint: disable=too-many-arguments
233234
self,
@@ -236,6 +237,7 @@ def __init__( # pylint: disable=too-many-arguments
236237
host: str = "minfraud.maxmind.com",
237238
locales: Tuple[str, ...] = ("en",),
238239
timeout: float = 60,
240+
proxy: Optional[str] = None,
239241
) -> None:
240242
"""Constructor for AsyncClient.
241243
@@ -247,14 +249,18 @@ def __init__( # pylint: disable=too-many-arguments
247249
:type host: str
248250
:param locales: A tuple of locale codes to use in name property
249251
:type locales: tuple[str]
250-
:param timeout: The timeout in seconts to use when waiting on the request.
252+
:param timeout: The timeout in seconds to use when waiting on the request.
251253
This sets both the connect timeout and the read timeout. The default is
252254
60.
253255
:type timeout: float
256+
:param proxy: The URL of an HTTP proxy to use. It may optionally include
257+
a basic auth username and password, e.g.,
258+
``http://username:password@host:port``.
254259
:return: Client object
255260
:rtype: Client
256261
"""
257262
super().__init__(account_id, license_key, host, locales, timeout)
263+
self._proxy = proxy
258264

259265
async def factors(
260266
self, transaction: Dict[str, Any], validate: bool = True
@@ -383,7 +389,7 @@ async def _do_request(
383389
self, uri: str, data: Dict[str, Any]
384390
) -> aiohttp.ClientResponse:
385391
session = await self._session()
386-
return await session.post(uri, json=data)
392+
return await session.post(uri, json=data, proxy=self._proxy)
387393

388394
async def _session(self) -> aiohttp.ClientSession:
389395
if not hasattr(self, "_existing_session"):
@@ -413,6 +419,7 @@ async def __aexit__(self, exc_type: None, exc_value: None, traceback: None) -> N
413419
class Client(BaseClient):
414420
"""Synchronous client for accessing the minFraud web services."""
415421

422+
_proxies: Optional[Dict[str, str]]
416423
_session: requests.Session
417424

418425
def __init__( # pylint: disable=too-many-arguments
@@ -422,6 +429,7 @@ def __init__( # pylint: disable=too-many-arguments
422429
host: str = "minfraud.maxmind.com",
423430
locales: Tuple[str, ...] = ("en",),
424431
timeout: float = 60,
432+
proxy: Optional[str] = None,
425433
) -> None:
426434
"""Constructor for Client.
427435
@@ -433,9 +441,12 @@ def __init__( # pylint: disable=too-many-arguments
433441
:type host: str
434442
:param locales: A tuple of locale codes to use in name property
435443
:type locales: tuple[str]
436-
:param timeout: The timeout in seconts to use when waiting on the request.
444+
:param timeout: The timeout in seconds to use when waiting on the request.
437445
This sets both the connect timeout and the read timeout. The default is
438446
60.
447+
:param proxy: The URL of an HTTP proxy to use. It may optionally include
448+
a basic auth username and password, e.g.,
449+
``http://username:password@host:port``.
439450
:type timeout: float
440451
:return: Client object
441452
:rtype: Client
@@ -447,6 +458,11 @@ def __init__( # pylint: disable=too-many-arguments
447458
self._session.headers["Accept"] = "application/json"
448459
self._session.headers["User-Agent"] = _REQUEST_UA
449460

461+
if proxy is None:
462+
self._proxies = None
463+
else:
464+
self._proxies = {"https": proxy}
465+
450466
def factors(self, transaction: Dict[str, Any], validate: bool = True) -> Factors:
451467
"""Query Factors endpoint with transaction data.
452468
@@ -562,7 +578,9 @@ def _response_for(
562578
return self._handle_success(body, uri, model_class)
563579

564580
def _do_request(self, uri: str, data: Dict[str, Any]) -> Response:
565-
return self._session.post(uri, json=data, timeout=self._timeout)
581+
return self._session.post(
582+
uri, json=data, timeout=self._timeout, proxies=self._proxies
583+
)
566584

567585
def close(self):
568586
"""Close underlying session

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ max-line-length = 88
99
universal = 1
1010

1111
[tox:tox]
12-
envlist = py36, py37, py38, mypy
12+
envlist = py36, py37, py38, py39, mypy
1313

1414
[gh-actions]
1515
python =

0 commit comments

Comments
 (0)