Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlm committed Jun 26, 2023
1 parent 8ec990c commit 7f2a51f
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 32 deletions.
24 changes: 23 additions & 1 deletion botocore/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,11 +506,33 @@ def _compute_request_compression_config(self, config_kwargs):
'request_min_compression_size_bytes'
)
config_kwargs['request_min_compression_size_bytes'] = min_size
# conversion func is skipped so input validation must be done here
# regardless if the value is coming from the config store or the
# config object
self._validate_min_compression_size(min_size)
if disabled is None:
disabled = self._config_store.get_config_variable(
'disable_request_compression'
)
config_kwargs['disable_request_compression'] = disabled
else:
# if the user provided a value we must check if its a boolean
disabled = ensure_boolean(disabled)
config_kwargs['disable_request_compression'] = disabled

def _validate_min_compression_size(self, min_size):
try:
min_size = int(min_size)
except ValueError:
raise botocore.exceptions.InvalidConfigError(
f"Invalid value '{min_size}' for "
"request_min_compression_size_bytes. Value must be an integer."
)
if not 0 <= min_size <= 1048576:
raise botocore.exceptions.InvalidConfigError(
f"Invalid value '{min_size}' for "
"request_min_compression_size_bytes. Value must be between 0 "
"and 1048576."
)

def _ensure_boolean(self, val):
if isinstance(val, bool):
Expand Down
8 changes: 6 additions & 2 deletions botocore/awsrequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ def reset_stream(self):
raise UnseekableStreamError(stream_object=self.body)


class AWSRequestCompressor:
class RequestCompressor:
"""A class that can compress the body of an ``AWSRequest``."""

def compress(self, config, request_dict, operation_model):
Expand Down Expand Up @@ -597,7 +597,11 @@ def _gzip_compress_fileobj(self, body):
return compressed_obj

def _should_compress_request(self, config, body, operation_model):
if config.disable_request_compression is not True:
if (
config.disable_request_compression is not True
and config.signature_version != 'v2'
and operation_model.request_compression is not None
):
# Request is compressed no matter the content length if it has a streaming input.
# However, if the stream has the `requiresLength` trait it is NOT compressed.
if operation_model.has_streaming_input:
Expand Down
6 changes: 5 additions & 1 deletion botocore/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from botocore import waiter, xform_name
from botocore.args import ClientArgsCreator
from botocore.auth import AUTH_TYPE_MAPS
from botocore.awsrequest import prepare_request_dict
from botocore.awsrequest import RequestCompressor, prepare_request_dict
from botocore.config import Config
from botocore.discovery import (
EndpointDiscoveryHandler,
Expand Down Expand Up @@ -71,6 +71,7 @@
's3v4',
)
)
REQUEST_COMPRESSOR = RequestCompressor()


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -943,6 +944,9 @@ def _make_api_call(self, operation_name, api_params):
if event_response is not None:
http, parsed_response = event_response
else:
REQUEST_COMPRESSOR.compress(
self.meta.config, request_dict, operation_model
)
apply_request_checksum(request_dict)
http, parsed_response = self._make_request(
operation_model, request_dict, request_context
Expand Down
2 changes: 1 addition & 1 deletion botocore/configprovider.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
'request_min_compression_size_bytes',
'AWS_REQUEST_MIN_COMPRESSION_SIZE_BYTES',
10240,
int,
None,
),
'disable_request_compression': (
'disable_request_compression',
Expand Down
3 changes: 1 addition & 2 deletions botocore/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import uuid

from botocore import parsers
from botocore.awsrequest import AWSRequestCompressor, create_request_object
from botocore.awsrequest import create_request_object
from botocore.exceptions import HTTPClientError
from botocore.history import get_global_history_recorder
from botocore.hooks import first_non_none_response
Expand Down Expand Up @@ -97,7 +97,6 @@ def __init__(
self.http_session = http_session
if self.http_session is None:
self.http_session = URLLib3Session()
self._request_compressor = AWSRequestCompressor()

def __repr__(self):
return f'{self._endpoint_prefix}({self.host})'
Expand Down
16 changes: 1 addition & 15 deletions botocore/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import botocore
import botocore.auth
from botocore import utils
from botocore.awsrequest import AWSRequestCompressor
from botocore.compat import (
ETree,
OrderedDict,
Expand Down Expand Up @@ -102,7 +101,6 @@
VERSION_ID_SUFFIX = re.compile(r'\?versionId=[^\s]+$')

SERVICE_NAME_ALIASES = {'runtime.sagemaker': 'sagemaker-runtime'}
AWS_REQUEST_COMPRESSOR = AWSRequestCompressor()


def handle_service_name_alias(service_name, **kwargs):
Expand Down Expand Up @@ -1165,17 +1163,6 @@ def urlencode_body(model, params, context, **kwargs):
)


def compress_request(model, params, context, **kwargs):
body = params.get('body')
config = context['client_config']
if config.signature_version == 'v2':
logger.debug(
"Request compression is not supported for signature version 2"
)
elif model.request_compression and body is not None:
AWS_REQUEST_COMPRESSOR.compress(body, context['client_config'], model)


# This is a list of (event_name, handler).
# When a Session is created, everything in this list will be
# automatically registered with that Session.
Expand Down Expand Up @@ -1248,8 +1235,6 @@ def compress_request(model, params, context, **kwargs):
('before-call.neptune.CreateDBCluster', inject_presigned_url_rds),
('before-call.docdb.CopyDBClusterSnapshot', inject_presigned_url_rds),
('before-call.docdb.CreateDBCluster', inject_presigned_url_rds),
('before-call', urlencode_body),
('before-call', compress_request),
('before-call.s3.PutObject', conditionally_calculate_md5),
('before-call.s3.UploadPart', conditionally_calculate_md5),
('before-call.s3.DeleteObjects', escape_xml_payload),
Expand Down Expand Up @@ -1436,5 +1421,6 @@ def compress_request(model, params, context, **kwargs):
AutoPopulatedParam('PreSignedUrl').document_auto_populated_param,
),
('before-call', inject_api_version_header_if_needed),
('before-call', urlencode_body),
]
_add_parameter_aliases(BUILTIN_HANDLERS)
18 changes: 9 additions & 9 deletions tests/unit/test_awsrequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
AWSHTTPConnection,
AWSHTTPSConnection,
AWSRequest,
AWSRequestCompressor,
AWSResponse,
HeadersDict,
RequestCompressor,
create_request_object,
prepare_request_dict,
)
Expand Down Expand Up @@ -186,8 +186,8 @@ def request_dict_with_content_encoding_header():


