async def __call__(self, value): if _is_guillotina_response(value): value = aioResponse(body=self.get_body(value), status=value.status, headers=value.headers) if 'content-type' not in value.headers: value.headers.update({'content-type': self.content_type}) return value
def guess_response(self, value): resp = value.response if isinstance(resp, dict): resp = aioResponse(body=bytes(json.dumps(resp, cls=PServerJSONEncoder), 'utf-8')) resp.headers['Content-Type'] = 'application/json' elif isinstance(resp, list): resp = aioResponse(body=bytes(json.dumps(resp, cls=PServerJSONEncoder), 'utf-8')) resp.headers['Content-Type'] = 'application/json' elif isinstance(resp, str): resp = aioResponse(body=bytes(resp, 'utf-8')) resp.headers['Content-Type'] = 'text/html' elif resp is None: # missing result... resp = aioResponse(body=b'{}') resp.headers['Content-Type'] = 'application/json' resp.headers.update(value.headers) if not resp.prepared: resp.set_status(value.status) return resp
def guess_response(self, value): resp = value.response if isinstance(resp, dict): resp = aioResponse(body=bytes(json.dumps(resp), 'utf-8')) resp.headers['Content-Type'] = 'application/json' elif isinstance(resp, list): resp = aioResponse(body=bytes(json.dumps(resp), 'utf-8')) resp.headers['Content-Type'] = 'application/json' elif isinstance(resp, str): resp = aioResponse(body=bytes(resp, 'utf-8')) resp.headers['Content-Type'] = 'text/html' elif resp is None: # missing result... resp = aioResponse(body=b'{}') resp.headers['Content-Type'] = 'application/json' resp.headers.update(value.headers) if not resp.started: resp.set_status(value.status) return resp
def guess_response(self, value): resp = value.response if type(resp) in (dict, list, int, float, bool): resp = aioResponse(body=bytes( json.dumps(resp, cls=GuillotinaJSONEncoder), 'utf-8')) resp.headers['Content-Type'] = 'application/json' elif isinstance(resp, str): resp = aioResponse(body=bytes(resp, 'utf-8')) if '<html' in resp: resp.headers['Content-Type'] = 'text/html' else: resp.headers['Content-Type'] = 'text/plain' elif resp is None: # missing result... resp = aioResponse(body=b'{}') resp.headers['Content-Type'] = 'application/json' resp.headers.update(value.headers) if not resp.prepared: resp.set_status(value.status) return resp
def json_response(data=sentinel, *, text=None, body=None, status=200, reason=None, headers=None, content_type='application/json', dumps=json.dumps): if data is not sentinel: if text or body: raise ValueError( "only one of data, text, or body should be specified" ) else: text = dumps(data, cls=PServerJSONEncoder) return aioResponse( text=text, body=body, status=status, reason=reason, headers=headers, content_type=content_type)
def json_response(data=sentinel, *, text=None, body=None, status=200, reason=None, headers=None, content_type='application/json', dumps=json.dumps): if data is not sentinel: if text or body: raise ValueError( "only one of data, text, or body should be specified" ) else: text = dumps(data, cls=GuillotinaJSONEncoder) return aioResponse( text=text, body=body, status=status, reason=reason, headers=headers, content_type=content_type)
async def __call__(self, value): # Safe html transformation if _is_guillotina_response(value): body = value.response if not isinstance(body, bytes): if not isinstance(body, str): body = json.dumps(value.response) body = body.encode('utf8') value = aioResponse(body=body, status=value.status, headers=value.headers) if 'content-type' not in value.headers: value.headers.update({'content-type': self.content_type}) return value
async def __call__(self, value) -> aioResponse: """ Value can be: - Guillotina response object - serializable value """ status = 200 headers: Dict[str, str] = {} if IResponse.providedBy(value): headers = value.headers status = value.status_code or 200 value = value.content headers.update({"Content-Type": self.content_type}) return aioResponse(body=self.get_body(value), status=status, headers=headers)
async def __call__(self, value): # Safe html transformation if _is_pserver_response(value): body = value.response if not isinstance(body, bytes): if not isinstance(body, str): body = json.dumps(value.response) body = body.encode('utf8') value = aioResponse( body=body, status=value.status, headers=value.headers) if 'content-type' not in value.headers: value.headers.update({ 'content-type': 'text/html' }) return value
async def __call__(self, value) -> aioResponse: ''' Value can be: - Guillotina response object - serializable value ''' status = 200 headers: Dict[str, str] = {} if IResponse.providedBy(value): headers = value.headers status = value.status_code or 200 value = value.content headers.update({ 'Content-Type': self.content_type }) return aioResponse( body=self.get_body(value), status=status, headers=headers)