async def resource_id_subpath_put(resource_type, resource_id, subpath, body: http.Body, headers: http.Headers, storage: Storage): try: if storage.is_file(resource_type, subpath): content_type = headers.get('Content-Type') revision = headers.get('Revision') return await storage.put_file(resource_type, resource_id, subpath, body, revision, content_type) else: data = JSONParser().parse(body) return await storage.put_subpath(resource_type, resource_id, subpath, data) except ResourceTypeNotFound: raise NotFound({ 'error_code': 'ResourceTypeDoesNotExist', 'resource_type': resource_type, 'message': 'Resource type does not exist', }) except ResourceNotFound: raise NotFound({ 'error_code': 'ItemDoesNotExist', 'item_id': resource_id, 'message': "Item does not exist", }) except WrongRevision as e: raise Conflict({ 'error_code': 'WrongRevision', 'item_id': resource_id, 'current': e.current, 'update': e.update, 'message': ( 'Updating resource {item_id} failed: resource currently has revision {current}, update wants to ' 'update {update}.' ), })
async def resource_id_delete(resource_type, resource_id, storage: Storage): try: return await storage.delete(resource_type, resource_id) except ResourceTypeNotFound: raise NotFound({ 'error_code': 'ResourceTypeDoesNotExist', 'resource_type': resource_type, 'message': 'Resource type does not exist', }) except ResourceNotFound: raise NotFound({ 'error_code': 'ItemDoesNotExist', 'item_id': resource_id, 'message': "Item does not exist", }) except WrongRevision as e: raise Conflict({ 'error_code': 'WrongRevision', 'item_id': resource_id, 'current': e.current, 'update': e.update, 'message': ( 'Updating resource {item_id} failed: resource currently has revision {current}, update wants to ' 'update {update}.' ), })
async def resource_id_get(resource_type, resource_id, storage: Storage): try: return await storage.get(resource_type, resource_id) except ResourceTypeNotFound: raise NotFound({ 'error_code': 'ResourceTypeDoesNotExist', 'resource_type': resource_type, 'message': 'Resource type does not exist', }) except ResourceNotFound: raise NotFound({ 'error_code': 'ItemDoesNotExist', 'item_id': resource_id, 'message': "Item does not exist", })
async def resource_post(resource_type, data: http.RequestData, storage: Storage): try: return await storage.create(resource_type, data) except ResourceTypeNotFound: raise NotFound({ 'error_code': 'ResourceTypeDoesNotExist', 'resource_type': resource_type, 'message': 'Resource type does not exist', })
async def resource_get(resource_type, storage: Storage): try: return { 'resources': [ {'id': resource_id} for resource_id in await storage.list(resource_type) ], } except ResourceTypeNotFound: raise NotFound({ 'error_code': 'ResourceTypeDoesNotExist', 'resource_type': resource_type, 'message': 'Resource type does not exist', })
async def resource_search(resource_type, query: PathWildcard, storage: Storage): try: return { 'resources': [ resource for resource in await storage.search(resource_type, query) ], } except ResourceTypeNotFound: raise NotFound({ 'error_code': 'ResourceTypeDoesNotExist', 'resource_type': resource_type, 'message': 'Resource type does not exist', })
async def resource_id_subpath_get(resource_type, resource_id, subpath, storage: Storage): try: if storage.is_file(resource_type, subpath): data = await storage.get_file(resource_type, resource_id, subpath) return Response(data['blob'], status=200, content_type=data['content-type'], headers={ 'Revision': data['revision'], }) else: return await storage.get_subpath(resource_type, resource_id, subpath) except ResourceTypeNotFound: raise NotFound({ 'error_code': 'ResourceTypeDoesNotExist', 'resource_type': resource_type, 'message': 'Resource type does not exist', }) except ResourceNotFound: raise NotFound({ 'error_code': 'ItemDoesNotExist', 'item_id': resource_id, 'message': "Item does not exist", })