14
14
import geoip2 .records
15
15
16
16
17
- class IPRiskReason (SimpleEquality ):
17
+ class _Serializable (SimpleEquality ):
18
+ def to_dict (self ):
19
+ """Returns a dict of the object suitable for serialization"""
20
+ result = {}
21
+ for key , value in self .__dict__ .items ():
22
+ if hasattr (value , "to_dict" ) and callable (value .to_dict ):
23
+ result [key ] = value .to_dict ()
24
+ elif hasattr (value , "raw" ):
25
+ # geoip2 uses "raw" for historical reasons
26
+ result [key ] = value .raw
27
+ elif isinstance (value , list ):
28
+ result [key ] = [
29
+ (
30
+ item .to_dict ()
31
+ if hasattr (item , "to_dict" ) and callable (item .to_dict )
32
+ else item
33
+ )
34
+ for item in value
35
+ ]
36
+ else :
37
+ result [key ] = value
38
+ return result
39
+
40
+
41
+ class IPRiskReason (_Serializable ):
18
42
"""Reason for the IP risk.
19
43
20
44
This class provides both a machine-readable code and a human-readable
@@ -202,23 +226,32 @@ class IPAddress(geoip2.models.Insights):
202
226
203
227
def __init__ (
204
228
self ,
205
- locales : Sequence [str ],
229
+ locales : Optional [ Sequence [str ] ],
206
230
* ,
207
231
country : Optional [Dict ] = None ,
208
232
location : Optional [Dict ] = None ,
209
233
risk : Optional [float ] = None ,
210
234
risk_reasons : Optional [List [Dict ]] = None ,
211
235
** kwargs ,
212
236
) -> None :
213
-
214
- super ().__init__ (kwargs , locales = list (locales ))
237
+ # For raw attribute
238
+ if country is not None :
239
+ kwargs ["country" ] = country
240
+ if location is not None :
241
+ kwargs ["location" ] = location
242
+ if risk is not None :
243
+ kwargs ["risk" ] = risk
244
+ if risk_reasons is not None :
245
+ kwargs ["risk_reasons" ] = risk_reasons
246
+
247
+ super ().__init__ (kwargs , locales = list (locales or []))
215
248
self .country = GeoIP2Country (locales , ** (country or {}))
216
249
self .location = GeoIP2Location (** (location or {}))
217
250
self .risk = risk
218
251
self .risk_reasons = [IPRiskReason (** x ) for x in risk_reasons or []]
219
252
220
253
221
- class ScoreIPAddress (SimpleEquality ):
254
+ class ScoreIPAddress (_Serializable ):
222
255
"""Information about the IP address for minFraud Score.
223
256
224
257
.. attribute:: risk
@@ -235,7 +268,7 @@ def __init__(self, *, risk: Optional[float] = None, **_):
235
268
self .risk = risk
236
269
237
270
238
- class Issuer (SimpleEquality ):
271
+ class Issuer (_Serializable ):
239
272
"""Information about the credit card issuer.
240
273
241
274
.. attribute:: name
@@ -293,7 +326,7 @@ def __init__(
293
326
self .matches_provided_phone_number = matches_provided_phone_number
294
327
295
328
296
- class Device (SimpleEquality ):
329
+ class Device (_Serializable ):
297
330
"""Information about the device associated with the IP address.
298
331
299
332
In order to receive device output from minFraud Insights or minFraud
@@ -353,7 +386,7 @@ def __init__(
353
386
self .local_time = local_time
354
387
355
388
356
- class Disposition (SimpleEquality ):
389
+ class Disposition (_Serializable ):
357
390
"""Information about disposition for the request as set by custom rules.
358
391
359
392
In order to receive a disposition, you must be use the minFraud custom
@@ -402,7 +435,7 @@ def __init__(
402
435
self .rule_label = rule_label
403
436
404
437
405
- class EmailDomain (SimpleEquality ):
438
+ class EmailDomain (_Serializable ):
406
439
"""Information about the email domain passed in the request.
407
440
408
441
.. attribute:: first_seen
@@ -421,7 +454,7 @@ def __init__(self, *, first_seen: Optional[str] = None, **_):
421
454
self .first_seen = first_seen
422
455
423
456
424
- class Email (SimpleEquality ):
457
+ class Email (_Serializable ):
425
458
"""Information about the email address passed in the request.
426
459
427
460
.. attribute:: domain
@@ -484,7 +517,7 @@ def __init__(
484
517
self .is_high_risk = is_high_risk
485
518
486
519
487
- class CreditCard (SimpleEquality ):
520
+ class CreditCard (_Serializable ):
488
521
"""Information about the credit card based on the issuer ID number.
489
522
490
523
.. attribute:: country
@@ -578,7 +611,7 @@ def __init__(
578
611
self .type = type
579
612
580
613
581
- class BillingAddress (SimpleEquality ):
614
+ class BillingAddress (_Serializable ):
582
615
"""Information about the billing address.
583
616
584
617
.. attribute:: distance_to_ip_location
@@ -644,7 +677,7 @@ def __init__(
644
677
self .is_in_ip_country = is_in_ip_country
645
678
646
679
647
- class ShippingAddress (SimpleEquality ):
680
+ class ShippingAddress (_Serializable ):
648
681
"""Information about the shipping address.
649
682
650
683
.. attribute:: distance_to_ip_location
@@ -733,7 +766,7 @@ def __init__(
733
766
self .distance_to_billing_address = distance_to_billing_address
734
767
735
768
736
- class Phone (SimpleEquality ):
769
+ class Phone (_Serializable ):
737
770
"""Information about the billing or shipping phone number.
738
771
739
772
.. attribute:: country
@@ -790,7 +823,7 @@ def __init__(
790
823
self .number_type = number_type
791
824
792
825
793
- class ServiceWarning (SimpleEquality ):
826
+ class ServiceWarning (_Serializable ):
794
827
"""Warning from the web service.
795
828
796
829
.. attribute:: code
@@ -837,7 +870,7 @@ def __init__(
837
870
self .input_pointer = input_pointer
838
871
839
872
840
- class Subscores (SimpleEquality ):
873
+ class Subscores (_Serializable ):
841
874
"""Risk factor scores used in calculating the overall risk score.
842
875
843
876
.. deprecated:: 2.12.0
@@ -1081,7 +1114,7 @@ def __init__(
1081
1114
self .time_of_day = time_of_day
1082
1115
1083
1116
1084
- class Reason (SimpleEquality ):
1117
+ class Reason (_Serializable ):
1085
1118
"""The risk score reason for the multiplier.
1086
1119
1087
1120
This class provides both a machine-readable code and a human-readable
@@ -1174,7 +1207,7 @@ def __init__(
1174
1207
self .reason = reason
1175
1208
1176
1209
1177
- class RiskScoreReason (SimpleEquality ):
1210
+ class RiskScoreReason (_Serializable ):
1178
1211
"""The risk score multiplier and the reasons for that multiplier.
1179
1212
1180
1213
.. attribute:: multiplier
@@ -1209,7 +1242,7 @@ def __init__(
1209
1242
self .reasons = [Reason (** x ) for x in reasons or []]
1210
1243
1211
1244
1212
- class Factors (SimpleEquality ):
1245
+ class Factors (_Serializable ):
1213
1246
"""Model for Factors response.
1214
1247
1215
1248
.. attribute:: id
@@ -1397,7 +1430,7 @@ def __init__(
1397
1430
]
1398
1431
1399
1432
1400
- class Insights (SimpleEquality ):
1433
+ class Insights (_Serializable ):
1401
1434
"""Model for Insights response.
1402
1435
1403
1436
.. attribute:: id
@@ -1557,7 +1590,7 @@ def __init__(
1557
1590
self .warnings = [ServiceWarning (** x ) for x in warnings or []]
1558
1591
1559
1592
1560
- class Score (SimpleEquality ):
1593
+ class Score (_Serializable ):
1561
1594
"""Model for Score response.
1562
1595
1563
1596
.. attribute:: id
0 commit comments