Skip to content

Commit 165d40a

Browse files
committed
Pass in locales as a normal parameter
1 parent 00482e0 commit 165d40a

File tree

4 files changed

+32
-27
lines changed

4 files changed

+32
-27
lines changed

minfraud/models.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"""
88

99
# pylint:disable=too-many-lines,too-many-instance-attributes,too-many-locals
10-
from typing import Dict, List, Optional
10+
from typing import Dict, List, Optional, Sequence
1111

1212
from geoip2.mixins import SimpleEquality
1313
import geoip2.models
@@ -202,16 +202,16 @@ class IPAddress(geoip2.models.Insights):
202202

203203
def __init__(
204204
self,
205+
locales: Sequence[str],
205206
*,
206-
locales: Optional[List[str]] = None,
207207
country: Optional[Dict] = None,
208208
location: Optional[Dict] = None,
209209
risk: Optional[float] = None,
210210
risk_reasons: Optional[List[Dict]] = None,
211211
**kwargs,
212212
) -> None:
213213

214-
super().__init__(kwargs, locales=locales)
214+
super().__init__(kwargs, locales=list(locales))
215215
self.country = GeoIP2Country(locales, **(country or {}))
216216
self.location = GeoIP2Location(**(location or {}))
217217
self.risk = risk
@@ -1356,6 +1356,7 @@ class Factors(SimpleEquality):
13561356

13571357
def __init__(
13581358
self,
1359+
locales: Sequence[str],
13591360
*,
13601361
billing_address: Optional[Dict] = None,
13611362
billing_phone: Optional[Dict] = None,
@@ -1384,7 +1385,7 @@ def __init__(
13841385
self.device = Device(**(device or {}))
13851386
self.email = Email(**(email or {}))
13861387
self.id = id
1387-
self.ip_address = IPAddress(**(ip_address or {}))
1388+
self.ip_address = IPAddress(locales, **(ip_address or {}))
13881389
self.queries_remaining = queries_remaining
13891390
self.risk_score = risk_score
13901391
self.shipping_address = ShippingAddress(**(shipping_address or {}))
@@ -1521,6 +1522,7 @@ class Insights(SimpleEquality):
15211522

15221523
def __init__(
15231524
self,
1525+
locales: Sequence[str],
15241526
*,
15251527
billing_address: Optional[Dict] = None,
15261528
billing_phone: Optional[Dict] = None,
@@ -1547,7 +1549,7 @@ def __init__(
15471549
self.email = Email(**(email or {}))
15481550
self.funds_remaining = funds_remaining
15491551
self.id = id
1550-
self.ip_address = IPAddress(**(ip_address or {}))
1552+
self.ip_address = IPAddress(locales, **(ip_address or {}))
15511553
self.queries_remaining = queries_remaining
15521554
self.risk_score = risk_score
15531555
self.shipping_address = ShippingAddress(**(shipping_address or {}))

minfraud/webservice.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"""
88

99
import json
10-
from typing import Any, cast, Dict, Optional, Tuple, Type, Union
10+
from functools import partial
11+
from typing import Any, Callable, cast, Dict, Optional, Sequence, Union
1112

1213
import aiohttp
1314
import aiohttp.http
@@ -39,7 +40,7 @@
3940
class BaseClient:
4041
_account_id: str
4142
_license_key: str
42-
_locales: Tuple[str, ...]
43+
_locales: Sequence[str]
4344
_timeout: float
4445

4546
_score_uri: str
@@ -52,7 +53,7 @@ def __init__(
5253
account_id: int,
5354
license_key: str,
5455
host: str = "minfraud.maxmind.com",
55-
locales: Tuple[str, ...] = ("en",),
56+
locales: Sequence[str] = ("en",),
5657
timeout: float = 60,
5758
) -> None:
5859
self._locales = locales
@@ -69,7 +70,7 @@ def _handle_success(
6970
self,
7071
raw_body: str,
7172
uri: str,
72-
model_class: Union[Type[Factors], Type[Score], Type[Insights]],
73+
model_class: Callable,
7374
) -> Union[Score, Factors, Insights]:
7475
"""Handle successful response."""
7576
try:
@@ -81,8 +82,6 @@ def _handle_success(
8182
200,
8283
uri,
8384
) from ex
84-
if "ip_address" in decoded_body:
85-
decoded_body["ip_address"]["locales"] = self._locales
8685
return model_class(**decoded_body) # type: ignore
8786

