Coverage for apis_core/apis_metainfo/viewsets.py: 50%

18 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2024-11-22 07:51 +0000

1from django.http import HttpResponseRedirect, QueryDict 

2from django.shortcuts import get_object_or_404 

3from drf_spectacular.utils import OpenApiParameter, OpenApiTypes, extend_schema 

4from rest_framework import viewsets 

5from rest_framework.response import Response 

6 

7from apis_core.apis_metainfo.models import Uri 

8 

9 

10class UriToObjectViewSet(viewsets.ViewSet): 

11 """ 

12 This API route provides an endpoint for resolving URIs and forwarding 

13 them to the endpoint in the local instance. Pass a `uri` request 

14 parameter to resolve the uri. 

15 """ 

16 

17 @extend_schema( 

18 parameters=[ 

19 OpenApiParameter( 

20 "uri", OpenApiTypes.URI, OpenApiParameter.QUERY 

21 ), # path variable was overridden 

22 ], 

23 responses={301: None}, 

24 description="This API route provides an endpoint for resolving URIs and forwarding them to the endpoint in the local instance. Pass a `uri` request parameter to resolve the uri.", 

25 ) 

26 def list(self, request): 

27 params = request.query_params.dict() 

28 uri = params.pop("uri", None) 

29 if uri: 

30 u = get_object_or_404(Uri, uri=request.query_params.get("uri")) 

31 r = u.root_object.get_api_detail_endpoint() 

32 if params: 

33 r += "?" + QueryDict.from_keys(params).urlencode() 

34 return HttpResponseRedirect(r) 

35 return Response()