""" Result. A result encapsulates the outcome of a call to the logic layer. The result contains 3 fields: data (data), error (any error found), error_detail (details of the error found) and a status. Although the status values correspond to HTTP status codes, a result object is not necessarily meant to represent an HTTP response. Crafting an HTTP response is a responsibility of the controller and not the logic layer, and a result object can be consumed by a non-HTTP caller such as a CLI script or a queue consumer, as well as by a controller. The data property can be any type of object. The exact type of the object is part of the contract between the service that creates the result and the caller that consumes it. This is modeled after the Response class in grass. """ class Result: """Result class.""" def __init__(self, data=None, error=None, error_detail=None, status=200): """Initialize result with provided data.""" self.data = data self.error = error self.error_detail = error_detail self.status = status @property def success(self): """Show success if self is a 2XX http response.""" return 200 <= self.status < 300 def create_fatal_result(error=None, error_detail=None): """Create a fatal result Result object.""" return Result(error=error, error_detail=error_detail, status=500) def create_error_result(error=None, error_detail=None): """Create an error result Result object.""" return Result(error=error, error_detail=error_detail, status=400)