Skip to content

Commit

Permalink
Merge pull request #83 from metaodi/develop
Browse files Browse the repository at this point in the history
Release 1.0.2
  • Loading branch information
metaodi committed Sep 7, 2017
2 parents dc50ee6 + bae6dce commit e60f625
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 38 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,13 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p

## [Unreleased][unreleased]

## 1.0.2 - 2017-09-07
### Added
- Rais ResponseEmptyApiError if we expect a response from the OpenStreetMap API, but didn't get one

### Removed
- Removed httpretty as HTTP mock library

## 1.0.1 - 2017-09-07
### Fixed
- Make sure tests run offline
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -96,7 +96,7 @@ To create a new release, follow these steps (please respect [Semantic Versioning

1. Adapt the version number in `osmapi/__init__.py`
1. Update the CHANGELOG with the version
1. Create a pull request to merge develop into master
1. Create a pull request to merge develop into master (make sure the tests pass!)
1. Create a [new release/tag on GitHub](https://github.com/metaodi/osmapi/releases) (on the master branch)
1. The [publication on PyPI](https://pypi.python.org/pypi/osmapi) happens via [Travis CI](https://travis-ci.org/metaodi/osmapi) on every tagged commit

Expand Down
40 changes: 32 additions & 8 deletions osmapi/OsmApi.py
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
2 changes: 1 addition & 1 deletion osmapi/__init__.py
@@ -1,5 +1,5 @@
from __future__ import (absolute_import, print_function, unicode_literals)

__version__ = '1.0.1'
__version__ = '1.0.2'

from .OsmApi import * # noqa
1 change: 0 additions & 1 deletion test-requirements.txt
Expand Up @@ -9,4 +9,3 @@ coveralls==0.4.1
mock==1.0.1
xmltodict==0.9.0
virtualenv==15.1.0
httpretty==0.8.14
18 changes: 9 additions & 9 deletions tests/changeset_tests.py
Expand Up @@ -92,10 +92,10 @@ def test_ChangesetUpdate(self):
xmltosorteddict(kwargs['data']),
xmltosorteddict(
b'<?xml version="1.0" encoding="UTF-8"?>\n'
b'<osm version="0.6" generator="osmapi/1.0.1">\n'
b'<osm version="0.6" generator="osmapi/1.0.2">\n'
b' <changeset visible="true">\n'
b' <tag k="test" v="foobar"/>\n'
b' <tag k="created_by" v="osmapi/1.0.1"/>\n'
b' <tag k="created_by" v="osmapi/1.0.2"/>\n'
b' </changeset>\n'
b'</osm>\n'
)
Expand Down Expand Up @@ -125,7 +125,7 @@ def test_ChangesetUpdate_with_created_by(self):
xmltosorteddict(kwargs['data']),
xmltosorteddict(
b'<?xml version="1.0" encoding="UTF-8"?>\n'
b'<osm version="0.6" generator="osmapi/1.0.1">\n'
b'<osm version="0.6" generator="osmapi/1.0.2">\n'
b' <changeset visible="true">\n'
b' <tag k="test" v="foobar"/>\n'
b' <tag k="created_by" v="MyTestOSMApp"/>\n'
Expand Down Expand Up @@ -163,10 +163,10 @@ def test_ChangesetCreate(self):
xmltosorteddict(kwargs['data']),
xmltosorteddict(
b'<?xml version="1.0" encoding="UTF-8"?>\n'
b'<osm version="0.6" generator="osmapi/1.0.1">\n'
b'<osm version="0.6" generator="osmapi/1.0.2">\n'
b' <changeset visible="true">\n'
b' <tag k="foobar" v="A new test changeset"/>\n'
b' <tag k="created_by" v="osmapi/1.0.1"/>\n'
b' <tag k="created_by" v="osmapi/1.0.2"/>\n'
b' </changeset>\n'
b'</osm>\n'
)
Expand All @@ -190,7 +190,7 @@ def test_ChangesetCreate_with_created_by(self):
xmltosorteddict(kwargs['data']),
xmltosorteddict(
b'<?xml version="1.0" encoding="UTF-8"?>\n'
b'<osm version="0.6" generator="osmapi/1.0.1">\n'
b'<osm version="0.6" generator="osmapi/1.0.2">\n'
b' <changeset visible="true">\n'
b' <tag k="foobar" v="A new test changeset"/>\n'
b' <tag k="created_by" v="CoolTestApp"/>\n'
Expand Down Expand Up @@ -276,7 +276,7 @@ def test_ChangesetUpload_create_node(self):
xmltosorteddict(kwargs['data']),
xmltosorteddict(
b'<?xml version="1.0" encoding="UTF-8"?>\n'
b'<osmChange version="0.6" generator="osmapi/1.0.1">\n'
b'<osmChange version="0.6" generator="osmapi/1.0.2">\n'
b'<create>\n'
b' <node lat="47.123" lon="8.555" visible="true" '
b'changeset="4444">\n'
Expand Down Expand Up @@ -350,7 +350,7 @@ def test_ChangesetUpload_modify_way(self):
xmltosorteddict(kwargs['data']),
xmltosorteddict(
b'<?xml version="1.0" encoding="UTF-8"?>\n'
b'<osmChange version="0.6" generator="osmapi/1.0.1">\n'
b'<osmChange version="0.6" generator="osmapi/1.0.2">\n'
b'<modify>\n'
b' <way id="4294967296" version="2" visible="true" '
b'changeset="4444">\n'
Expand Down Expand Up @@ -434,7 +434,7 @@ def test_ChangesetUpload_delete_relation(self):
xmltosorteddict(kwargs['data']),
xmltosorteddict(
b'<?xml version="1.0" encoding="UTF-8"?>\n'
b'<osmChange version="0.6" generator="osmapi/1.0.1">\n'
b'<osmChange version="0.6" generator="osmapi/1.0.2">\n'
b'<delete>\n'
b' <relation id="676" version="2" visible="true" '
b'changeset="4444">\n'
Expand Down
Empty file.
18 changes: 0 additions & 18 deletions tests/functional_tests.py

This file was deleted.

7 changes: 7 additions & 0 deletions tests/relation_tests.py
Expand Up @@ -310,3 +310,10 @@ def test_RelationsGet(self):
self.assertEquals(result[1532552]['id'], 1532552)
self.assertEquals(result[1532552]['visible'], True)
self.assertEquals(result[1532552]['tag']['route'], 'bicycle')

def test_RelationFull_with_deleted_relation(self):
self._session_mock(filenames=[], status=410)

with self.assertRaises(osmapi.ElementDeletedApiError) as context:
self.api.RelationFull(2911456)
self.assertEquals(410, context.exception.status)
6 changes: 6 additions & 0 deletions tests/way_tests.py
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 e60f625

Please sign in to comment.