def _entity(catalog_name, collection_name, entity_id, view=None): """Returns the entity within the specified collection with the specified id An individual entity is returned. :param catalog_name: e.g. meetbouten :param collection_name: e.g. meting :param entity_id: unique identifier of the entity :param view: the database view that's being used to get the entity, defaults to the entity table :return: """ if GOBModel().get_collection(catalog_name, collection_name): view = request.args.get('view', None) # If a view is requested and doesn't exist return a 404 if view and not GOBViews().get_view(catalog_name, collection_name, view): return not_found( f'{catalog_name}.{collection_name}?view={view} not found') view_name = GOBViews().get_view(catalog_name, collection_name, view)['name'] if view else None result = get_entity(catalog_name, collection_name, entity_id, view_name) return hal_response(result) if result is not None else not_found( f'{catalog_name}.{collection_name}:{entity_id} not found') else: return not_found(f'{catalog_name}.{collection_name} not found')
def _reference_collection(catalog_name, collection_name, entity_id, reference_path): """Returns the (very many) references from an entity within the specified collection with the specified id An list of references is returned. :param catalog_name: e.g. meetbouten :param collection_name: e.g. meting :param entity_id: unique identifier of the entity :param reference: unique identifier of the reference attribute e.g. ligt_in_buurt :param view: the database view that's being used to get the entity, defaults to the entity table :return: """ model = GOBModel() entity_collection = model.get_collection(catalog_name, collection_name) if entity_collection: # Get the reference reference_name = reference_path.replace('-', '_') reference = model.get_collection( catalog_name, collection_name)['references'].get(reference_name) # Check if the source entity exists entity = get_entity(catalog_name, collection_name, entity_id) if entity and reference: page = int(request.args.get('page', 1)) page_size = int(request.args.get('page_size', 100)) stream = request.args.get('stream', None) == "true" ndjson = request.args.get('ndjson', None) == "true" if stream: entities, convert = query_reference_entities( catalog_name, collection_name, reference_name, entity_id) return Response(stream_entities(entities, convert), mimetype='application/json') elif ndjson: entities, convert = query_reference_entities( catalog_name, collection_name, reference_name, entity_id) return Response(ndjson_entities(entities, convert), mimetype='application/x-ndjson') else: result, links = _reference_entities(catalog_name, collection_name, reference_name, entity_id, page, page_size) return hal_response(data=result, links=links) response = not_found(f'{catalog_name}.{collection_name}:{entity_id} not found') \ if not entity else not_found(f'{catalog_name}.{collection_name}:{entity_id}:{reference_name} not found') return response else: return not_found(f'{catalog_name}.{collection_name} not found')
def _catalog(catalog_name): """Return the details of a specific GOB catalog :param catalog_name: e.g. meetbouten :return: the details of the specified catalog {name, href} """ catalog = GOBModel().get_catalog(catalog_name) if catalog: result = { 'name': catalog_name, 'abbreviation': catalog['abbreviation'], 'description': catalog['description'], 'version': catalog['version'], 'collections': [a for a in catalog['collections'].keys()], '_embedded': { 'collections': [{ 'name': collection_name, 'abbreviation': collection['abbreviation'], '_links': { 'self': { 'href': f'/gob/{catalog_name}/{collection_name}/' } } } for collection_name, collection in GOBModel().get_collections(catalog_name).items()] } } return hal_response(result) else: return not_found(f"Catalog {catalog_name} not found")
def test_not_found(monkeypatch): before_each_response_test(monkeypatch) from gobapi.response import not_found assert (not_found('msg') == ('{"error": 404, "text": "msg"}', 404, { 'Content-Type': 'application/json' }))
def _collection(catalog_name, collection_name): """Returns the list of entities within the specified collection A list of entities is returned. This output is paged, default page 1 page size 100 :param catalog_name: e.g. meetbouten :param collection_name: e.g. meting :return: """ if GOBModel().get_collection(catalog_name, collection_name): page = int(request.args.get('page', 1)) page_size = int(request.args.get('page_size', 100)) view = request.args.get('view', None) stream = request.args.get('stream', None) == "true" ndjson = request.args.get('ndjson', None) == "true" # If a view is requested and doesn't exist return a 404 if view and not GOBViews().get_view(catalog_name, collection_name, view): return not_found( f'{catalog_name}.{collection_name}?view={view} not found') view_name = GOBViews().get_view(catalog_name, collection_name, view)['name'] if view else None if stream: entities, convert = query_entities(catalog_name, collection_name, view_name) result = stream_entities(entities, convert) return WorkerResponse.stream_with_context( result, mimetype='application/json') elif ndjson: entities, convert = query_entities(catalog_name, collection_name, view_name) result = ndjson_entities(entities, convert) return WorkerResponse.stream_with_context( result, mimetype='application/x-ndjson') else: result, links = _entities(catalog_name, collection_name, page, page_size, view_name) return hal_response(data=result, links=links) else: return not_found(f'{catalog_name}.{collection_name} not found')
def _states(): """Returns the states for the supplied list of collections All states for a collection with the related collections are returned. The list of collections can be passed as an URL parameter: ?collections=gebieden:wijken,gebieden:stadsdelen :return: """ collection_names = request.args.get('collections') page = int(request.args.get('page', 1)) page_size = int(request.args.get('page_size', 100)) offset = (page - 1) * page_size if collection_names: collections = [] for c in collection_names.split(','): collections.append(c.split(':')) entities, total_count = get_states(collections, offset=offset, limit=page_size) num_pages = (total_count + page_size - 1) // page_size result = { 'total_count': total_count, 'page_size': page_size, 'pages': num_pages, 'results': entities } links = { 'next': get_page_ref(page + 1, num_pages), 'previous': get_page_ref(page - 1, num_pages) } return hal_response(result, links) else: return not_found(f'No collections requested')