Skip to content

Commit

Permalink
Merge pull request #74 from metaodi/fix-type-error
Browse files Browse the repository at this point in the history
Raise an exception if the API returned 410
  • Loading branch information
metaodi committed Sep 5, 2017
2 parents cdcf416 + f076ef9 commit da7f023
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,5 +1,6 @@
dist/
MANIFEST
*.pyc
*.egg-info
.coverage
.tox
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
### Removed
- osmapi does **not** support Python 2.6 anymore (it might work, it might not)

### Changed
- **BC-Break:** raise an exception of the requested element is deleted (previoulsy `None` has been returned)

## 0.8.1 - 2016-12-21
### Fixed
- Use setuptools instead of distutils in setup.py
Expand Down
24 changes: 22 additions & 2 deletions osmapi/OsmApi.py
Expand Up @@ -122,6 +122,13 @@ class NotSubscribedApiError(ApiError):
pass


class ElementDeletedApiError(ApiError):
"""
Error when the requested element is deleted
"""
pass


class OsmApi:
"""
Main class of osmapi, instanciate this class to use osmapi
Expand Down Expand Up @@ -1898,6 +1905,15 @@ 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.
If the username or password is missing,
`OsmApi.UsernamePasswordMissingError` is raised.
If the requested element has been deleted,
`OsmApi.ElementDeletedApiError` is raised.
If the response status code indicates an error,
`OsmApi.ApiError` is raised.
"""
if self._debug:
error_msg = (
Expand All @@ -1919,9 +1935,13 @@ def _http_request(self, method, path, auth, send): # noqa
response = self._session.request(method, path, auth=user_pass,
data=send)
if response.status_code != 200:
if response.status_code == 410:
return None
payload = response.content.strip()
if response.status_code == 410:
raise ElementDeletedApiError(
response.status_code,
response.reason,
payload
)
raise ApiError(response.status_code, response.reason, payload)
if self._debug:
error_msg = (
Expand Down
1 change: 1 addition & 0 deletions test-requirements.txt
Expand Up @@ -9,3 +9,4 @@ coveralls==0.4.1
mock==1.0.1
xmltodict==0.9.0
virtualenv==15.1.0
httpretty==0.8.14
17 changes: 17 additions & 0 deletions tests/functional_tests.py
@@ -0,0 +1,17 @@
import httpretty
import unittest
import osmapi


class TestOsmApiFunctional(unittest.TestCase):
@httpretty.activate
def test_deleted_element_raises_exception(self):
httpretty.register_uri(
httpretty.GET,
"https://www.openstreetmap.org/api/0.6/relation/2911456/full",
status=410
)
with self.assertRaises(osmapi.ElementDeletedApiError) as context:
api = osmapi.OsmApi()
api.RelationFull(2911456)
self.assertEquals(410, context.exception.status)
23 changes: 10 additions & 13 deletions tests/helper_tests.py
Expand Up @@ -101,19 +101,16 @@ def test_http_request_auth(self):

def test_http_request_410_response(self):
self.setupMock(410)
response = self.api._http_request(
'GET',
'/api/0.6/test410',
False,
None
)
self.api._session.request.assert_called_with(
'GET',
self.api_base + '/api/0.6/test410',
auth=None,
data=None
)
self.assertIsNone(response, "test response")
with self.assertRaises(osmapi.ElementDeletedApiError) as cm:
self.api._http_request(
'GET',
'/api/0.6/test410',
False,
None
)
self.assertEquals(cm.exception.status, 410)
self.assertEquals(cm.exception.reason, "test reason")
self.assertEquals(cm.exception.payload, "test response")

def test_http_request_500_response(self):
self.setupMock(500)
Expand Down

0 comments on commit da7f023

Please sign in to comment.