13
13
pre_load ,
14
14
validate ,
15
15
)
16
+ from typing_extensions import Self
16
17
17
18
from .config import DOCUMENT_SIZE_THRESHOLD_BYTES , MULTI_DOCUMENT_LIMIT
18
19
19
20
21
+ class ToDictMixin :
22
+ """
23
+ Provides a type-safe `to_dict()` method for classes using Marshmallow
24
+ """
25
+
26
+ SCHEMA : ClassVar [Schema ]
27
+
28
+ def to_dict (self ) -> Dict [str , Any ]:
29
+ return cast (Dict [str , Any ], self .SCHEMA .dump (self ))
30
+
31
+
32
+ class FromDictMixin :
33
+ """This class must be used as an additional base class for all classes whose schema
34
+ implements a `post_load` function turning the received dict into a class instance.
35
+
36
+ It makes it possible to deserialize an object using `MyClass.from_dict(dct)` instead
37
+ of `MyClass.SCHEMA.load(dct)`. The `from_dict()` method is shorter, but more
38
+ importantly, type-safe: its return type is an instance of `MyClass`, not
39
+ `list[Any] | Any`.
40
+
41
+ Reference: https://marshmallow.readthedocs.io/en/stable/quickstart.html#deserializing-to-objects E501
42
+ """
43
+
44
+ SCHEMA : ClassVar [Schema ]
45
+
46
+ @classmethod
47
+ def from_dict (cls , dct : Dict [str , Any ]) -> Self :
48
+ return cast (Self , cls .SCHEMA .load (dct ))
49
+
50
+
20
51
class BaseSchema (Schema ):
21
52
class Meta :
22
53
ordered = True
23
54
unknown = EXCLUDE
24
55
25
56
26
- class Base :
27
- SCHEMA : ClassVar [BaseSchema ]
28
-
57
+ class Base (ToDictMixin ):
29
58
def __init__ (self , status_code : Optional [int ] = None ) -> None :
30
59
self .status_code = status_code
31
60
@@ -35,12 +64,6 @@ def to_json(self) -> str:
35
64
"""
36
65
return cast (str , self .SCHEMA .dumps (self ))
37
66
38
- def to_dict (self ) -> Dict :
39
- """
40
- to_dict converts model to a dictionary representation.
41
- """
42
- return cast (Dict , self .SCHEMA .dump (self ))
43
-
44
67
@property
45
68
def success (self ) -> bool :
46
69
return self .__bool__ ()
@@ -122,7 +145,7 @@ def make_detail_response(self, data: Dict[str, Any], **kwargs: Any) -> "Detail":
122
145
return Detail (** data )
123
146
124
147
125
- class Detail (Base ):
148
+ class Detail (Base , FromDictMixin ):
126
149
"""Detail is a response object mostly returned on error or when the
127
150
api output is a simple string.
128
151
@@ -155,7 +178,7 @@ def make_match(self, data: Dict[str, Any], **kwargs: Any) -> "Match":
155
178
return Match (** data )
156
179
157
180
158
- class Match (Base ):
181
+ class Match (Base , FromDictMixin ):
159
182
"""
160
183
Match describes a found issue by GitGuardian.
161
184
With info such as match location and type.
@@ -219,7 +242,7 @@ def make_policy_break(self, data: Dict[str, Any], **kwargs: Any) -> "PolicyBreak
219
242
return PolicyBreak (** data )
220
243
221
244
222
- class PolicyBreak (Base ):
245
+ class PolicyBreak (Base , FromDictMixin ):
223
246
"""
224
247
PolicyBreak describes a GitGuardian policy break found
225
248
in a scan.
@@ -269,7 +292,7 @@ def make_scan_result(self, data: Dict[str, Any], **kwargs: Any) -> "ScanResult":
269
292
return ScanResult (** data )
270
293
271
294
272
- class ScanResult (Base ):
295
+ class ScanResult (Base , FromDictMixin ):
273
296
"""ScanResult is a response object returned on a Content Scan
274
297
275
298
Attributes:
@@ -355,7 +378,7 @@ def make_scan_result(
355
378
return MultiScanResult (** data )
356
379
357
380
358
- class MultiScanResult (Base ):
381
+ class MultiScanResult (Base , FromDictMixin ):
359
382
"""ScanResult is a response object returned on a Content Scan
360
383
361
384
Attributes:
@@ -425,7 +448,7 @@ def make_quota(self, data: Dict[str, Any], **kwargs: Any) -> "Quota":
425
448
return Quota (** data )
426
449
427
450
428
- class Quota (Base ):
451
+ class Quota (Base , FromDictMixin ):
429
452
"""
430
453
Quota describes a quota category in the GitGuardian API.
431
454
Allows you to check your current available quota.
@@ -468,7 +491,7 @@ def make_quota_response(
468
491
return QuotaResponse (** data )
469
492
470
493
471
- class QuotaResponse (Base ):
494
+ class QuotaResponse (Base , FromDictMixin ):
472
495
"""
473
496
Quota describes a quota category in the GitGuardian API.
474
497
Allows you to check your current available quota.
@@ -515,7 +538,7 @@ def make_honeytoken_response(
515
538
return HoneytokenResponse (** data )
516
539
517
540
518
- class HoneytokenResponse (Base ):
541
+ class HoneytokenResponse (Base , FromDictMixin ):
519
542
"""
520
543
honeytoken creation in the GitGuardian API.
521
544
Allows users to create and get a honeytoken.
@@ -633,14 +656,15 @@ class SecretScanPreferences:
633
656
634
657
635
658
@dataclass
636
- class ServerMetadata (Base ):
659
+ class ServerMetadata (Base , FromDictMixin ):
637
660
version : str
638
661
preferences : Dict [str , Any ]
639
662
secret_scan_preferences : SecretScanPreferences = field (
640
663
default_factory = SecretScanPreferences
641
664
)
642
665
643
666
644
- ServerMetadata .SCHEMA = marshmallow_dataclass .class_schema (
645
- ServerMetadata , base_schema = BaseSchema
646
- )()
667
+ ServerMetadata .SCHEMA = cast (
668
+ BaseSchema ,
669
+ marshmallow_dataclass .class_schema (ServerMetadata , base_schema = BaseSchema )(),
670
+ )
0 commit comments