1- from typing import List
1+ from typing import List , Dict , Any
22
33from ravendb .documents .operations .connection_strings import ConnectionString
44import ravendb .serverwide .server_operation_executor
@@ -10,27 +10,88 @@ def __init__(self, api_key_id: str = None, api_key: str = None, encoded_api_key:
1010 self .api_key = api_key
1111 self .encoded_api_key = encoded_api_key
1212
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+
1328
1429class BasicAuthentication :
1530 def __init__ (self , username : str = None , password : str = None ):
1631 self .username = username
1732 self .password = password
1833
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+
1948
2049class CertificateAuthentication :
2150 def __init__ (self , certificates_base64 : List [str ] = None ):
2251 self .certificates_base64 = certificates_base64
2352
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+
2464
2565class Authentication :
2666 def __init__ (self , api_key : ApiKeyAuthentication = None , basic : BasicAuthentication = None , certificate : CertificateAuthentication = None ):
2767 self .api_key = api_key
2868 self .basic = basic
2969 self .certificate = certificate
3070
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+
3191
3292class ElasticSearchConnectionString (ConnectionString ):
3393 def __init__ (self , name : str , nodes : List [str ] = None , authentication : Authentication = None ):
94+ super ().__init__ ()
3495 self .name = name
3596 self .nodes = nodes
3697 self .authentication = authentication
@@ -44,4 +105,12 @@ def to_json(self):
44105 "Nodes" : self .nodes ,
45106 "Authentication" : self .authentication ,
46107 "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