finch
finch copied to clipboard
Add a parse_response method to Model and Collection
I would like to divide the current parse(body, headers)
method, which parses the http response and returns a dict with the model or collection values, into a parse_response(body, headers)
method that only parses the response and returns a raw dict, and a parse(raw)
method that receives the raw dict from parse_response
and returns a new dict with the values for the model or collection.
See the example below:
class User(Model):
name = StringField()
age = IntegerField()
def parse_response(self, body, headers):
return json.loads(body)
def parse(self, raw):
return {
'name': raw['fullName'],
'age': int(raw['age'])
}
Also, that way we could call the Model.parse
method for each item when retrieving an entire collection.
I think I will start moving the parse(self, body, headers)
method to an static method named parse_response
to be called before creating a new model instance in the Collection's get
and all
methods.
class User(Model):
@staticmethod
def parse_response(body, headers):
return json.loads(body)