Skip to content

Commit 14b8be2

Browse files
committed
Allow setting a proxy for web service requests
1 parent 105c8aa commit 14b8be2

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

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+
4.2.0
7+
++++++++++++++++++
8+
9+
* You may now set a proxy to use when making web service request by passing
10+
the ``proxy`` parameter to the ``AsyncClient`` or ``Client`` constructor.
11+
612
4.1.0 (2020-09-25)
713
++++++++++++++++++
814

geoip2/webservice.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
import ipaddress
2929
import json
30-
from typing import Any, cast, List, Optional, Type, Union
30+
from typing import Any, Dict, cast, List, Optional, Type, Union
3131

3232
import aiohttp
3333
import aiohttp.http
@@ -248,10 +248,14 @@ class AsyncClient(BaseClient):
248248
:param timeout: The timeout in seconts to use when waiting on the request.
249249
This sets both the connect timeout and the read timeout. The default is
250250
60.
251+
:param proxy: The URL of an HTTP proxy to use. It may optionally include
252+
a basic auth username and password, e.g.,
253+
``http://username:password@host:port``.
251254
252255
"""
253256

254257
_existing_session: aiohttp.ClientSession
258+
_proxy: Optional[str]
255259

256260
def __init__( # pylint: disable=too-many-arguments
257261
self,
@@ -260,6 +264,7 @@ def __init__( # pylint: disable=too-many-arguments
260264
host: str = "geoip.maxmind.com",
261265
locales: Optional[List[str]] = None,
262266
timeout: float = 60,
267+
proxy: Optional[str] = None,
263268
) -> None:
264269
super().__init__(
265270
account_id,
@@ -268,6 +273,7 @@ def __init__( # pylint: disable=too-many-arguments
268273
locales,
269274
timeout,
270275
)
276+
self._proxy = proxy
271277

272278
async def city(self, ip_address: IPAddress = "me") -> City:
273279
"""Call GeoIP2 Precision City endpoint with the specified IP.
@@ -331,7 +337,7 @@ async def _response_for(
331337
) -> Union[Country, City, Insights]:
332338
uri = self._uri(path, ip_address)
333339
session = await self._session()
334-
async with await session.get(uri) as response:
340+
async with await session.get(uri, proxy=self._proxy) as response:
335341
status = response.status
336342
content_type = response.content_type
337343
body = await response.text()
@@ -398,10 +404,15 @@ class Client(BaseClient):
398404
:param timeout: The timeout in seconts to use when waiting on the request.
399405
This sets both the connect timeout and the read timeout. The default is
400406
60.
407+
:param proxy: The URL of an HTTP proxy to use. It may optionally include
408+
a basic auth username and password, e.g.,
409+
``http://username:password@host:port``.
410+
401411
402412
"""
403413

404414
_session: requests.Session
415+
_proxies: Optional[Dict[str, str]]
405416

406417
def __init__( # pylint: disable=too-many-arguments
407418
self,
@@ -410,12 +421,17 @@ def __init__( # pylint: disable=too-many-arguments
410421
host: str = "geoip.maxmind.com",
411422
locales: Optional[List[str]] = None,
412423
timeout: float = 60,
424+
proxy: Optional[str] = None,
413425
) -> None:
414426
super().__init__(account_id, license_key, host, locales, timeout)
415427
self._session = requests.Session()
416428
self._session.auth = (self._account_id, self._license_key)
417429
self._session.headers["Accept"] = "application/json"
418430
self._session.headers["User-Agent"] = _REQUEST_UA
431+
if proxy is None:
432+
self._proxies = None
433+
else:
434+
self._proxies = {"https": proxy}
419435

420436
def city(self, ip_address: IPAddress = "me") -> City:
421437
"""Call GeoIP2 Precision City endpoint with the specified IP.
@@ -464,7 +480,7 @@ def _response_for(
464480
ip_address: IPAddress,
465481
) -> Union[Country, City, Insights]:
466482
uri = self._uri(path, ip_address)
467-
response = self._session.get(uri, timeout=self._timeout)
483+
response = self._session.get(uri, proxies=self._proxies, timeout=self._timeout)
468484
status = response.status_code
469485
content_type = response.headers["Content-Type"]
470486
body = response.text

0 commit comments

Comments
 (0)