Skip to content

Commit

Permalink
Raise ResponseEmptyApiError for requests that expect a response
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Oderbolz committed Sep 7, 2017
1 parent 055f05d commit 452b7d3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
40 changes: 32 additions & 8 deletions osmapi/OsmApi.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ class ElementDeletedApiError(ApiError):
pass


class ResponseEmptyApiError(ApiError):
"""
Error when the response to the request is empty
"""
pass


class OsmApi:
"""
Main class of osmapi, instanciate this class to use osmapi
Expand Down Expand Up @@ -238,7 +245,7 @@ def __del__(self):
try:
if self._changesetauto:
self._changesetautoflush(True)
except ValueError:
except ResponseEmptyApiError:
pass

return None
Expand Down Expand Up @@ -1240,7 +1247,8 @@ def ChangesetUpdate(self, ChangesetTags={}):
ChangesetTags["created_by"] = self._created_by
self._put(
"/api/0.6/changeset/%s" % (self._CurrentChangesetId),
self._XmlBuild("changeset", {"tag": ChangesetTags})
self._XmlBuild("changeset", {"tag": ChangesetTags}),
return_value=False
)
return self._CurrentChangesetId

Expand Down Expand Up @@ -1285,7 +1293,8 @@ def ChangesetClose(self):
raise NoChangesetOpenError("No changeset currently opened")
self._put(
"/api/0.6/changeset/%s/close" % (self._CurrentChangesetId),
""
"",
return_value=False
)
CurrentChangesetId = self._CurrentChangesetId
self._CurrentChangesetId = 0
Expand Down Expand Up @@ -1920,7 +1929,7 @@ def _changesetautoflush(self, force=False):
self._changesetautocpt = 0
return None

def _http_request(self, method, path, auth, send): # noqa
def _http_request(self, method, path, auth, send, return_value=True): # noqa
"""
Returns the response generated by an HTTP request.
Expand All @@ -1933,6 +1942,8 @@ def _http_request(self, method, path, auth, send): # noqa
be preformed on this request.
`send` contains additional data that might be sent in a
request.
`return_value` indicates wheter this request should return
any data or not.
If the username or password is missing,
`OsmApi.UsernamePasswordMissingError` is raised.
Expand Down Expand Up @@ -1971,6 +1982,13 @@ def _http_request(self, method, path, auth, send): # noqa
payload
)
raise ApiError(response.status_code, response.reason, payload)
if return_value and not response.content:
raise ResponseEmptyApiError(
response.status_code,
response.reason,
''
)

if self._debug:
error_msg = (
"%s %s %s"
Expand All @@ -1979,12 +1997,18 @@ def _http_request(self, method, path, auth, send): # noqa
print(error_msg, file=sys.stderr)
return response.content

def _http(self, cmd, path, auth, send): # noqa
def _http(self, cmd, path, auth, send, return_value=True): # noqa
i = 0
while True:
i += 1
try:
return self._http_request(cmd, path, auth, send)
return self._http_request(
cmd,
path,
auth,
send,
return_value=return_value
)
except ApiError as e:
if e.status >= 500:
if i == self.MAX_RETRY_LIMIT:
Expand Down Expand Up @@ -2022,8 +2046,8 @@ def _sleep(self):
def _get(self, path):
return self._http('GET', path, False, None)

def _put(self, path, data):
return self._http('PUT', path, True, data)
def _put(self, path, data, return_value=True):
return self._http('PUT', path, True, data, return_value=return_value)

def _post(self, path, data, optionalAuth=False):
auth = True
Expand Down
Empty file.
6 changes: 6 additions & 0 deletions tests/way_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ def test_WayGet_with_version(self):
self.assertEquals(result['changeset'], 41303)
self.assertEquals(result['user'], 'metaodi')

def test_WayGet_nodata(self):
self._session_mock()

with self.assertRaises(osmapi.ResponseEmptyApiError):
self.api.WayGet(321)

def test_WayCreate(self):
self._session_mock(auth=True)

Expand Down

0 comments on commit 452b7d3

Please sign in to comment.