@pytest.fixture(scope="module")
def aws_request_compressor():
return AWSRequestCompressor()
def request_compressor():
return RequestCompressor()


@pytest.fixture(scope="module")
Expand Down Expand Up @@ -1004,14 +1004,14 @@ def test_iteration(self):
],
)
def test_compress(
aws_request_compressor,
request_compressor,
config,
request_dict,
operation_model,
is_compressed,
encoding,
):
aws_request_compressor.compress(config, request_dict, operation_model)
request_compressor.compress(config, request_dict, operation_model)
_assert_compression(is_compressed, request_dict['body'])
assert (
'headers' in request_dict
Expand All @@ -1021,9 +1021,9 @@ def test_compress(


@pytest.mark.parametrize('body', [1, object(), None, True, 1.0])
def test_compress_bad_types(aws_request_compressor, body):
def test_compress_bad_types(request_compressor, body):
request_dict = {'body': body}
aws_request_compressor.compress(
request_compressor.compress(
COMPRESSION_CONFIG_0_BYTES, request_dict, OP_WITH_COMPRESSION
)
assert request_dict['body'] == body
Expand All @@ -1033,8 +1033,8 @@ def test_compress_bad_types(aws_request_compressor, body):
'body',
[io.StringIO("foo"), io.BytesIO(b"foo")],
)
def test_body_streams_position_reset(aws_request_compressor, body):
aws_request_compressor.compress(
def test_body_streams_position_reset(request_compressor, body):
request_compressor.compress(
COMPRESSION_CONFIG_0_BYTES, {'body': body}, OP_WITH_COMPRESSION
)
assert body.tell() == 0
Expand Down
1 change: 0 additions & 1 deletion tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1298,7 +1298,6 @@ def test_events_are_per_client(self):
second_client = creator.create_client(
'myservice', 'us-west-2', credentials=self.credentials
)

first_client.meta.events.register('before-call', first_handler)
second_client.meta.events.register('before-call', second_handler)

Expand Down

0 comments on commit 7f2a51f

Please sign in to comment.