Skip to content

Commit a3a6573

Browse files
committed
Utilise the new Forbidden exception, and fixing a bug with semi complete url when querying the api page
1 parent 2ab3257 commit a3a6573

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

coc/http.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
from urllib.parse import urlencode
3535

36-
from .errors import HTTPException, Maitenance, NotFound, InvalidArgument, InvalidToken
36+
from .errors import HTTPException, Maitenance, NotFound, InvalidArgument, InvalidToken, Forbidden
3737

3838
log = logging.getLogger(__name__)
3939

@@ -51,13 +51,13 @@ class Route:
5151
BASE = 'https://api.clashofclans.com/v1'
5252
API_PAGE_BASE = 'https://developer.clashofclans.com/api'
5353

54-
def __init__(self, method, path, kwargs: dict, api_page=False):
54+
def __init__(self, method, path, kwargs: dict=None, api_page=False):
5555
if '#' in path:
5656
path = path.replace('#', '%23')
5757

5858
self.method = method
5959
self.path = path
60-
url = (self.API_PAGE_BASE if api_page else self.BASE + self.path)
60+
url = (self.API_PAGE_BASE + self.path if api_page else self.BASE + self.path)
6161

6262
if kwargs:
6363
self.url = '{}?{}'.format(url, urlencode({k: v for k, v in kwargs.items() if v is not None}))
@@ -81,7 +81,7 @@ async def login(self):
8181
# try:
8282
# data = await self.request(Route('GET', '', {}))
8383
# except HTTPException as e:
84-
# if e.response.status == 401:
84+
# if e.response.status == 403:
8585
# raise InvalidToken(e, 'invalid token has been passed') from e
8686
# raise
8787
# return self
@@ -118,17 +118,21 @@ async def request(self, route, **kwargs):
118118
if r.status == 400:
119119
raise InvalidArgument(r, data)
120120
if r.status == 403:
121-
if self.update_tokens:
122-
log.info('Resetting Clash of Clans token')
123-
await self.reset_token()
124-
return await self.request(route, **kwargs)
125-
raise InvalidToken(r, data)
121+
if r.reason == 'accessDenied.invalidIp':
122+
if self.update_tokens:
123+
log.info('Resetting Clash of Clans token')
124+
await self.reset_token()
125+
return await self.request(route, **kwargs)
126+
log.info('detected invalid token, however client requested not to reset.')
127+
raise InvalidToken(r, data)
128+
129+
raise Forbidden(r, data)
126130

127131
if r.status == 404:
128-
raise NotFound(r, {})
132+
raise NotFound(r, data)
129133

130134
if r.status == 503:
131-
raise Maitenance(r, {})
135+
raise Maitenance(r, data)
132136
else:
133137
raise HTTPException(r, data)
134138

@@ -281,7 +285,7 @@ async def create_token(self, cookies, token_name, token_description, cidr_ranges
281285
"cidrRanges": cidr_ranges
282286
}
283287

284-
r = await self.request(Route('POST', '/apikey/create', {}, api_page=True), json=data, headers=headers)
288+
r = await self.request(Route('POST', '/apikey/create', api_page=True), json=data, headers=headers)
285289
return r['key']['key']
286290

287291
def delete_token(self, cookies, token_id):
@@ -294,7 +298,7 @@ def delete_token(self, cookies, token_id):
294298
"id": token_id
295299
}
296300

297-
return self.request(Route('POST', '/apikey/revoke', {}, api_page=True), json=data, headers=headers)
301+
return self.request(Route('POST', '/apikey/revoke', api_page=True), json=data, headers=headers)
298302

299303
async def get_data_from_url(self, url):
300304
async with self.__session.get(url) as r:

0 commit comments

Comments
 (0)