diff --git a/dogapi/dogapi/models.py b/dogapi/dogapi/models.py index cf71136..9bcf601 100644 --- a/dogapi/dogapi/models.py +++ b/dogapi/dogapi/models.py @@ -1,4 +1,5 @@ from doglib import DOG +from doglib import expand_datatype as doglib_expand_datatype dog = DOG() diff --git a/dogapi/dogapi/views_api.py b/dogapi/dogapi/views_api.py index 16574ca..10469dd 100644 --- a/dogapi/dogapi/views_api.py +++ b/dogapi/dogapi/views_api.py @@ -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¶m[]=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", @@ -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) @@ -65,7 +65,7 @@ def identify(request: Request) -> Response: Returns [{pid: }] """ - 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) @@ -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) @@ -113,9 +113,22 @@ def sniff(request: Request) -> Response: Returns [{pid: }] """ - 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) diff --git a/dogconfig/dogproject/urls.py b/dogconfig/dogproject/urls.py index 171b756..e0f6c50 100644 --- a/dogconfig/dogproject/urls.py +++ b/dogconfig/dogproject/urls.py @@ -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', @@ -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'), ]