File tree Expand file tree Collapse file tree 2 files changed +28
-2
lines changed Expand file tree Collapse file tree 2 files changed +28
-2
lines changed Original file line number Diff line number Diff line change @@ -118,6 +118,16 @@ class HTTPServiceUnavailable(HTTPException):
118
118
119
119
120
120
def from_response (response , details = None ):
121
- """Return an instance of an HTTPException based on httplib response."""
122
- cls = _code_map .get (response .status , HTTPException )
121
+ """Return an instance of an HTTPException based on http response."""
122
+ if hasattr (response , "status" ):
123
+ # it is response from HTTPClient (httplib)
124
+ code = response .status
125
+ elif hasattr (response , "status_code" ):
126
+ # it is response from SessionClient (requests)
127
+ code = response .status_code
128
+ else :
129
+ # it is something unexpected
130
+ raise TypeError ("Function 'from_response' expects only response object"
131
+ " from httplib or requests libraries." )
132
+ cls = _code_map .get (code , HTTPException )
123
133
return cls (details )
Original file line number Diff line number Diff line change @@ -69,3 +69,19 @@ def test_str_faultstring(self):
69
69
{"error_message" : {"faultstring" : "oops" }}))
70
70
ret_str = k + " (HTTP " + str (exception .code ) + ") ERROR oops"
71
71
self .assertEqual (ret_str , str (exception ))
72
+
73
+ def test_from_response (self ):
74
+ class HTTPLibLikeResponse (object ):
75
+ status = 400
76
+
77
+ class RequestsLikeResponse (object ):
78
+ status_code = 401
79
+
80
+ class UnexpectedResponse (object ):
81
+ code = 200
82
+
83
+ self .assertEqual (HTTPLibLikeResponse .status ,
84
+ exc .from_response (HTTPLibLikeResponse ).code )
85
+ self .assertEqual (RequestsLikeResponse .status_code ,
86
+ exc .from_response (RequestsLikeResponse ).code )
87
+ self .assertRaises (TypeError , exc .from_response , UnexpectedResponse )
You can’t perform that action at this time.
0 commit comments