Skip to content

Commit

Permalink
fixed DTR endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalGawor committed Apr 4, 2024
1 parent 7a3b316 commit dab2cac
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
1 change: 1 addition & 0 deletions dogapi/dogapi/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from doglib import DOG
from doglib import expand_datatype as doglib_expand_datatype

dog = DOG()

31 changes: 22 additions & 9 deletions dogapi/dogapi/views_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@
from rest_framework.request import Request
from typing import List

from .models import dog
from .models import dog, doglib_expand_datatype
from .schemas import fetch_response_schema, identify_response_schema, is_collection_response_schema, pid_queryparam, \
sniff_response_schema


def parse_pid_queryparam(request: Request) -> List[str]:
def parse_queryparam(request: Request, param_name: str) -> List[str]:
"""
Parses queryparameters from direct API call (PHP-like format ?param[]=val1&param[]=val2, both with and without [])
and via Swagger UI (?param=val1,val2)
"""
query_pid_candidates = request.GET.getlist('pid')
query_param_candidates = request.GET.getlist(param_name)
# Swagger UI returns a 1 element list with comma separated values of parameter, e.g. ["string,string,string"]
pid_candidates = [pid for pid_candidate in query_pid_candidates for pid in pid_candidate.split(',')]
return pid_candidates
param_candidates = [param for param_candidate in query_param_candidates for param in param_candidate.split(',')]
return param_candidates


@swagger_auto_schema(method="get",
Expand All @@ -41,7 +41,7 @@ def fetch(request: Request) -> Response:
Returns [{pid: {fetch_results}}]
"""
pids = parse_pid_queryparam(request)
pids = parse_queryparam(request, 'pid')
fetch_results = {pid: dog.fetch(pid) for pid in pids}
if fetch_results:
return Response(fetch_results, status=200)
Expand All @@ -65,7 +65,7 @@ def identify(request: Request) -> Response:
Returns [{pid: <identify_result>}]
"""
pids = parse_pid_queryparam(request)
pids = parse_queryparam(request, 'pid')
identify_result = {pid: dog.identify(pid) for pid in pids}
if identify_result:
return Response(identify_result, status=200)
Expand All @@ -89,7 +89,7 @@ def is_collection(request: Request) -> Response:
Returns [{pid: bool}]
"""
pids = parse_pid_queryparam(request)
pids = parse_queryparam(request, 'pid')
is_collection_result = {pid: dog.is_collection(pid) for pid in pids}
if is_collection_result:
return Response(is_collection_result, status=200)
Expand All @@ -113,9 +113,22 @@ def sniff(request: Request) -> Response:
Returns [{pid: <sniff_result>}]
"""
pids = parse_pid_queryparam(request)
pids = parse_queryparam(request, 'pid')
sniff_result = {pid: dog.sniff(pid) for pid in pids}
if sniff_result:
return Response(sniff_result, status=200)
else:
return Response(f"Persistent Identifier(s) {pids} is either not correct or has been not recognised", status=400)


@permission_classes([AllowAny])
@api_view(['GET'])
def expand_datatype(request: Request) -> Response:
data_types = parse_queryparam(request, 'data_type')
expanded_datatypes: dict = {}
for data_type in data_types:
expanded_datatypes.update(doglib_expand_datatype(data_type))
if expanded_datatypes:
return Response(expanded_datatypes, status=200)
else:
return Response(f"MIME data type(s) {data_types} is either not correct or has been not recognised", status=400)
3 changes: 2 additions & 1 deletion dogconfig/dogproject/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from drf_yasg.views import get_schema_view
from rest_framework.permissions import AllowAny

from dogapi.views_api import fetch, identify, sniff
from dogapi.views_api import fetch, identify, sniff, expand_datatype

openapi_info = openapi.Info(title="Snippets API",
default_version='v1',
Expand All @@ -40,4 +40,5 @@
path('fetch/', fetch, name='fetch'),
path('identify/', identify, name='identify'),
path('sniff/', sniff, name='sniff'),
path('expanddatatype/', expand_datatype, name='expand data type'),
]

0 comments on commit dab2cac

Please sign in to comment.