@@ -65,25 +65,25 @@ def __init__(
65
65
66
66
def _handle_success (
67
67
self ,
68
- body : str ,
68
+ raw_body : str ,
69
69
uri : str ,
70
70
model_class : Union [Type [Factors ], Type [Score ], Type [Insights ]],
71
71
) -> Union [Score , Factors , Insights ]:
72
72
"""Handle successful response."""
73
73
try :
74
- decoded_body = json .loads (body )
74
+ decoded_body = json .loads (raw_body )
75
75
except ValueError as ex :
76
76
raise MinFraudError (
77
- f"Received a 200 response but could not decode the response as JSON: { body } " ,
77
+ f"Received a 200 response but could not decode the response as JSON: { raw_body } " ,
78
78
200 ,
79
79
uri ,
80
80
) from ex
81
- if "ip_address" in body :
81
+ if "ip_address" in decoded_body :
82
82
decoded_body ["ip_address" ]["_locales" ] = self ._locales
83
83
return model_class (decoded_body ) # type: ignore
84
84
85
85
def _exception_for_error (
86
- self , status : int , content_type : str , body : str , uri : str
86
+ self , status : int , content_type : str , raw_body : str , uri : str
87
87
) -> Union [
88
88
AuthenticationError ,
89
89
InsufficientFundsError ,
@@ -94,13 +94,13 @@ def _exception_for_error(
94
94
"""Returns the exception for the error responses."""
95
95
96
96
if 400 <= status < 500 :
97
- return self ._exception_for_4xx_status (status , content_type , body , uri )
97
+ return self ._exception_for_4xx_status (status , content_type , raw_body , uri )
98
98
if 500 <= status < 600 :
99
- return self ._exception_for_5xx_status (status , body , uri )
100
- return self ._exception_for_unexpected_status (status , body , uri )
99
+ return self ._exception_for_5xx_status (status , raw_body , uri )
100
+ return self ._exception_for_unexpected_status (status , raw_body , uri )
101
101
102
102
def _exception_for_4xx_status (
103
- self , status : int , content_type : str , body : str , uri : str
103
+ self , status : int , content_type : str , raw_body : str , uri : str
104
104
) -> Union [
105
105
AuthenticationError ,
106
106
InsufficientFundsError ,
@@ -109,36 +109,38 @@ def _exception_for_4xx_status(
109
109
PermissionRequiredError ,
110
110
]:
111
111
"""Returns exception for error responses with 4xx status codes."""
112
- if not body :
112
+ if not raw_body :
113
113
return HTTPError (
114
- f"Received a { status } error with no body" , status , uri , body
114
+ f"Received a { status } error with no body" , status , uri , raw_body
115
115
)
116
116
if content_type .find ("json" ) == - 1 :
117
117
return HTTPError (
118
- f"Received a { status } with the following body: { body } " ,
118
+ f"Received a { status } with the following body: { raw_body } " ,
119
119
status ,
120
120
uri ,
121
- body ,
121
+ raw_body ,
122
122
)
123
123
try :
124
- decoded_body = json .loads (body )
124
+ decoded_body = json .loads (raw_body )
125
125
except ValueError :
126
126
return HTTPError (
127
- f"Received a { status } error but it did not include the expected JSON body: { body } " ,
127
+ f"Received a { status } error but it did not "
128
+ + f"include the expected JSON body: { raw_body } " ,
128
129
status ,
129
130
uri ,
130
- body ,
131
+ raw_body ,
131
132
)
132
133
else :
133
134
if "code" in decoded_body and "error" in decoded_body :
134
135
return self ._exception_for_web_service_error (
135
136
decoded_body .get ("error" ), decoded_body .get ("code" ), status , uri
136
137
)
137
138
return HTTPError (
138
- f"Error response contains JSON but it does not specify code or error keys: { body } " ,
139
+ "Error response contains JSON but it does not "
140
+ + f"specify code or error keys: { raw_body } " ,
139
141
status ,
140
142
uri ,
141
- body ,
143
+ raw_body ,
142
144
)
143
145
144
146
@staticmethod
@@ -168,29 +170,29 @@ def _exception_for_web_service_error(
168
170
@staticmethod
169
171
def _exception_for_5xx_status (
170
172
status : int ,
171
- body : Optional [str ],
173
+ raw_body : Optional [str ],
172
174
uri : str ,
173
175
) -> HTTPError :
174
176
"""Returns exception for error response with 5xx status codes."""
175
177
return HTTPError (
176
178
f"Received a server error ({ status } ) for { uri } " ,
177
179
status ,
178
180
uri ,
179
- body ,
181
+ raw_body ,
180
182
)
181
183
182
184
@staticmethod
183
185
def _exception_for_unexpected_status (
184
186
status : int ,
185
- body : Optional [str ],
187
+ raw_body : Optional [str ],
186
188
uri : str ,
187
189
) -> HTTPError :
188
190
"""Returns exception for responses with unexpected status codes."""
189
191
return HTTPError (
190
192
f"Received an unexpected HTTP status ({ status } ) for { uri } " ,
191
193
status ,
192
194
uri ,
193
- body ,
195
+ raw_body ,
194
196
)
195
197
196
198
@@ -374,10 +376,10 @@ async def report(
374
376
async with await self ._do_request (uri , prepared_request ) as response :
375
377
status = response .status
376
378
content_type = response .content_type
377
- body = await response .text ()
379
+ raw_body = await response .text ()
378
380
379
381
if status != 204 :
380
- raise self ._exception_for_error (status , content_type , body , uri )
382
+ raise self ._exception_for_error (status , content_type , raw_body , uri )
381
383
382
384
async def _response_for (
383
385
self ,
@@ -392,11 +394,11 @@ async def _response_for(
392
394
async with await self ._do_request (uri , prepared_request ) as response :
393
395
status = response .status
394
396
content_type = response .content_type
395
- body = await response .text ()
397
+ raw_body = await response .text ()
396
398
397
399
if status != 200 :
398
- raise self ._exception_for_error (status , content_type , body , uri )
399
- return self ._handle_success (body , uri , model_class )
400
+ raise self ._exception_for_error (status , content_type , raw_body , uri )
401
+ return self ._handle_success (raw_body , uri , model_class )
400
402
401
403
async def _do_request (
402
404
self , uri : str , data : Dict [str , Any ]
@@ -617,9 +619,9 @@ def report(self, report: Dict[str, Optional[str]], validate: bool = True) -> Non
617
619
response = self ._do_request (uri , prepared_request )
618
620
status = response .status_code
619
621
content_type = response .headers ["Content-Type" ]
620
- body = response .text
622
+ raw_body = response .text
621
623
if status != 204 :
622
- raise self ._exception_for_error (status , content_type , body , uri )
624
+ raise self ._exception_for_error (status , content_type , raw_body , uri )
623
625
624
626
def _response_for (
625
627
self ,
@@ -635,10 +637,10 @@ def _response_for(
635
637
response = self ._do_request (uri , prepared_request )
636
638
status = response .status_code
637
639
content_type = response .headers ["Content-Type" ]
638
- body = response .text
640
+ raw_body = response .text
639
641
if status != 200 :
640
- raise self ._exception_for_error (status , content_type , body , uri )
641
- return self ._handle_success (body , uri , model_class )
642
+ raise self ._exception_for_error (status , content_type , raw_body , uri )
643
+ return self ._handle_success (raw_body , uri , model_class )
642
644
643
645
def _do_request (self , uri : str , data : Dict [str , Any ]) -> Response :
644
646
return self ._session .post (
0 commit comments