Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a parse_response method to Model and Collection #2

Open
jaimegildesagredo opened this issue Mar 12, 2013 · 1 comment
Open

Add a parse_response method to Model and Collection #2

jaimegildesagredo opened this issue Mar 12, 2013 · 1 comment
Assignees

Comments

@jaimegildesagredo
Copy link
Owner

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.

@jaimegildesagredo
Copy link
Owner Author

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant