Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
0.3 (unreleased)
----------------

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

0.2.3 (2014-09-30)
------------------


- AstroResponse has been removed, which means that all cached objects will have
new hashes. You should clear your cache: for most users, that means
new hashes. You should clear your cache: for most users, that means
``rm -r ~/.astropy/cache/astroquery/`` (#418)
- In ESO and ALMA, default to *not* storing your password. New keyword
``store_password=False``. (#415)
Expand Down
7 changes: 5 additions & 2 deletions astroquery/utils/commons.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,16 @@ def send_request(url, data, timeout, request_type='POST', headers={},
if request_type == 'GET':
response = requests.get(url, params=data, timeout=timeout,
headers=headers, **kwargs)
return response
elif request_type == 'POST':
response = requests.post(url, data=data, timeout=timeout,
headers=headers, **kwargs)
return response
else:
raise ValueError("request_type must be either 'GET' or 'POST'.")

response.raise_for_status()

return response

except requests.exceptions.Timeout:
raise TimeoutError("Query timed out, time elapsed {time}s".
format(time=timeout))
Expand Down
5 changes: 3 additions & 2 deletions astroquery/utils/testing_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,16 @@ class MockResponse(object):
"""

def __init__(self, content=None, url=None, headers={},
content_type=None, stream=False, auth=None):
content_type=None, stream=False, auth=None, status_code=200):
assert content is None or hasattr(content, 'decode')
self.content = content
self.raw = content
self.headers = headers
if content_type is not None:
self.headers.update({'Content-Type':content_type})
self.headers.update({'Content-Type': content_type})
self.url = url
self.auth = auth
self.status_code = status_code

def iter_lines(self):
c = self.content.split(b"\n")
Expand Down
20 changes: 15 additions & 5 deletions astroquery/utils/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,20 @@ def test_parse_radius_2(radius):


def test_send_request_post(monkeypatch):
def mock_post(url, data, timeout, headers={}):
def mock_post(url, data, timeout, headers={}, status_code=200):
class SpecialMockResponse(object):

def __init__(self, url, data, headers):
def __init__(self, url, data, headers, status_code):
self.url = url
self.data = data
self.headers = headers
return SpecialMockResponse(url, data, headers=headers)
self.status_code = status_code

def raise_for_status(self):
pass

return SpecialMockResponse(url, data, headers=headers,
status_code=status_code)
monkeypatch.setattr(requests, 'post', mock_post)

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


def test_send_request_get(monkeypatch):
def mock_get(url, params, timeout, headers={}):
def mock_get(url, params, timeout, headers={}, status_code=200):
req = requests.Request('GET', url, params=params, headers=headers).prepare()
req.status_code = status_code
req.raise_for_status = lambda: None
return req
monkeypatch.setattr(requests, 'get', mock_get)
response = commons.send_request('https://github.com/astropy/astroquery',
Expand All @@ -113,8 +121,10 @@ def mock_get(url, params, timeout, headers={}):


def test_quantity_timeout(monkeypatch):
def mock_get(url, params, timeout, headers={}):
def mock_get(url, params, timeout, headers={}, status_code=200):
req = requests.Request('GET', url, params=params, headers=headers).prepare()
req.status_code = status_code
req.raise_for_status = lambda: None
return req
monkeypatch.setattr(requests, 'get', mock_get)
response = commons.send_request('https://github.com/astropy/astroquery',
Expand Down