27
27
28
28
import ipaddress
29
29
import json
30
- from typing import Any , cast , List , Optional , Type , Union
30
+ from typing import Any , Dict , cast , List , Optional , Type , Union
31
31
32
32
import aiohttp
33
33
import aiohttp .http
@@ -248,10 +248,14 @@ class AsyncClient(BaseClient):
248
248
:param timeout: The timeout in seconts to use when waiting on the request.
249
249
This sets both the connect timeout and the read timeout. The default is
250
250
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``.
251
254
252
255
"""
253
256
254
257
_existing_session : aiohttp .ClientSession
258
+ _proxy : Optional [str ]
255
259
256
260
def __init__ ( # pylint: disable=too-many-arguments
257
261
self ,
@@ -260,6 +264,7 @@ def __init__( # pylint: disable=too-many-arguments
260
264
host : str = "geoip.maxmind.com" ,
261
265
locales : Optional [List [str ]] = None ,
262
266
timeout : float = 60 ,
267
+ proxy : Optional [str ] = None ,
263
268
) -> None :
264
269
super ().__init__ (
265
270
account_id ,
@@ -268,6 +273,7 @@ def __init__( # pylint: disable=too-many-arguments
268
273
locales ,
269
274
timeout ,
270
275
)
276
+ self ._proxy = proxy
271
277
272
278
async def city (self , ip_address : IPAddress = "me" ) -> City :
273
279
"""Call GeoIP2 Precision City endpoint with the specified IP.
@@ -331,7 +337,7 @@ async def _response_for(
331
337
) -> Union [Country , City , Insights ]:
332
338
uri = self ._uri (path , ip_address )
333
339
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 :
335
341
status = response .status
336
342
content_type = response .content_type
337
343
body = await response .text ()
@@ -398,10 +404,15 @@ class Client(BaseClient):
398
404
:param timeout: The timeout in seconts to use when waiting on the request.
399
405
This sets both the connect timeout and the read timeout. The default is
400
406
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
+
401
411
402
412
"""
403
413
404
414
_session : requests .Session
415
+ _proxies : Optional [Dict [str , str ]]
405
416
406
417
def __init__ ( # pylint: disable=too-many-arguments
407
418
self ,
@@ -410,12 +421,17 @@ def __init__( # pylint: disable=too-many-arguments
410
421
host : str = "geoip.maxmind.com" ,
411
422
locales : Optional [List [str ]] = None ,
412
423
timeout : float = 60 ,
424
+ proxy : Optional [str ] = None ,
413
425
) -> None :
414
426
super ().__init__ (account_id , license_key , host , locales , timeout )
415
427
self ._session = requests .Session ()
416
428
self ._session .auth = (self ._account_id , self ._license_key )
417
429
self ._session .headers ["Accept" ] = "application/json"
418
430
self ._session .headers ["User-Agent" ] = _REQUEST_UA
431
+ if proxy is None :
432
+ self ._proxies = None
433
+ else :
434
+ self ._proxies = {"https" : proxy }
419
435
420
436
def city (self , ip_address : IPAddress = "me" ) -> City :
421
437
"""Call GeoIP2 Precision City endpoint with the specified IP.
@@ -464,7 +480,7 @@ def _response_for(
464
480
ip_address : IPAddress ,
465
481
) -> Union [Country , City , Insights ]:
466
482
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 )
468
484
status = response .status_code
469
485
content_type = response .headers ["Content-Type" ]
470
486
body = response .text
0 commit comments