Skip to content
This repository was archived by the owner on Aug 19, 2018. It is now read-only.

Commit 848ff0f

Browse files
committed
Managed timing out
1 parent 926e261 commit 848ff0f

File tree

3 files changed

+32
-24
lines changed

3 files changed

+32
-24
lines changed

crasync/core.py

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import aiohttp
2626
import asyncio
2727
from .models import Profile, Clan, Constants, ClanInfo
28-
from .errors import RequestError, NotFoundError, ServerError
28+
from .errors import RequestError, NotFoundError, ServerError, NotResponding
2929

3030
class Client:
3131

@@ -53,27 +53,30 @@ def close(self):
5353
self.session.close()
5454

5555
async def request(self, url):
56-
async with self.session.get(url, timeout=self.timeout) as resp:
57-
try:
58-
data = await resp.json()
59-
except (asyncio.TimeoutError, aiohttp.ClientResponseError):
60-
raise ServerError(resp, {})
61-
62-
# Request was successful
63-
if 300 > resp.status >= 200:
64-
return data
65-
66-
# Tag not found
67-
if resp.status == 404:
68-
raise NotFoundError(resp, data)
69-
70-
# Something wrong with the api servers :(
71-
if resp.status > 500:
72-
raise ServerError(resp, data)
73-
74-
# Everything else
75-
else:
76-
raise RequestError(resp, data)
56+
try:
57+
async with self.session.get(url, timeout=self.timeout) as resp:
58+
try:
59+
data = await resp.json()
60+
except (asyncio.TimeoutError, aiohttp.ClientResponseError):
61+
raise ServerError(resp, {})
62+
63+
# Request was successful
64+
if 300 > resp.status >= 200:
65+
return data
66+
67+
# Tag not found
68+
if resp.status == 404:
69+
raise NotFoundError(resp, data)
70+
71+
# Something wrong with the api servers :(
72+
if resp.status > 500:
73+
raise ServerError(resp, data)
74+
75+
# Everything else
76+
else:
77+
raise RequestError(resp, data)
78+
except asyncio.TimeoutError:
79+
raise NotResponding()
7780

7881

7982
async def get_profile(self, *tags):

crasync/errors.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
SOFTWARE.
2323
'''
2424

25+
class NotResponding(Exception):
26+
def __init__(self):
27+
self.code = 504
28+
self.error = 'API request timed out, please be patient.'
29+
super().__init__(self.error)
30+
2531
class RequestError(Exception):
2632
'''Base class for request errors'''
2733
def __init__(self, resp, data):
@@ -35,7 +41,6 @@ def __init__(self, resp, data):
3541
self.fmt = '{0.reason} ({0.code}): {0.error}'.format(self)
3642
super().__init__(self.fmt)
3743

38-
3944
class NotFoundError(RequestError):
4045
'''Raised if the player/clan is not found.'''
4146
pass

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
setup(
44
name='crasync',
55
packages=['crasync'], # this must be the same as the name above
6-
version='v2.1.2',
6+
version='v2.1.4',
77
description='An async wrapper for cr-api.com',
88
author='verixx',
99
license='MIT',

0 commit comments

Comments
 (0)