def deserialize(self, stream, content_type, content_length): try: return self.loads(stream.read().decode('utf-8')) except ValueError as err: raise errors.HTTPBadRequest( title='Invalid JSON', description='Could not parse JSON body - {0}'.format(err))
async def deserialize_async(self, stream, content_type, content_length): data = await stream.read() try: return self.loads(data.decode('utf-8')) except ValueError as err: raise errors.HTTPBadRequest( 'Invalid JSON', 'Could not parse JSON body - {0}'.format(err))
def deserialize(self, raw): try: return json.loads(raw.decode('utf-8')) except ValueError as err: raise errors.HTTPBadRequest( 'Invalid JSON', 'Could not parse JSON body - {0}'.format(err) )
def deserialize(self, stream, content_type, content_length): try: # NOTE(jmvrbanac): Using unpackb since we would need to manage # a buffer for Unpacker() which wouldn't gain us much. return self.msgpack.unpackb(stream.read(), encoding='utf-8') except ValueError as err: raise errors.HTTPBadRequest( 'Invalid MessagePack', 'Could not parse MessagePack body - {0}'.format(err))
async def deserialize_async(self, stream, content_type, content_length): data = await stream.read() try: # NOTE(jmvrbanac): Using unpackb since we would need to manage # a buffer for Unpacker() which wouldn't gain us much. return self.msgpack.unpackb(data, raw=False) except ValueError as err: raise errors.HTTPBadRequest( title='Invalid MessagePack', description='Could not parse MessagePack body - {0}'.format( err))
def on_get(self, req, resp, completed): """check completed is a valid word """ _true_list = ['true', 'True'] _false_test = ['false', 'False'] if completed in _true_list: completed = True elif completed in _false_test: completed = False else: raise errors.HTTPBadRequest( 'Invalid URI', {'Valid URI': [_true_list, _false_test]}) resp.body = json.dumps(JsonFile.find_in_json('completed', completed))
def deserialize(self, raw): try: result = json.loads(raw.decode('utf-8')) for key in result: if 'datetime' in key: result[key] = self.rfc3339_to_datetime(result[key]) elif 'date' in key: result[key] = self.isodate_to_date(result[key]) elif 'timestamp' in key: result[key] = self.timestamp_to_datetime(result[key]) return result except ValueError as err: raise errors.HTTPBadRequest( 'Invalid JSON', 'Could not parse JSON body - {0}'.format(err))
def on_post(self, request: falcon.Request, response: falcon.Response) -> None: payload = json.loads(request.bounded_stream.read()) try: gift_card_create_request = self._create_serializer.load(payload) except ValidationError as validation_error: raise errors.HTTPBadRequest( title="Validation Error", description=validation_error.messages, ) try: self._create_use_case.create(gift_card_create_request) except GiftCardAlreadyExists: raise errors.HTTPBadRequest response.status = falcon.HTTP_201
def on_put(self, req, resp, identify): """this method update one dictionary and can not change ID """ identify = self.make_id_int(identify) client_req = req.media if type(client_req) is not dict: raise falcon.errors.HTTPBadRequest( 'your request body must be a dictionary') exist_id = False my_data = JsonFile.read_file() for i in range(len(my_data['todo'])): if identify == my_data['todo'][i]['id']: exist_id = my_data['todo'][i] dic = my_data['todo'][i] if 'completed' in client_req: if client_req['completed'] in [ 'true', 'True' ] or client_req['completed'] is True: dic['completed'] = True else: dic['completed'] = False if 'text' in client_req: if type(client_req['text']) is not str: raise errors.HTTPBadRequest( 'Invalid text', 'text\' type must be string ') else: dic['text'] = client_req['text'] JsonFile.write_file(my_data) if exist_id is False: resp.status = falcon.HTTP_NO_CONTENT else: resp.status = falcon.HTTP_OK resp.body = json.dumps(exist_id)
def make_id_int(self, idd): try: return int(idd) except ValueError: raise errors.HTTPBadRequest('id must be integer type')