22import datetime
33
44import aiohttp
5+ import aiohttp_socks
56
67import tibiapy
78from tibiapy import Character , Guild , World , House , KillStatistics , ListedGuild , Highscores , Category , VocationFilter , \
1415
1516
1617class Client :
17- """An asynchronous client that fetches information from Tibia.com.ArithmeticError
18+ """An asynchronous client that fetches information from Tibia.com
1819
1920 The client uses a :class:`aiohttp.ClientSession` to request the information.
2021 A single session is shared across all operations.
@@ -29,20 +30,26 @@ class Client:
2930 The event loop to use. The default one will be used if not defined.
3031 session: :class:`aiohttp.ClientSession`
3132 The client session that will be used for the requests. One will be created by default.
33+ proxy_url: :class:`str`
34+ The URL of the SOCKS proxy to use for requests.
35+ Note that if a session is passed, the SOCKS proxy won't be used and must be applied when creating the session.
3236 """
33- def __init__ (self , loop = None , session = None ):
37+
38+ def __init__ (self , loop = None , session = None , * , proxy_url = None ):
3439 self .loop = asyncio .get_event_loop () if loop is None else loop # type: asyncio.AbstractEventLoop
3540 if session is not None :
3641 self .session = session # type: aiohttp.ClientSession
3742 else :
38- self .loop .create_task (self ._initialize_session ())
43+ self .loop .create_task (self ._initialize_session (proxy_url ))
3944
40- async def _initialize_session (self ):
45+ async def _initialize_session (self , proxy_url = None ):
4146 headers = {
4247 'User-Agent' : "Tibia.py/%s (+https://github.com/Galarzaa90/tibia.py" % tibiapy .__version__ ,
4348 'Accept-Encoding' : "deflate, gzip"
4449 }
45- self .session = aiohttp .ClientSession (loop = self .loop , headers = headers ) # type: aiohttp.ClientSession
50+ connector = aiohttp_socks .SocksConnector .from_url (proxy_url ) if proxy_url else None
51+ self .session = aiohttp .ClientSession (loop = self .loop , headers = headers ,
52+ connector = connector ) # type: aiohttp.ClientSession
4653
4754 @classmethod
4855 def _handle_status (cls , status_code ):
@@ -81,6 +88,8 @@ async def _get(self, url):
8188 return await resp .text ()
8289 except aiohttp .ClientError as e :
8390 raise NetworkError ("aiohttp.ClientError: %s" % e , e )
91+ except aiohttp_socks .SocksConnectionError as e :
92+ raise NetworkError ("aiohttp_socks.SocksConnectionError: %s" % e , e )
8493
8594 async def _post (self , url , data ):
8695 """Base POST request, handling possible error statuses.
0 commit comments