20
20
from os .path import basename
21
21
import platform
22
22
import sys
23
+ import zlib
23
24
from typing import Dict , List , Optional , Tuple , Union
24
25
25
26
import requests
41
42
# import http.client as http_client
42
43
# http_client.HTTPConnection.debuglevel = 1
43
44
45
+ #pylint: disable=too-many-instance-attributes
46
+ #pylint: disable=too-many-locals
44
47
class BaseService :
45
48
"""Common functionality shared by generated service classes.
46
49
@@ -52,6 +55,7 @@ class BaseService:
52
55
authenticator: Adds authentication data to service requests. Defaults to None.
53
56
disable_ssl_verification: A flag that indicates whether verification of the server's SSL
54
57
certificate should be disabled or not. Defaults to False.
58
+ enable_gzip_compression: A flag that indicates whether to enable gzip compression on request bodies
55
59
56
60
Attributes:
57
61
service_url (str): Url to the service endpoint.
@@ -61,6 +65,7 @@ class BaseService:
61
65
default_headers (dict): A dictionary of headers to be sent with every HTTP request to the service endpoint.
62
66
jar (http.cookiejar.CookieJar): Stores cookies received from the service.
63
67
http_config (dict): A dictionary containing values that control the timeout, proxies, and etc of HTTP requests.
68
+ enable_gzip_compression (bool): A flag that indicates whether to enable gzip compression on request bodies
64
69
Raises:
65
70
ValueError: If Authenticator is not provided or invalid type.
66
71
"""
@@ -74,13 +79,15 @@ def __init__(self,
74
79
* ,
75
80
service_url : str = None ,
76
81
authenticator : Authenticator = None ,
77
- disable_ssl_verification : bool = False ) -> None :
82
+ disable_ssl_verification : bool = False ,
83
+ enable_gzip_compression : bool = False ) -> None :
78
84
self .set_service_url (service_url )
79
85
self .http_config = {}
80
86
self .jar = CookieJar ()
81
87
self .authenticator = authenticator
82
88
self .disable_ssl_verification = disable_ssl_verification
83
89
self .default_headers = None
90
+ self .enable_gzip_compression = enable_gzip_compression
84
91
self ._set_user_agent_header (self ._build_user_agent ())
85
92
if not self .authenticator :
86
93
raise ValueError ('authenticator must be provided' )
@@ -124,6 +131,8 @@ def configure_service(self, service_name: str) -> None:
124
131
self .set_disable_ssl_verification (
125
132
bool (config .get ('DISABLE_SSL' ))
126
133
)
134
+ if config .get ('ENABLE_GZIP' ) is not None :
135
+ self .set_enable_gzip_compression (config .get ('ENABLE_GZIP' ) == 'True' )
127
136
128
137
def _set_user_agent_header (self , user_agent_string : str ) -> None :
129
138
self .user_agent_header = {'User-Agent' : user_agent_string }
@@ -245,6 +254,14 @@ def send(self, request: requests.Request, **kwargs) -> DetailedResponse:
245
254
logging .exception ('Error in service call' )
246
255
raise
247
256
257
+ def set_enable_gzip_compression (self , should_enable_compression : bool = False ) -> None :
258
+ """Set value to enable gzip compression on request bodies"""
259
+ self .enable_gzip_compression = should_enable_compression
260
+
261
+ def get_enable_gzip_compression (self ) -> bool :
262
+ """Get value for enabling gzip compression on request bodies"""
263
+ return self .enable_gzip_compression
264
+
248
265
def prepare_request (self ,
249
266
method : str ,
250
267
url : str ,
@@ -309,6 +326,16 @@ def prepare_request(self,
309
326
310
327
self .authenticator .authenticate (request )
311
328
329
+ # Compress the request body if applicable
330
+ if (self .get_enable_gzip_compression () and
331
+ 'content-encoding' not in headers and
332
+ request ['data' ] is not None ):
333
+ headers ['content-encoding' ] = 'gzip'
334
+ uncompressed_data = request ['data' ]
335
+ request_body = zlib .compress (uncompressed_data )
336
+ request ['data' ] = request_body
337
+ request ['headers' ] = headers
338
+
312
339
# Next, we need to process the 'files' argument to try to fill in
313
340
# any missing filenames where possible.
314
341
# 'files' can be a dictionary (i.e { '<part-name>': (<tuple>)} )
0 commit comments