Skip to content

Commit 1fc1926

Browse files
committed
Simply return the redirect instead of raising error
1 parent fdbf1a3 commit 1fc1926

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

libservice/exceptions.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,15 @@ def __init__(self, result: dict):
2323
assert isinstance(result, dict)
2424
self.result = result
2525
super().__init__('No count exception')
26+
27+
28+
class RedirectException(Exception):
29+
pass
30+
31+
32+
class RedirectsNotAllowed(RedirectException):
33+
pass
34+
35+
36+
class TooManyRedirects(RedirectException):
37+
pass

libservice/utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from urllib.parse import urlparse
55
from typing import TYPE_CHECKING
66
from .addr import addr_check
7+
from .exceptions import TooManyRedirects, RedirectsNotAllowed
78
if TYPE_CHECKING:
89
import aiohttp
910
try:
@@ -90,10 +91,9 @@ async def safe_get(uri: str,
9091
await addr_check(current_uri)
9192
async with session.get(current_uri,
9293
allow_redirects=False) as resp:
93-
if resp.status not in (301, 302, 303, 307, 308):
94+
if allow_redirects is False or \
95+
resp.status not in (301, 302, 303, 307, 308):
9496
return resp
95-
if allow_redirects is False:
96-
raise Exception('Redirects not allowed')
9797
next_url = resp.headers.get('Location')
9898
if not next_url:
9999
break
@@ -105,4 +105,4 @@ async def safe_get(uri: str,
105105
current_uri = next_url
106106
continue
107107

108-
raise Exception('Too many redirects')
108+
raise TooManyRedirects('Too many redirects')

version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '1.0.2'
1+
__version__ = '1.0.3'

0 commit comments

Comments
 (0)