Skip to content

Commit f2a4044

Browse files
committed
Add Validate url endpoint
Changes to be committed: modified: biocompute/apis.py modified: biocompute/urls.py modified: tests/test_apis/test_biocompute/test_objects_drafts_create.py modified: tests/test_apis/test_biocompute/test_objects_drafts_publish.py
1 parent 8213344 commit f2a4044

File tree

4 files changed

+95
-2
lines changed

4 files changed

+95
-2
lines changed

biocompute/apis.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,3 +716,95 @@ def get(self, request, bco_accession, bco_version):
716716
bco_counter_increment(bco_instance)
717717
return Response(status=status.HTTP_200_OK, data=bco_instance.contents)
718718

719+
class ValidateBcoApi(APIView):
720+
"""Bulk Validate BCOs [Bulk Enabled]
721+
722+
--------------------
723+
724+
Bulk operation to validate BCOs.
725+
726+
```JSON
727+
[
728+
{...BCO CONTENTS...},
729+
{...BCO CONTENTS...}
730+
]
731+
732+
"""
733+
734+
authentication_classes = []
735+
permission_classes = [AllowAny]
736+
737+
@swagger_auto_schema(
738+
operation_id="api_bco_validate",
739+
request_body=openapi.Schema(
740+
type=openapi.TYPE_ARRAY,
741+
title="Validate BCO against Schema",
742+
items=openapi.Schema(
743+
type=openapi.TYPE_OBJECT,
744+
required=["contents"],
745+
description="Contents of the BCO.",
746+
example=BCO_000001_DRAFT
747+
748+
),
749+
description="Validate BCO against IEEE schema.",
750+
),
751+
responses={
752+
200: "All BCO validations are successful.",
753+
207: "Some or all BCO validations failed. Each object submitted"
754+
" will have it's own response object with it's own status"
755+
" message:\n",
756+
400: "All BCO validations failed."
757+
},
758+
tags=["BCO Management"],
759+
)
760+
def post(self, request):
761+
validator = BcoValidator()
762+
response_data = []
763+
rejected_requests = False
764+
accepted_requests = False
765+
data = request.data
766+
if 'POST_validate_bco' in request.data:
767+
data = legacy_api_converter(data=request.data)
768+
769+
for index, object in enumerate(data):
770+
bco_results = validator.parse_and_validate(bco=object)
771+
identifier, results = bco_results.popitem()
772+
773+
if results["number_of_errors"] > 0:
774+
rejected_requests = True
775+
bco_status = "FAILED"
776+
status_code = 400
777+
message = "BCO not valid"
778+
else:
779+
accepted_requests = True
780+
bco_status = "SUCCESS"
781+
status_code = 200
782+
message = "BCO valid"
783+
784+
response_data.append(response_constructor(
785+
identifier = identifier,
786+
status=bco_status,
787+
code=status_code,
788+
message=message,
789+
data=results
790+
))
791+
792+
if accepted_requests is False and rejected_requests == True:
793+
return Response(
794+
status=status.HTTP_400_BAD_REQUEST,
795+
data=response_data
796+
)
797+
798+
if accepted_requests is True and rejected_requests is True:
799+
return Response(
800+
status=status.HTTP_207_MULTI_STATUS,
801+
data=response_data
802+
)
803+
804+
if accepted_requests is True and rejected_requests is False:
805+
return Response(
806+
status=status.HTTP_200_OK,
807+
data=response_data
808+
)
809+
810+
return Response(status=status.HTTP_200_OK, data={})

biocompute/urls.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
DraftsCreateApi,
88
DraftsModifyApi,
99
DraftsPublishApi,
10+
ValidateBcoApi,
1011
)
1112

1213
urlpatterns = [
1314
path("objects/drafts/create/", DraftsCreateApi.as_view()),
1415
path("objects/drafts/modify/", DraftsModifyApi.as_view()),
1516
path("objects/drafts/publish/", DraftsPublishApi.as_view()),
17+
path("objects/validate/", ValidateBcoApi.as_view()),
1618
]

tests/test_apis/test_biocompute/test_objects_drafts_create.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ def test_legacy_successful_creation(self):
6060

6161
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token.key)
6262
response = self.client.post('/api/objects/drafts/create/', self.legacy_data, format='json')
63-
print(response.data)
6463
self.assertEqual(response.status_code, 200)
6564

6665
def test_successful_creation(self):
@@ -69,7 +68,6 @@ def test_successful_creation(self):
6968

7069
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token.key)
7170
response = self.client.post('/api/objects/drafts/create/', self.data, format='json')
72-
print(response.data)
7371
self.assertEqual(response.status_code, 200)
7472

7573
def test_partial_failure(self):

tests/test_apis/test_biocompute/test_objects_drafts_publish.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ def test_successful_publish(self):
7171

7272
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token.key)
7373
response = self.client.post('/api/objects/drafts/publish/', self.data, format='json')
74+
print(response.data)
7475
self.assertEqual(response.status_code, 200)
7576

7677
def test_partial_failure(self):

0 commit comments

Comments
 (0)