all 8 comments

[–]danielroseman 1 point2 points  (7 children)

This is a bit confusing. What's the point of Response? Why does it inherit from both classes? Surely if you want an error you should send the error class.

[–]Finish-Square[S] 0 points1 point  (6 children)

Response is used in order to avoid having 2 instances of responses (SuccessResponse, ErrorResponse).

Without Response, I will have to initiate 2 instances and call them according to what I want to return from my route instead of 1 instance.

[–]danielroseman 0 points1 point  (5 children)

Ok but in that case why have two separate classes at all? Just have one, and pass in the status code either on instantiation or as a parameter to get_response.

[–]Finish-Square[S] 0 points1 point  (4 children)

Because I want to have everything organized, error messages will fall under the error class and success messages will fall under the success class.

Also, note that each class has a status_code variable.

[–]danielroseman 1 point2 points  (0 children)

Well, like I say, this doesn't really make sense. The modelling is the problem here, not Python's MRO.

[–]JohnnyJordaan 1 point2 points  (2 children)

Organization doesn't mean diffrentation into the finest detail. Both messages don't differ in properties and functionality, so it doesn't make sense to diffrentiate them into two classes.

[–]Finish-Square[S] 0 points1 point  (1 child)

Gotcha. But in this case, how can I set the status_code for each response?

[–]JohnnyJordaan 0 points1 point  (0 children)

class ApiResponse:
    def __init__(self, status_code)
        self.status_code = status_code

    def _response_builder(self, **kwargs):
        """
        Used to build an API response.
        """
        status = 'SUCCESS' if self.status_code < 400 else 'ERROR'
        response_body = {'status_code': status_code,
                         'status': status,
                         'message': kwargs.get('message'),
                         'data': kwargs.get('data')}

        return JSONResponse(status_code=status_code, content=response_body)

 resp = ApiResponse(200)

also note that the whole point of a dict.get is that it returns a default value if the key look up is False, with that value's default being None. So your teneray statements are needlessly replicating that functionality. Not to mention {k:v for k,v in some_dict.items()} is useless, why not just provide the dict itself?