Skip to content

Commit

Permalink
Merge pull request #438 from bento-platform/feat/dataset-resources-en…
Browse files Browse the repository at this point in the history
…dpoint

feat: add dataset resources endpoint
  • Loading branch information
davidlougheed authored Sep 22, 2023
2 parents 1fa9330 + e68a846 commit 67a668f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
10 changes: 10 additions & 0 deletions chord_metadata_service/chord/api_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from django_filters.rest_framework import DjangoFilterBackend
from chord_metadata_service.cleanup.run_all import run_all_cleanup

from chord_metadata_service.resources.serializers import ResourceSerializer
from chord_metadata_service.restapi.api_renderers import PhenopacketsRenderer, JSONLDDatasetRenderer, RDFDatasetRenderer
from chord_metadata_service.restapi.pagination import LargeResultsSetPagination

Expand Down Expand Up @@ -85,6 +86,15 @@ def dats(self, _request, *_args, **_kwargs):
dataset = self.get_object()
return Response(json.loads(dataset.dats_file))

@action(detail=True, methods=["get"])
def resources(self, _request, *_args, **_kwargs):
"""
Retrieve all resources (phenopackets/additional_resources) for a dataset and return a JSON response serialized
using ResourceSerializer
"""
dataset = self.get_object()
return Response(ResourceSerializer(dataset.resources.all(), many=True).data)

@async_to_sync
async def destroy(self, request, *args, **kwargs):
get_obj_async = sync_to_async(self.get_object)
Expand Down
29 changes: 29 additions & 0 deletions chord_metadata_service/chord/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,35 @@ def test_dats(self):
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertDictEqual(response.data, json.loads(payload['dats_file']))

def test_resources(self):
resource = {
"id": "NCBITaxon:2023-09-14",
"name": "NCBI Taxonomy OBO Edition",
"version": "2023-09-14",
"namespace_prefix": "NCBITaxon",
"url": "http://purl.obolibrary.org/obo/ncbitaxon/2023-09-14/ncbitaxon.owl",
"iri_prefix": "http://purl.obolibrary.org/obo/NCBITaxon_",
}

r = self.client.post("/api/resources", data=json.dumps(resource), content_type="application/json")
self.assertEqual(r.status_code, status.HTTP_201_CREATED)

r = self.client.post(
"/api/datasets",
data=json.dumps({
**valid_dataset_1(self.project["identifier"]),
"additional_resources": [resource["id"]],
}),
content_type="application/json")
self.assertEqual(r.status_code, status.HTTP_201_CREATED)

dataset_id = Dataset.objects.first().identifier
r = self.client.get(f"/api/datasets/{dataset_id}/resources")
self.assertEqual(r.status_code, status.HTTP_200_OK)
self.assertEqual(len(r.data), 1)
self.assertEqual(r.data[0]["id"], resource["id"])


# TODO: Update Dataset
# TODO: Delete Dataset

Expand Down

0 comments on commit 67a668f

Please sign in to comment.