1
- from typing import List
1
+ from typing import List , Dict , Any
2
2
3
3
from ravendb .documents .operations .connection_strings import ConnectionString
4
4
import ravendb .serverwide .server_operation_executor
@@ -10,27 +10,88 @@ def __init__(self, api_key_id: str = None, api_key: str = None, encoded_api_key:
10
10
self .api_key = api_key
11
11
self .encoded_api_key = encoded_api_key
12
12
13
+ def to_json (self ) -> Dict [str , Any ]:
14
+ return {
15
+ "ApiKeyId" : self .api_key_id ,
16
+ "ApiKey" : self .api_key ,
17
+ "EncodedApiKey" : self .encoded_api_key ,
18
+ }
19
+
20
+ @classmethod
21
+ def from_json (cls , data : Dict [str , Any ]) -> "ApiKeyAuthentication" :
22
+ return cls (
23
+ api_key_id = data .get ("ApiKeyId" ) or data .get ("api_key_id" ),
24
+ api_key = data .get ("ApiKey" ) or data .get ("api_key" ),
25
+ encoded_api_key = data .get ("EncodedApiKey" ) or data .get ("encoded_api_key" ),
26
+ )
27
+
13
28
14
29
class BasicAuthentication :
15
30
def __init__ (self , username : str = None , password : str = None ):
16
31
self .username = username
17
32
self .password = password
18
33
34
+ def to_json (self ) -> Dict [str , Any ]:
35
+ return {
36
+ "Username" : self .username ,
37
+ "Password" : self .password ,
38
+ }
39
+
40
+ @classmethod
41
+ def from_json (cls , data : Dict [str , Any ]) -> "BasicAuthentication" :
42
+ return cls (
43
+ username = data .get ("Username" ) or data .get ("username" ),
44
+ password = data .get ("Password" ) or data .get ("password" ),
45
+ )
46
+
47
+
19
48
20
49
class CertificateAuthentication :
21
50
def __init__ (self , certificates_base64 : List [str ] = None ):
22
51
self .certificates_base64 = certificates_base64
23
52
53
+ def to_json (self ) -> Dict [str , Any ]:
54
+ return {
55
+ "CertificatesBase64" : list (self .certificates_base64 ) if self .certificates_base64 is not None else None ,
56
+ }
57
+
58
+ @classmethod
59
+ def from_json (cls , data : Dict [str , Any ]) -> "CertificateAuthentication" :
60
+ certs = data .get ("CertificatesBase64" ) or data .get ("certificates_base64" )
61
+ return cls (certificates_base64 = list (certs ) if certs is not None else None )
62
+
63
+
24
64
25
65
class Authentication :
26
66
def __init__ (self , api_key : ApiKeyAuthentication = None , basic : BasicAuthentication = None , certificate : CertificateAuthentication = None ):
27
67
self .api_key = api_key
28
68
self .basic = basic
29
69
self .certificate = certificate
30
70
71
+ def to_json (self ) -> Dict [str , Any ]:
72
+ return {
73
+ "ApiKey" : self .api_key .to_json () if self .api_key is not None else None ,
74
+ "Basic" : self .basic .to_json () if self .basic is not None else None ,
75
+ "Certificate" : self .certificate .to_json () if self .certificate is not None else None ,
76
+ }
77
+
78
+ @classmethod
79
+ def from_json (cls , data : Dict [str , Any ]) -> "Authentication" :
80
+ api_key_data = data .get ("ApiKey" )
81
+ basic_data = data .get ("Basic" )
82
+ certificate_data = data .get ("Certificate" )
83
+
84
+ return cls (
85
+ api_key = ApiKeyAuthentication .from_json (api_key_data ) if api_key_data else None ,
86
+ basic = BasicAuthentication .from_json (basic_data ) if basic_data else None ,
87
+ certificate = CertificateAuthentication .from_json (certificate_data ) if certificate_data else None ,
88
+ )
89
+
90
+
31
91
32
92
class ElasticSearchConnectionString (ConnectionString ):
33
93
def __init__ (self , name : str , nodes : List [str ] = None , authentication : Authentication = None ):
94
+ super ().__init__ ()
34
95
self .name = name
35
96
self .nodes = nodes
36
97
self .authentication = authentication
@@ -44,4 +105,12 @@ def to_json(self):
44
105
"Nodes" : self .nodes ,
45
106
"Authentication" : self .authentication ,
46
107
"Type" : ravendb .serverwide .server_operation_executor .ConnectionStringType .ELASTIC_SEARCH ,
47
- }
108
+ }
109
+
110
+ @classmethod
111
+ def from_json (cls , json_dict : Dict [str , Any ]) -> Any :
112
+ return cls (
113
+ name = json_dict ["Name" ],
114
+ nodes = json_dict ["Nodes" ],
115
+ authentication = Authentication .from_json (json_dict ["Authentication" ]),
116
+ )
0 commit comments