8887
def _exception_for_error(
@@ -210,7 +209,7 @@ def __init__(
210209
account_id: int,
211210
license_key: str,
212211
host: str = "minfraud.maxmind.com",
213-
locales: Tuple[str, ...] = ("en",),
212+
locales: Sequence[str] = ("en",),
214213
timeout: float = 60,
215214
proxy: Optional[str] = None,
216215
) -> None:
@@ -269,7 +268,7 @@ async def factors(
269268
Factors,
270269
await self._response_for(
271270
self._factors_uri,
272-
Factors,
271+
partial(Factors, self._locales),
273272
transaction,
274273
validate,
275274
hash_email,
@@ -308,7 +307,7 @@ async def insights(
308307
Insights,
309308
await self._response_for(
310309
self._insights_uri,
311-
Insights,
310+
partial(Insights, self._locales),
312311
transaction,
313312
validate,
314313
hash_email,
@@ -387,7 +386,7 @@ async def report(
387386
async def _response_for(
388387
self,
389388
uri: str,
390-
model_class: Union[Type[Factors], Type[Score], Type[Insights]],
389+
model_class: Callable,
391390
request: Dict[str, Any],
392391
validate: bool,
393392
hash_email: bool,
@@ -445,7 +444,7 @@ def __init__(
445444
account_id: int,
446445
license_key: str,
447446
host: str = "minfraud.maxmind.com",
448-
locales: Tuple[str, ...] = ("en",),
447+
locales: Sequence[str] = ("en",),
449448
timeout: float = 60,
450449
proxy: Optional[str] = None,
451450
) -> None:
@@ -518,7 +517,7 @@ def factors(
518517
Factors,
519518
self._response_for(
520519
self._factors_uri,
521-
Factors,
520+
partial(Factors, self._locales),
522521
transaction,
523522
validate,
524523
hash_email,
@@ -557,7 +556,7 @@ def insights(
557556
Insights,
558557
self._response_for(
559558
self._insights_uri,
560-
Insights,
559+
partial(Insights, self._locales),
561560
transaction,
562561
validate,
563562
hash_email,
@@ -634,7 +633,7 @@ def report(self, report: Dict[str, Optional[str]], validate: bool = True) -> Non
634633
def _response_for(
635634
self,
636635
uri: str,
637-
model_class: Union[Type[Factors], Type[Score], Type[Insights]],
636+
model_class: Callable,
638637
request: Dict[str, Any],
639638
validate: bool,
640639
hash_email: bool,

tests/test_models.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ def test_geoip2_location(self):
119119
def test_ip_address(self):
120120
time = "2015-04-19T12:59:23-01:00"
121121
address = IPAddress(
122+
["en"],
122123
country={
123124
"is_high_risk": True,
124125
"is_in_european_union": True,
@@ -180,7 +181,7 @@ def test_ip_address(self):
180181
)
181182

182183
def test_empty_address(self):
183-
address = IPAddress()
184+
address = IPAddress([])
184185
self.assertEqual([], address.risk_reasons)
185186

186187
def test_score_ip_address(self):
@@ -189,7 +190,7 @@ def test_score_ip_address(self):
189190

190191
def test_ip_address_locales(self):
191192
loc = IPAddress(
192-
locales=["fr"],
193+
["fr"],
193194
country={"names": {"fr": "Country"}},
194195
city={"names": {"fr": "City"}},
195196
)
@@ -279,12 +280,12 @@ def test_score(self):
279280
def test_insights(self):
280281
response = self.factors_response()
281282
del response["subscores"]
282-
insights = Insights(**response)
283+
insights = Insights(None, **response)
283284
self.check_insights_data(insights, response["id"])
284285

285286
def test_factors(self):
286287
response = self.factors_response()
287-
factors = Factors(**response)
288+
factors = Factors(None, **response)
288289
self.check_insights_data(factors, response["id"])
289290
self.check_risk_score_reasons_data(factors.risk_score_reasons)
290291
self.assertEqual(0.01, factors.subscores.avs_result)

tests/test_webservice.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import asyncio
22
import json
33
import os
4+
from functools import partial
45
from io import open
56
from typing import Type, Union
67
from pytest_httpserver import HTTPServer
@@ -181,9 +182,10 @@ def has_ip_location(self):
181182
def test_200(self):
182183
model = self.create_success()
183184
response = json.loads(self.response)
185+
cls = self.cls
184186
if self.has_ip_location():
185-
response["ip_address"]["locales"] = ("en",)
186-
self.assertEqual(self.cls(**response), model)
187+
cls = partial(cls, ("en",))
188+
self.assertEqual(cls(**response), model)
187189
if self.has_ip_location():
188190
self.assertEqual("United Kingdom", model.ip_address.country.name)
189191
self.assertEqual(True, model.ip_address.traits.is_residential_proxy)
@@ -240,9 +242,10 @@ def test_200_with_locales(self):
240242
)
241243
model = self.create_success(client=client)
242244
response = json.loads(self.response)
245+
cls = self.cls
243246
if self.has_ip_location():
244-
response["ip_address"]["locales"] = locales
245-
self.assertEqual(self.cls(**response), model)
247+
cls = partial(cls, locales)
248+
self.assertEqual(cls(**response), model)
246249
if self.has_ip_location():
247250
self.assertEqual("Royaume-Uni", model.ip_address.country.name)
248251
self.assertEqual("Londres", model.ip_address.city.name)

0 commit comments

Comments
 (0)