def read_one(id_: str, dao: GenericSQLDAO = None): result = dao.get(id_=id_) if not result: return success_response(status_code=404, message="EntityForTest not found in database", data={"id_": id_}) return success_response(message="EntityForTest retrieved", data={"EntityForTest": dict(result)})
def read_one(id_: str, phone_id: str, dao: GenericSQLDAO = None): result = dao.get(id_=phone_id) if result.contact.id_ != id_: result = None if not result: return success_response(status_code=404, message="Phone not found in database", data={"id_": id_}) return success_response(message="Phone retrieved", data={"Phone": dict(result)})
def create(entity: dict, dao: GenericSQLDAO = None): entity_to_create = EntityForTest(**entity) dao.create(entity=entity_to_create) return success_response(message="EntityForTest created", data={"EntityForTest": dict(entity_to_create)})
def read(length: int = 20, offset: int = 0, dao: GenericSQLDAO = None, **kwargs): filters = dict() entity_attributes = [field.name for field in fields(EntityForTest)] for key, value in kwargs.items(): if key not in entity_attributes: continue filters[key] = value.split(',') \ if len(str(value).split(',')) > 1 \ else value total, results = dao.get_all(length=length, offset=offset, filters=filters if filters else None) return success_response(message="List of entityfortest", data={ "total": total, "results": [dict(result) for result in results] })
def update(id_: str, phone_id: str, entity: dict, dao: GenericSQLDAO = None): entity_to_update = dao.get(phone_id) if entity_to_update.contact.id_ != id_: entity_to_update = None if not entity_to_update: return error_response(status_code=404, message="Phone not found", data={"id_": id_}) entity_fields = dao.fields.keys() for key, value in entity.items(): if key not in entity_fields: raise KeyError("{key} not in {entity}" .format(key=key, entity=dao.return_class)) entity_to_update.__dict__[key] = value dao.update(entity_to_update) return success_response(message="Phone updated", data={"Phone": dict(entity_to_update)})
def create(id_: str, entity: dict, dao: GenericSQLDAO = None): entity_to_create = Phone(**entity) entity_to_create.contact = Contact(id_=id_) dao.create(entity=entity_to_create) return success_response(message="Phone created", data={"Phone": dict(entity_to_create)})
def delete(id_: str, dao: GenericSQLDAO): entity = dao.get(id_=id_) if not entity: return error_response(status_code=404, message="EntityForTest not found", data={"id_": id_}) dao.remove(entity) return success_response(message="EntityForTest deleted", data={"EntityForTest": dict(entity)})
def test_success_response(self, mocker, data): default_response_mock = mocker.patch("nova_api.default_response", return_value="OK") default_response_mock.return_value = 1 ret_val = nova_api.success_response(data=data) assert (default_response_mock.mock_calls == [ call(success=True, status_code=200, message="OK", data={} if data is None else data) ]) assert ret_val == 1
def read(length: int = 20, offset: int = 0, dao: GenericSQLDAO = None, **kwargs): for key, value in kwargs.items(): kwargs[key] = value.split(',') \ if len(value.split(',')) > 1 \ else value total, results = dao.get_all(length=length, offset=offset, filters=kwargs if len(kwargs) > 0 else None) return success_response(message="List of contact", data={"total": total, "results": [dict(result) for result in results]})
def delete(id_: str, phone_id: str, dao: GenericSQLDAO): entity = dao.get(id_=phone_id) if entity.contact.id_ != id_: entity = None if not entity: return error_response(status_code=404, message="Phone not found", data={"id_": id_}) dao.remove(entity) return success_response(message="Phone deleted", data={"Phone": dict(entity)})
def probe(dao: GenericSQLDAO = None): total, _ = dao.get_all(length=1, offset=0, filters=None) return success_response(message="API Ready", data={"available": total})
def probe(id_: str = None, dao: GenericSQLDAO = None): total, results = dao.get_all(length=1, offset=0, filters={"contact": id_}) return success_response(message="API Ready", data={"available": total})