Skip to content

Commit

Permalink
Raise 'InvalidEntityException' if entity is malformed
Browse files Browse the repository at this point in the history
See #56
  • Loading branch information
Ben Einaudi committed Sep 27, 2019
1 parent 74bdf99 commit 435d109
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion main/cloudfoundry_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
This module provides a client library for cloudfoundry_client v2/v3.
"""

__version__ = "1.8.0"
__version__ = "1.9.0"
11 changes: 10 additions & 1 deletion main/cloudfoundry_client/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,13 @@ def __str__(self):
elif type(self.body) == str:
return '%d : %s' % (self.status_code, self.body)
else:
return '%d : %s' % (self.status_code, json.dumps(self.body))
return '%d : %s' % (self.status_code, json.dumps(self.body))


class InvalidEntity(Exception):
def __init__(self, **kwargs):
super(InvalidEntity, self).__init__()
self.raw_entity = dict(**kwargs)

def __str__(self):
return 'InvalidEntity: %s' % json.dumps(self.raw_entity)
6 changes: 5 additions & 1 deletion main/cloudfoundry_client/v2/entities.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import functools
import logging

from cloudfoundry_client.errors import InvalidEntity
from cloudfoundry_client.imported import quote, reduce
from cloudfoundry_client.json_object import JsonObject
from cloudfoundry_client.request_object import Request
Expand All @@ -14,6 +15,9 @@ def __init__(self, target_endpoint, client, *args, **kwargs):
self.target_endpoint = target_endpoint
self.client = client
try:
if 'entity' not in self:
raise InvalidEntity(**self)

for attribute, value in list(self['entity'].items()):
domain_name, suffix = attribute.rpartition('_')[::2]
if suffix == 'url':
Expand All @@ -34,7 +38,7 @@ def __init__(self, target_endpoint, client, *args, **kwargs):
new_method.__name__ = domain_name
setattr(self, domain_name, new_method)
except KeyError:
raise
raise InvalidEntity(**self)


class EntityManager(object):
Expand Down
11 changes: 6 additions & 5 deletions main/cloudfoundry_client/v3/entities.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import logging
import functools

from cloudfoundry_client.errors import InvalidEntity
from cloudfoundry_client.imported import quote, reduce
from cloudfoundry_client.json_object import JsonObject
from cloudfoundry_client.request_object import Request
Expand All @@ -8,7 +10,7 @@


class Entity(JsonObject):
def __init__(self, client, entity_manager, *args, **kwargs):
def __init__(self, entity_manager, *args, **kwargs):
super(Entity, self).__init__(*args, **kwargs)
try:
def default_method(m, u):
Expand All @@ -17,9 +19,8 @@ def default_method(m, u):
for link_name, link in self.get('links', {}).items():
if link_name != 'self':
link_method = link.get('method', 'GET').lower()
new_method = None
ref = link['href']
if link_method== 'get':
if link_method == 'get':
new_method = functools.partial(entity_manager._paginate, ref) if link_name.endswith('s')\
else functools.partial(entity_manager._get, ref)
elif link_method == 'post':
Expand All @@ -33,7 +34,7 @@ def default_method(m, u):
new_method.__name__ = link_name
setattr(self, link_name, new_method)
except KeyError:
raise
raise InvalidEntity(**self)


class EntityManager(object):
Expand Down Expand Up @@ -125,7 +126,7 @@ def _request(**mandatory_parameters):

def _entity(self, result):
if 'guid' in result:
return Entity(self.client, self, **result)
return Entity(self, **result)
else:
return result

Expand Down

0 comments on commit 435d109

Please sign in to comment.