3
3
Module that contains the factory to create response handlers.
4
4
"""
5
5
6
- from typing import TYPE_CHECKING , Union
7
-
8
- from rest_framework .response import Response
6
+ import contextlib
7
+ from typing import TYPE_CHECKING
9
8
10
9
from openapi_tester .response_handler import (
11
10
DjangoNinjaResponseHandler ,
@@ -26,8 +25,20 @@ class ResponseHandlerFactory:
26
25
27
26
@staticmethod
28
27
def create (
29
- * request_args , response : Union [Response , "HttpResponse" ], ** kwargs
28
+ * request_args ,
29
+ response : "HttpResponse" ,
30
+ ** kwargs ,
30
31
) -> "ResponseHandler" :
31
- if isinstance (response , Response ):
32
- return DRFResponseHandler (response = response )
33
- return DjangoNinjaResponseHandler (* request_args , response = response , ** kwargs )
32
+ with contextlib .suppress (ImportError ):
33
+ from rest_framework .response import Response as DRFResponse
34
+
35
+ if isinstance (response , DRFResponse ):
36
+ return DRFResponseHandler (response = response )
37
+ with contextlib .suppress (ImportError ):
38
+ from ninja .testing .client import NinjaResponse
39
+
40
+ if isinstance (response , NinjaResponse ):
41
+ return DjangoNinjaResponseHandler (
42
+ * request_args , response = response , ** kwargs
43
+ )
44
+ raise TypeError (f"Can't pick response handler for { response } !" )
0 commit comments