2
2
"""PyMiloClient for RESTFull Protocol."""
3
3
from enum import Enum
4
4
from .encryptor import DummyEncryptor
5
- from .compressor import DummyCompressor
5
+ from .compressor import Compression
6
6
from ..pymilo_obj import Export , Import
7
7
from .param import PYMILO_CLIENT_INVALID_MODE , PYMILO_CLIENT_MODEL_SYNCHED , \
8
8
PYMILO_CLIENT_LOCAL_MODEL_UPLOADED , PYMILO_CLIENT_LOCAL_MODEL_UPLOAD_FAILED , \
@@ -25,6 +25,7 @@ def __init__(
25
25
self ,
26
26
model = None ,
27
27
mode = Mode .LOCAL ,
28
+ compressor = Compression .NULL ,
28
29
server_url = "http://127.0.0.1:8000" ,
29
30
):
30
31
"""
@@ -34,6 +35,8 @@ def __init__(
34
35
:type model: Any
35
36
:param mode: the mode in which PymiloClient should work, either LOCAL mode or DELEGATE
36
37
:type mode: str (LOCAL|DELEGATE)
38
+ :param compressor: the compression method to be used in client-server communications
39
+ :type compressor: pymilo.streaming.compressor.Compression
37
40
:param server_url: the url to which PyMilo Server listens
38
41
:type server_url: str
39
42
:return: an instance of the Pymilo PymiloClient class
@@ -42,10 +45,24 @@ def __init__(
42
45
self ._model_id = "0x_model_id"
43
46
self ._model = model
44
47
self ._mode = mode
45
- self ._compressor = DummyCompressor ()
48
+ self ._compressor = compressor . value
46
49
self ._encryptor = DummyEncryptor ()
47
50
self ._communicator = RESTClientCommunicator (server_url )
48
51
52
+ def encrypt_compress (self , body ):
53
+ """
54
+ Compress and Encrypt body payload.
55
+
56
+ :param body: body payload of the request
57
+ :type body: dict
58
+ :return: the compressed and encrypted version of the body payload
59
+ """
60
+ return self ._encryptor .encrypt (
61
+ self ._compressor .compress (
62
+ body
63
+ )
64
+ )
65
+
49
66
def toggle_mode (self , mode = Mode .LOCAL ):
50
67
"""
51
68
Toggle the PyMiloClient mode, either from LOCAL to DELEGATE or vice versa.
@@ -63,10 +80,14 @@ def download(self):
63
80
64
81
:return: None
65
82
"""
66
- serialized_model = self ._communicator .download ({
67
- "client_id" : self ._client_id ,
68
- "model_id" : self ._model_id
69
- })
83
+ serialized_model = self ._communicator .download (
84
+ self .encrypt_compress (
85
+ {
86
+ "client_id" : self ._client_id ,
87
+ "model_id" : self ._model_id ,
88
+ }
89
+ )
90
+ )
70
91
if serialized_model is None :
71
92
print (PYMILO_CLIENT_FAILED_TO_DOWNLOAD_REMOTE_MODEL )
72
93
return
@@ -79,11 +100,15 @@ def upload(self):
79
100
80
101
:return: None
81
102
"""
82
- succeed = self ._communicator .upload ({
83
- "client_id" : self ._client_id ,
84
- "model_id" : self ._model_id ,
85
- "model" : Export (self ._model ).to_json (),
86
- })
103
+ succeed = self ._communicator .upload (
104
+ self .encrypt_compress (
105
+ {
106
+ "client_id" : self ._client_id ,
107
+ "model_id" : self ._model_id ,
108
+ "model" : Export (self ._model ).to_json (),
109
+ }
110
+ )
111
+ )
87
112
if succeed :
88
113
print (PYMILO_CLIENT_LOCAL_MODEL_UPLOADED )
89
114
else :
@@ -106,14 +131,12 @@ def __getattr__(self, attribute):
106
131
elif self ._mode == Mode .DELEGATE :
107
132
gdst = GeneralDataStructureTransporter ()
108
133
response = self ._communicator .attribute_type (
109
- self ._encryptor .encrypt (
110
- self ._compressor .compress (
111
- {
112
- "client_id" : self ._client_id ,
113
- "model_id" : self ._model_id ,
114
- "attribute" : attribute ,
115
- }
116
- )
134
+ self .encrypt_compress (
135
+ {
136
+ "client_id" : self ._client_id ,
137
+ "model_id" : self ._model_id ,
138
+ "attribute" : attribute ,
139
+ }
117
140
)
118
141
)
119
142
if response ["attribute type" ] == "field" :
@@ -130,10 +153,8 @@ def relayer(*args, **kwargs):
130
153
payload ["args" ] = gdst .serialize (payload , "args" , None )
131
154
payload ["kwargs" ] = gdst .serialize (payload , "kwargs" , None )
132
155
result = self ._communicator .attribute_call (
133
- self ._encryptor .encrypt (
134
- self ._compressor .compress (
135
- payload
136
- )
156
+ self .encrypt_compress (
157
+ payload
137
158
)
138
159
)
139
160
return gdst .deserialize (result , "payload" , None )
0 commit comments