Skip to content

Commit 28e2d19

Browse files
committed
Merge pull request #491 from bsipocz/utils_check_response_status_code
Check response status code and raise if it is an error
2 parents ac827c9 + eeaf478 commit 28e2d19

File tree

4 files changed

+26
-11
lines changed

4 files changed

+26
-11
lines changed

CHANGES

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
0.3 (unreleased)
22
----------------
33

4-
- None yet
4+
- Bugfix for ``utils.commons.send_request()``: Raise exception if error status
5+
is returned in the response. (#491)
56

67
0.2.3 (2014-09-30)
78
------------------
89

910

1011
- AstroResponse has been removed, which means that all cached objects will have
11-
new hashes. You should clear your cache: for most users, that means
12+
new hashes. You should clear your cache: for most users, that means
1213
``rm -r ~/.astropy/cache/astroquery/`` (#418)
1314
- In ESO and ALMA, default to *not* storing your password. New keyword
1415
``store_password=False``. (#415)

astroquery/utils/commons.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,16 @@ def send_request(url, data, timeout, request_type='POST', headers={},
8484
if request_type == 'GET':
8585
response = requests.get(url, params=data, timeout=timeout,
8686
headers=headers, **kwargs)
87-
return response
8887
elif request_type == 'POST':
8988
response = requests.post(url, data=data, timeout=timeout,
9089
headers=headers, **kwargs)
91-
return response
9290
else:
9391
raise ValueError("request_type must be either 'GET' or 'POST'.")
92+
93+
response.raise_for_status()
94+
95+
return response
96+
9497
except requests.exceptions.Timeout:
9598
raise TimeoutError("Query timed out, time elapsed {time}s".
9699
format(time=timeout))

astroquery/utils/testing_tools.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,16 @@ class MockResponse(object):
3333
"""
3434

3535
def __init__(self, content=None, url=None, headers={},
36-
content_type=None, stream=False, auth=None):
36+
content_type=None, stream=False, auth=None, status_code=200):
3737
assert content is None or hasattr(content, 'decode')
3838
self.content = content
3939
self.raw = content
4040
self.headers = headers
4141
if content_type is not None:
42-
self.headers.update({'Content-Type':content_type})
42+
self.headers.update({'Content-Type': content_type})
4343
self.url = url
4444
self.auth = auth
45+
self.status_code = status_code
4546

4647
def iter_lines(self):
4748
c = self.content.split(b"\n")

astroquery/utils/tests/test_utils.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,20 @@ def test_parse_radius_2(radius):
8585

8686

8787
def test_send_request_post(monkeypatch):
88-
def mock_post(url, data, timeout, headers={}):
88+
def mock_post(url, data, timeout, headers={}, status_code=200):
8989
class SpecialMockResponse(object):
9090

91-
def __init__(self, url, data, headers):
91+
def __init__(self, url, data, headers, status_code):
9292
self.url = url
9393
self.data = data
9494
self.headers = headers
95-
return SpecialMockResponse(url, data, headers=headers)
95+
self.status_code = status_code
96+
97+
def raise_for_status(self):
98+
pass
99+
100+
return SpecialMockResponse(url, data, headers=headers,
101+
status_code=status_code)
96102
monkeypatch.setattr(requests, 'post', mock_post)
97103

98104
response = commons.send_request('https://github.com/astropy/astroquery',
@@ -103,8 +109,10 @@ def __init__(self, url, data, headers):
103109

104110

105111
def test_send_request_get(monkeypatch):
106-
def mock_get(url, params, timeout, headers={}):
112+
def mock_get(url, params, timeout, headers={}, status_code=200):
107113
req = requests.Request('GET', url, params=params, headers=headers).prepare()
114+
req.status_code = status_code
115+
req.raise_for_status = lambda: None
108116
return req
109117
monkeypatch.setattr(requests, 'get', mock_get)
110118
response = commons.send_request('https://github.com/astropy/astroquery',
@@ -113,8 +121,10 @@ def mock_get(url, params, timeout, headers={}):
113121

114122

115123
def test_quantity_timeout(monkeypatch):
116-
def mock_get(url, params, timeout, headers={}):
124+
def mock_get(url, params, timeout, headers={}, status_code=200):
117125
req = requests.Request('GET', url, params=params, headers=headers).prepare()
126+
req.status_code = status_code
127+
req.raise_for_status = lambda: None
118128
return req
119129
monkeypatch.setattr(requests, 'get', mock_get)
120130
response = commons.send_request('https://github.com/astropy/astroquery',

0 commit comments

Comments
 (0)