Skip to content

Commit 509251e

Browse files
committed
Added classmethod decorators for from_error and with_content methods
1 parent 5d2de75 commit 509251e

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

sage_utils/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
__title__ = 'sage-utils'
2-
__version__ = '0.1.0'
2+
__version__ = '0.2.0'
33
__license__ = 'BSD'
44
VERSION = __version__

sage_utils/wrappers.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,28 @@ class Response(object):
88
ERROR_FIELD_NAME = 'error'
99
EVENT_FIELD_NAME = 'event-name'
1010

11-
def append_extra_fields(self, event_name, *args, **kwargs):
12-
return {self.EVENT_FIELD_NAME: event_name}
11+
def __init__(self, data=None, *args, **kwargs):
12+
super(Response, self).__init__()
13+
self.data = data
1314

14-
def from_error(self, error_type, message, event_name=None):
15+
@classmethod
16+
def from_error(cls, error_type, message, event_name=None):
1517
if isinstance(message, str) and not message.endswith('.'):
1618
message = message + '.'
1719

1820
response = {
19-
self.ERROR_FIELD_NAME: {
21+
cls.ERROR_FIELD_NAME: {
2022
"type": error_type,
2123
"message": message
22-
}
24+
},
25+
cls.EVENT_FIELD_NAME: event_name
2326
}
24-
response.update(self.append_extra_fields(event_name))
25-
return response
27+
return cls(data=response)
2628

27-
def wrap_content(self, data, event_name=None):
28-
response = {self.CONTENT_FIELD_NAME: data}
29-
response.update(self.append_extra_fields(event_name))
30-
return response
29+
@classmethod
30+
def with_content(cls, data, event_name=None):
31+
response = {
32+
cls.CONTENT_FIELD_NAME: data,
33+
cls.EVENT_FIELD_NAME: event_name
34+
}
35+
return cls(data=response)

0 commit comments

Comments
 (0)