|
11 | 11 |
|
12 | 12 | """ |
13 | 13 |
|
14 | | -# pylint: disable=too-many-instance-attributes,too-few-public-methods |
| 14 | +# pylint: disable=too-many-instance-attributes,too-few-public-methods,too-many-arguments |
15 | 15 | import ipaddress |
16 | 16 | from abc import ABCMeta |
17 | | -from typing import Any, cast, Dict, Optional, Sequence, Union |
| 17 | +from typing import Dict, List, Optional, Sequence, Union |
18 | 18 |
|
19 | 19 | import geoip2.records |
20 | 20 | from geoip2._internal import Model |
| 21 | +from geoip2.types import IPAddress |
21 | 22 |
|
22 | 23 |
|
23 | 24 | class Country(Model): |
@@ -76,30 +77,44 @@ class Country(Model): |
76 | 77 | traits: geoip2.records.Traits |
77 | 78 |
|
78 | 79 | def __init__( |
79 | | - self, raw_response: Dict[str, Any], locales: Optional[Sequence[str]] = None |
| 80 | + self, |
| 81 | + locales: Optional[Sequence[str]], |
| 82 | + *, |
| 83 | + continent: Optional[Dict] = None, |
| 84 | + country: Optional[Dict] = None, |
| 85 | + ip_address: Optional[IPAddress] = None, |
| 86 | + maxmind: Optional[Dict] = None, |
| 87 | + prefix_len: Optional[int] = None, |
| 88 | + registered_country: Optional[Dict] = None, |
| 89 | + represented_country: Optional[Dict] = None, |
| 90 | + traits: Optional[Dict] = None, |
| 91 | + **_, |
80 | 92 | ) -> None: |
81 | | - if locales is None: |
82 | | - locales = ["en"] |
83 | 93 | self._locales = locales |
84 | | - self.continent = geoip2.records.Continent( |
85 | | - locales, **raw_response.get("continent", {}) |
86 | | - ) |
87 | | - self.country = geoip2.records.Country( |
88 | | - locales, **raw_response.get("country", {}) |
89 | | - ) |
| 94 | + self.continent = geoip2.records.Continent(locales, **(continent or {})) |
| 95 | + self.country = geoip2.records.Country(locales, **(country or {})) |
90 | 96 | self.registered_country = geoip2.records.Country( |
91 | | - locales, **raw_response.get("registered_country", {}) |
| 97 | + locales, **(registered_country or {}) |
92 | 98 | ) |
93 | 99 | self.represented_country = geoip2.records.RepresentedCountry( |
94 | | - locales, **raw_response.get("represented_country", {}) |
| 100 | + locales, **(represented_country or {}) |
95 | 101 | ) |
96 | 102 |
|
97 | | - self.maxmind = geoip2.records.MaxMind(**raw_response.get("maxmind", {})) |
| 103 | + self.maxmind = geoip2.records.MaxMind(**(maxmind or {})) |
| 104 | + |
| 105 | + traits = traits or {} |
| 106 | + if ip_address is not None: |
| 107 | + traits["ip_address"] = ip_address |
| 108 | + if prefix_len is not None: |
| 109 | + traits["prefix_len"] = prefix_len |
98 | 110 |
|
99 | | - self.traits = geoip2.records.Traits(**raw_response.get("traits", {})) |
| 111 | + self.traits = geoip2.records.Traits(**traits) |
100 | 112 |
|
101 | 113 | def __repr__(self) -> str: |
102 | | - return f"{self.__module__}.{self.__class__.__name__}({self.to_dict()}, {self._locales})" |
| 114 | + return ( |
| 115 | + f"{self.__module__}.{self.__class__.__name__}({self._locales}, " |
| 116 | + f"{', '.join(f'{k}={repr(v)}' for k, v in self.to_dict().items())})" |
| 117 | + ) |
103 | 118 |
|
104 | 119 |
|
105 | 120 | class City(Country): |
@@ -179,15 +194,38 @@ class City(Country): |
179 | 194 | subdivisions: geoip2.records.Subdivisions |
180 | 195 |
|
181 | 196 | def __init__( |
182 | | - self, raw_response: Dict[str, Any], locales: Optional[Sequence[str]] = None |
| 197 | + self, |
| 198 | + locales: Optional[Sequence[str]], |
| 199 | + *, |
| 200 | + city: Optional[Dict] = None, |
| 201 | + continent: Optional[Dict] = None, |
| 202 | + country: Optional[Dict] = None, |
| 203 | + location: Optional[Dict] = None, |
| 204 | + ip_address: Optional[IPAddress] = None, |
| 205 | + maxmind: Optional[Dict] = None, |
| 206 | + postal: Optional[Dict] = None, |
| 207 | + prefix_len: Optional[int] = None, |
| 208 | + registered_country: Optional[Dict] = None, |
| 209 | + represented_country: Optional[Dict] = None, |
| 210 | + subdivisions: Optional[List[Dict]] = None, |
| 211 | + traits: Optional[Dict] = None, |
| 212 | + **_, |
183 | 213 | ) -> None: |
184 | | - super().__init__(raw_response, locales) |
185 | | - self.city = geoip2.records.City(locales, **raw_response.get("city", {})) |
186 | | - self.location = geoip2.records.Location(**raw_response.get("location", {})) |
187 | | - self.postal = geoip2.records.Postal(**raw_response.get("postal", {})) |
188 | | - self.subdivisions = geoip2.records.Subdivisions( |
189 | | - locales, *raw_response.get("subdivisions", []) |
| 214 | + super().__init__( |
| 215 | + locales, |
| 216 | + continent=continent, |
| 217 | + country=country, |
| 218 | + ip_address=ip_address, |
| 219 | + maxmind=maxmind, |
| 220 | + prefix_len=prefix_len, |
| 221 | + registered_country=registered_country, |
| 222 | + represented_country=represented_country, |
| 223 | + traits=traits, |
190 | 224 | ) |
| 225 | + self.city = geoip2.records.City(locales, **(city or {})) |
| 226 | + self.location = geoip2.records.Location(**(location or {})) |
| 227 | + self.postal = geoip2.records.Postal(**(postal or {})) |
| 228 | + self.subdivisions = geoip2.records.Subdivisions(locales, *(subdivisions or [])) |
191 | 229 |
|
192 | 230 |
|
193 | 231 | class Insights(City): |
@@ -325,20 +363,28 @@ class SimpleModel(Model, metaclass=ABCMeta): |
325 | 363 | _network: Optional[Union[ipaddress.IPv4Network, ipaddress.IPv6Network]] |
326 | 364 | _prefix_len: int |
327 | 365 |
|
328 | | - def __init__(self, raw: Dict[str, Union[bool, str, int]]) -> None: |
329 | | - if network := raw.get("network"): |
| 366 | + def __init__( |
| 367 | + self, |
| 368 | + ip_address: Optional[str], |
| 369 | + network: Optional[str], |
| 370 | + prefix_len: Optional[int], |
| 371 | + ) -> None: |
| 372 | + if network: |
330 | 373 | self._network = ipaddress.ip_network(network, False) |
331 | 374 | self._prefix_len = self._network.prefixlen |
332 | 375 | else: |
333 | 376 | # This case is for MMDB lookups where performance is paramount. |
334 | 377 | # This is why we don't generate the network unless .network is |
335 | 378 | # used. |
336 | 379 | self._network = None |
337 | | - self._prefix_len = cast(int, raw.get("prefix_len")) |
338 | | - self.ip_address = cast(str, raw.get("ip_address")) |
| 380 | + self._prefix_len = prefix_len |
| 381 | + self.ip_address = ip_address |
339 | 382 |
|
340 | 383 | def __repr__(self) -> str: |
341 | | - return f"{self.__module__}.{self.__class__.__name__}({self.to_dict()})" |
| 384 | + return ( |
| 385 | + f"{self.__module__}.{self.__class__.__name__}" |
| 386 | + f"({', '.join(f'{k}={repr(v)}' for k, v in self.to_dict().items())})" |
| 387 | + ) |
342 | 388 |
|
343 | 389 | @property |
344 | 390 | def network(self) -> Optional[Union[ipaddress.IPv4Network, ipaddress.IPv6Network]]: |
@@ -427,14 +473,27 @@ class AnonymousIP(SimpleModel): |
427 | 473 | is_residential_proxy: bool |
428 | 474 | is_tor_exit_node: bool |
429 | 475 |
|
430 | | - def __init__(self, raw: Dict[str, bool]) -> None: |
431 | | - super().__init__(raw) # type: ignore |
432 | | - self.is_anonymous = raw.get("is_anonymous", False) |
433 | | - self.is_anonymous_vpn = raw.get("is_anonymous_vpn", False) |
434 | | - self.is_hosting_provider = raw.get("is_hosting_provider", False) |
435 | | - self.is_public_proxy = raw.get("is_public_proxy", False) |
436 | | - self.is_residential_proxy = raw.get("is_residential_proxy", False) |
437 | | - self.is_tor_exit_node = raw.get("is_tor_exit_node", False) |
| 476 | + def __init__( |
| 477 | + self, |
| 478 | + *, |
| 479 | + is_anonymous: bool = False, |
| 480 | + is_anonymous_vpn: bool = False, |
| 481 | + is_hosting_provider: bool = False, |
| 482 | + is_public_proxy: bool = False, |
| 483 | + is_residential_proxy: bool = False, |
| 484 | + is_tor_exit_node: bool = False, |
| 485 | + ip_address: Optional[str] = None, |
| 486 | + network: Optional[str] = None, |
| 487 | + prefix_len: Optional[int] = None, |
| 488 | + **_, |
| 489 | + ) -> None: |
| 490 | + super().__init__(ip_address, network, prefix_len) |
| 491 | + self.is_anonymous = is_anonymous |
| 492 | + self.is_anonymous_vpn = is_anonymous_vpn |
| 493 | + self.is_hosting_provider = is_hosting_provider |
| 494 | + self.is_public_proxy = is_public_proxy |
| 495 | + self.is_residential_proxy = is_residential_proxy |
| 496 | + self.is_tor_exit_node = is_tor_exit_node |
438 | 497 |
|
439 | 498 |
|
440 | 499 | class ASN(SimpleModel): |
@@ -474,14 +533,19 @@ class ASN(SimpleModel): |
474 | 533 | autonomous_system_organization: Optional[str] |
475 | 534 |
|
476 | 535 | # pylint:disable=too-many-arguments,too-many-positional-arguments |
477 | | - def __init__(self, raw: Dict[str, Union[str, int]]) -> None: |
478 | | - super().__init__(raw) |
479 | | - self.autonomous_system_number = cast( |
480 | | - Optional[int], raw.get("autonomous_system_number") |
481 | | - ) |
482 | | - self.autonomous_system_organization = cast( |
483 | | - Optional[str], raw.get("autonomous_system_organization") |
484 | | - ) |
| 536 | + def __init__( |
| 537 | + self, |
| 538 | + *, |
| 539 | + autonomous_system_number: Optional[int] = None, |
| 540 | + autonomous_system_organization: Optional[str] = None, |
| 541 | + ip_address: Optional[str] = None, |
| 542 | + network: Optional[str] = None, |
| 543 | + prefix_len: Optional[int] = None, |
| 544 | + **_, |
| 545 | + ) -> None: |
| 546 | + super().__init__(ip_address, network, prefix_len) |
| 547 | + self.autonomous_system_number = autonomous_system_number |
| 548 | + self.autonomous_system_organization = autonomous_system_organization |
485 | 549 |
|
486 | 550 |
|
487 | 551 | class ConnectionType(SimpleModel): |
@@ -520,9 +584,17 @@ class ConnectionType(SimpleModel): |
520 | 584 |
|
521 | 585 | connection_type: Optional[str] |
522 | 586 |
|
523 | | - def __init__(self, raw: Dict[str, Union[str, int]]) -> None: |
524 | | - super().__init__(raw) |
525 | | - self.connection_type = cast(Optional[str], raw.get("connection_type")) |
| 587 | + def __init__( |
| 588 | + self, |
| 589 | + *, |
| 590 | + connection_type: Optional[str] = None, |
| 591 | + ip_address: Optional[str] = None, |
| 592 | + network: Optional[str] = None, |
| 593 | + prefix_len: Optional[int] = None, |
| 594 | + **_, |
| 595 | + ) -> None: |
| 596 | + super().__init__(ip_address, network, prefix_len) |
| 597 | + self.connection_type = connection_type |
526 | 598 |
|
527 | 599 |
|
528 | 600 | class Domain(SimpleModel): |
@@ -554,9 +626,17 @@ class Domain(SimpleModel): |
554 | 626 |
|
555 | 627 | domain: Optional[str] |
556 | 628 |
|
557 | | - def __init__(self, raw: Dict[str, Union[str, int]]) -> None: |
558 | | - super().__init__(raw) |
559 | | - self.domain = cast(Optional[str], raw.get("domain")) |
| 629 | + def __init__( |
| 630 | + self, |
| 631 | + *, |
| 632 | + domain: Optional[str] = None, |
| 633 | + ip_address: Optional[str] = None, |
| 634 | + network: Optional[str] = None, |
| 635 | + prefix_len: Optional[int] = None, |
| 636 | + **_, |
| 637 | + ) -> None: |
| 638 | + super().__init__(ip_address, network, prefix_len) |
| 639 | + self.domain = domain |
560 | 640 |
|
561 | 641 |
|
562 | 642 | class ISP(ASN): |
@@ -626,9 +706,28 @@ class ISP(ASN): |
626 | 706 | organization: Optional[str] |
627 | 707 |
|
628 | 708 | # pylint:disable=too-many-arguments,too-many-positional-arguments |
629 | | - def __init__(self, raw: Dict[str, Union[str, int]]) -> None: |
630 | | - super().__init__(raw) |
631 | | - self.isp = cast(Optional[str], raw.get("isp")) |
632 | | - self.mobile_country_code = cast(Optional[str], raw.get("mobile_country_code")) |
633 | | - self.mobile_network_code = cast(Optional[str], raw.get("mobile_network_code")) |
634 | | - self.organization = cast(Optional[str], raw.get("organization")) |
| 709 | + def __init__( |
| 710 | + self, |
| 711 | + *, |
| 712 | + autonomous_system_number: Optional[int] = None, |
| 713 | + autonomous_system_organization: Optional[str] = None, |
| 714 | + isp: Optional[str] = None, |
| 715 | + mobile_country_code: Optional[str] = None, |
| 716 | + mobile_network_code: Optional[str] = None, |
| 717 | + organization: Optional[str] = None, |
| 718 | + ip_address: Optional[str] = None, |
| 719 | + network: Optional[str] = None, |
| 720 | + prefix_len: Optional[int] = None, |
| 721 | + **_, |
| 722 | + ) -> None: |
| 723 | + super().__init__( |
| 724 | + autonomous_system_number=autonomous_system_number, |
| 725 | + autonomous_system_organization=autonomous_system_organization, |
| 726 | + ip_address=ip_address, |
| 727 | + network=network, |
| 728 | + prefix_len=prefix_len, |
| 729 | + ) |
| 730 | + self.isp = isp |
| 731 | + self.mobile_country_code = mobile_country_code |
| 732 | + self.mobile_network_code = mobile_network_code |
| 733 | + self.organization = organization |
0 commit comments