2525import aiohttp
2626import asyncio
2727from .models import Profile , Clan , Constants , ClanInfo
28+ from .errors import RequestError , NotFound , InvalidTag
2829
2930class Client :
3031
@@ -47,24 +48,35 @@ async def __aenter__(self):
4748 async def __aexit__ (self , exc_type , exc_value , traceback ):
4849 self .session .close ()
4950
51+ async def request (self , url ):
52+ async with self .session .get (url ) as resp :
53+ data = await resp .json ()
54+
55+ # Request was successful
56+ if 300 > resp .status >= 200 :
57+ return data
58+
59+ # Tag not found
60+ if resp .status == 404 :
61+ raise NotFoundError (resp , data )
62+
63+ # Something wrong with the api servers :(
64+ if resp .stats > 500 :
65+ raise ServerError (resp , data )
66+
67+ # Everything else
68+ else :
69+ raise RequestError (resp , data )
70+
71+
5072 async def get_profile (self , * tags ):
5173 '''Get a profile object using tag(s)'''
5274
53- if ', ' in tags :
54- raise SyntaxError ("Read the docs please" )
55-
5675 tags = ',' .join (tags )
5776 url = f'{ self .BASE } /profile/{ tags } '
5877
59- async with self .session .get (url ) as resp :
60- if resp .status == 200 :
61- data = await resp .json ()
62- else :
63- raise ConnectionError (f'API not responding: { resp .status } ' )
78+ data = await self .request (url )
6479
65- if 'error' in data :
66- raise NameError ('Invalid Tag' )
67-
6880 if isinstance (data , list ):
6981 return [Profile (self , c ) for c in data ]
7082 else :
@@ -75,21 +87,10 @@ async def get_profile(self, *tags):
7587 async def get_clan (self , * tags ):
7688 '''Get a clan object using tag(s)'''
7789
78- if ', ' in tags : #hahaha
79- raise SyntaxError ("Read the docs please" )
80-
81- tags = ',' .join (tags )
82- url = f'{ self .BASE } /clan/{ tags } '
90+ url = f'{ self .BASE } /clan/{ ',' .join (tags )} '
8391
84- async with self .session .get (url ) as resp :
85- if resp .status == 200 :
86- data = await resp .json ()
87- else :
88- raise ConnectionError (f'API not responding: { resp .status } ' )
92+ data = await self .request (url )
8993
90- if 'error' in data :
91- raise NameError ('Invalid Tag' )
92-
9394 if isinstance (data , list ):
9495 return [Clan (self , c ) for c in data ]
9596 else :
@@ -102,11 +103,7 @@ async def get_constants(self):
102103
103104 url = f'{ self .BASE } /constants'
104105
105- async with self .session .get (url ) as resp :
106- if resp .status == 200 :
107- data = await resp .json ()
108- else :
109- raise ConnectionError (f'API not responding: { resp .status } ' )
106+ data = await self .request (url )
110107
111108 return Constants (self , data )
112109
@@ -117,10 +114,6 @@ async def get_top_clans(self):
117114
118115 url = f'{ self .BASE } /top/clans'
119116
120- async with self .session .get (url ) as resp :
121- if resp .status == 200 :
122- data = await resp .json ()
123- else :
124- raise ConnectionError (f'API not responding: { resp .status } ' )
117+ data = await self .request (url )
125118
126119 return [ClanInfo (self , c ) for c in data .get ('clans' )]
0 commit comments