7
7
"""
8
8
9
9
import json
10
+ from collections .abc import Sequence
10
11
from functools import partial
11
- from typing import Any , Callable , cast , Dict , Optional , Sequence , Union
12
+ from typing import Any , Callable , Dict , Optional , Union , cast
12
13
13
14
import aiohttp
14
15
import aiohttp .http
15
16
import requests
16
17
import requests .utils
17
18
from requests .models import Response
18
19
19
- from .version import __version__
20
20
from .errors import (
21
- MinFraudError ,
22
- HTTPError ,
23
21
AuthenticationError ,
22
+ HTTPError ,
24
23
InsufficientFundsError ,
25
24
InvalidRequestError ,
25
+ MinFraudError ,
26
26
PermissionRequiredError ,
27
27
)
28
28
from .models import Factors , Insights , Score
29
29
from .request import prepare_report , prepare_transaction
30
-
30
+ from . version import __version__
31
31
32
32
_AIOHTTP_UA = f"minFraud-API/{ __version__ } { aiohttp .http .SERVER_SOFTWARE } "
33
33
@@ -85,7 +85,11 @@ def _handle_success(
85
85
return model_class (** decoded_body ) # type: ignore
86
86
87
87
def _exception_for_error (
88
- self , status : int , content_type : Optional [str ], raw_body : str , uri : str
88
+ self ,
89
+ status : int ,
90
+ content_type : Optional [str ],
91
+ raw_body : str ,
92
+ uri : str ,
89
93
) -> Union [
90
94
AuthenticationError ,
91
95
InsufficientFundsError ,
@@ -94,15 +98,18 @@ def _exception_for_error(
94
98
PermissionRequiredError ,
95
99
]:
96
100
"""Returns the exception for the error responses."""
97
-
98
101
if 400 <= status < 500 :
99
102
return self ._exception_for_4xx_status (status , content_type , raw_body , uri )
100
103
if 500 <= status < 600 :
101
104
return self ._exception_for_5xx_status (status , raw_body , uri )
102
105
return self ._exception_for_unexpected_status (status , raw_body , uri )
103
106
104
107
def _exception_for_4xx_status (
105
- self , status : int , content_type : Optional [str ], raw_body : str , uri : str
108
+ self ,
109
+ status : int ,
110
+ content_type : Optional [str ],
111
+ raw_body : str ,
112
+ uri : str ,
106
113
) -> Union [
107
114
AuthenticationError ,
108
115
InsufficientFundsError ,
@@ -113,7 +120,10 @@ def _exception_for_4xx_status(
113
120
"""Returns exception for error responses with 4xx status codes."""
114
121
if not raw_body :
115
122
return HTTPError (
116
- f"Received a { status } error with no body" , status , uri , raw_body
123
+ f"Received a { status } error with no body" ,
124
+ status ,
125
+ uri ,
126
+ raw_body ,
117
127
)
118
128
if content_type is None or content_type .find ("json" ) == - 1 :
119
129
return HTTPError (
@@ -135,7 +145,10 @@ def _exception_for_4xx_status(
135
145
136
146
if "code" in decoded_body and "error" in decoded_body :
137
147
return self ._exception_for_web_service_error (
138
- decoded_body .get ("error" ), decoded_body .get ("code" ), status , uri
148
+ decoded_body .get ("error" ),
149
+ decoded_body .get ("code" ),
150
+ status ,
151
+ uri ,
139
152
)
140
153
return HTTPError (
141
154
"Error response contains JSON but it does not "
@@ -147,7 +160,10 @@ def _exception_for_4xx_status(
147
160
148
161
@staticmethod
149
162
def _exception_for_web_service_error (
150
- message : str , code : str , status : int , uri : str
163
+ message : str ,
164
+ code : str ,
165
+ status : int ,
166
+ uri : str ,
151
167
) -> Union [
152
168
InvalidRequestError ,
153
169
AuthenticationError ,
@@ -354,7 +370,9 @@ async def score(
354
370
)
355
371
356
372
async def report (
357
- self , report : Dict [str , Optional [str ]], validate : bool = True
373
+ self ,
374
+ report : Dict [str , Optional [str ]],
375
+ validate : bool = True ,
358
376
) -> None :
359
377
"""Send a transaction report to the Report Transaction endpoint.
360
378
@@ -403,7 +421,9 @@ async def _response_for(
403
421
return self ._handle_success (raw_body , uri , model_class )
404
422
405
423
async def _do_request (
406
- self , uri : str , data : Dict [str , Any ]
424
+ self ,
425
+ uri : str ,
426
+ data : Dict [str , Any ],
407
427
) -> aiohttp .ClientResponse :
408
428
session = await self ._session ()
409
429
return await session .post (uri , json = data , proxy = self ._proxy )
@@ -651,7 +671,10 @@ def _response_for(
651
671
652
672
def _do_request (self , uri : str , data : Dict [str , Any ]) -> Response :
653
673
return self ._session .post (
654
- uri , json = data , timeout = self ._timeout , proxies = self ._proxies
674
+ uri ,
675
+ json = data ,
676
+ timeout = self ._timeout ,
677
+ proxies = self ._proxies ,
655
678
)
656
679
657
680
def close (self ):
0 commit comments