Skip to content

Commit

Permalink
Improved error control when making requests to the store
Browse files Browse the repository at this point in the history
  • Loading branch information
aarranz committed May 21, 2015
1 parent c98401a commit f4e02bf
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
30 changes: 24 additions & 6 deletions src/wirecloud/fiware/storeclient.py
Expand Up @@ -17,6 +17,8 @@
# You should have received a copy of the GNU Affero General Public License
# along with Wirecloud. If not, see <http://www.gnu.org/licenses/>.

from __future__ import unicode_literals

import json
import requests
from six.moves.urllib.parse import urljoin, urlparse, urlunparse
Expand All @@ -30,8 +32,20 @@ class UnexpectedResponse(Exception):

status = None

def __init__(self, status=None):
self.status = status
def __init__(self, response):
self.status = response.status_code

try:
error_info = json.loads(response.content)
self.message = error_info['message']
except:
pass

def __str__(self):
if self.status is not None and self.message is not None:
return "Unexpected response from server (Error code: %(error_code)s, Message: %(error_message)s)" % {"error_code": self.status, "error_message": self.message}
else:
return "Unexpected response from server (%s error code)" % self.status


class StoreClient(object):
Expand All @@ -52,7 +66,7 @@ def get_supported_plugins(self, token):
response = requests.get(urljoin(self._url, 'api/offering/resources/plugins'), headers=headers)

if response.status_code != 200:
raise UnexpectedResponse(response.status_code)
raise UnexpectedResponse(response)

return json.loads(response.text)

Expand All @@ -68,7 +82,7 @@ def get_offering_info(self, offering_id, token):
raise NotFound()

if response.status_code != 200:
raise UnexpectedResponse()
raise UnexpectedResponse(response)

return json.loads(response.text)

Expand All @@ -85,6 +99,9 @@ def start_purchase(self, offering_url, redirect_uri, token):
}
response = requests.post(urljoin(self._url, 'api/contracting/form'), data=json.dumps(data, ensure_ascii=False), headers=headers)

if response.status_code != 200:
raise UnexpectedResponse(response)

return json.loads(response.text)

def download_resource(self, url, token):
Expand All @@ -96,13 +113,14 @@ def download_resource(self, url, token):
response = requests.get(urljoin(self._url, url), headers=headers)

if response.status_code not in (200, 201, 204):
raise UnexpectedResponse(response.status_code)
raise UnexpectedResponse(response)

return response.content

def upload_resource(self, name, version, filename, description, content_type, f, token, open=True, resource_type=None):

headers = {
'Accept': 'application/json',
'Authorization': 'Bearer ' + token,
}
json_data = {
Expand All @@ -124,4 +142,4 @@ def upload_resource(self, name, version, filename, description, content_type, f,
raise Exception('Resource already exists')

if response.status_code not in (200, 201, 204):
raise UnexpectedResponse(response.status_code)
raise UnexpectedResponse(response)
13 changes: 9 additions & 4 deletions src/wirecloud/fiware/tests/store.py
Expand Up @@ -24,7 +24,7 @@
import os

from wirecloud.commons.utils.testcases import DynamicWebServer, WirecloudTestCase
from wirecloud.fiware.storeclient import StoreClient
from wirecloud.fiware.storeclient import NotFound, UnexpectedResponse, StoreClient


# Avoid nose to repeat these tests (they are run through wirecloud/fiware/tests/__init__.py)
Expand All @@ -33,7 +33,7 @@

class StoreTestCase(WirecloudTestCase):

tags = ('fiware', 'fiware-plugin', 'fiware-ut-13', 'wirecloud-fiware-store')
tags = ('wirecloud-fiware', 'wirecloud-fiware-plugin', 'fiware-ut-13', 'wirecloud-fiware-store')
servers = {
'http': {
'example.com': DynamicWebServer()
Expand Down Expand Up @@ -84,7 +84,12 @@ def test_offering_info_retreival(self):

def test_offering_info_retreival_404(self):

self.assertRaises(Exception, self.store_client.get_offering_info, '17', 'wirecloud_token')
self.assertRaises(NotFound, self.store_client.get_offering_info, '17', 'wirecloud_token')

def test_offering_info_retreival_unexpected_response(self):

self.network._servers['http']['example.com'].add_response('GET', '/api/offering/offerings/17', {'status_code': 409, 'content': '{"message": "error description"}'})
self.assertRaises(UnexpectedResponse, self.store_client.get_offering_info, '17', 'wirecloud_token')

def test_resource_upload(self):

Expand All @@ -94,4 +99,4 @@ def test_resource_upload(self):
def test_resource_upload_error(self):

self.network._servers['http']['example.com'].add_response('POST', '/api/offering/resources', {'content': '', 'status_code': 400})
self.assertRaises(Exception, self.store_client.upload_resource, 'Resource Name', '1.0', 'resource.zip', 'Resource file, probably a widget, an operator or a mashup', 'application/octet-stream', BytesIO(b'file contents'), 'test_token')
self.assertRaises(UnexpectedResponse, self.store_client.upload_resource, 'Resource Name', '1.0', 'resource.zip', 'Resource file, probably a widget, an operator or a mashup', 'application/octet-stream', BytesIO(b'file contents'), 'test_token')

0 comments on commit f4e02bf

Please sign in to comment.