Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_network_response_constructor(make_network_request_and_assert_response):
assert DefaultNetwork().network_response_constructor is DefaultNetworkResponse
class DefaultNetworkResponseSubclass(DefaultNetworkResponse):
pass
class DefaultNetworkSubclass(DefaultNetwork):
@property
def network_response_constructor(self):
return DefaultNetworkResponseSubclass
network = DefaultNetworkSubclass()
response = make_network_request_and_assert_response(network)
assert isinstance(response, DefaultNetworkResponseSubclass)
def network():
return DefaultNetwork()
def mock_network_layer():
return Mock(DefaultNetwork)
def test_default_network_request(make_network_request_and_assert_response):
# pylint:disable=redefined-outer-name
default_network = DefaultNetwork()
make_network_request_and_assert_response(default_network, custom_kwargs='test')
def test_logging_network_logs_request_exception(make_network_request, http_verb, test_url, logger, network):
with patch.object(default_network.DefaultNetwork, 'request') as super_request:
super_request.side_effect = expected_exception = ExceptionSubclass('exception raised from request()')
with pytest.raises(ExceptionSubclass) as pytest_exc_info:
make_network_request(network)
assert pytest_exc_info.value is expected_exception
logger.warning.assert_called_once_with(
network.EXCEPTION_FORMAT,
{'method': http_verb, 'url': test_url, 'exc_type_name': ExceptionSubclass.__name__, 'exc_value': expected_exception},
)
def test_network_response_constructor(make_network_request_and_assert_response):
assert DefaultNetwork().network_response_constructor is DefaultNetworkResponse
class DefaultNetworkResponseSubclass(DefaultNetworkResponse):
pass
class DefaultNetworkSubclass(DefaultNetwork):
@property
def network_response_constructor(self):
return DefaultNetworkResponseSubclass
network = DefaultNetworkSubclass()
response = make_network_request_and_assert_response(network)
assert isinstance(response, DefaultNetworkResponseSubclass)
def test_default_network_retry_after_sleeps(delay):
default_network = DefaultNetwork()
retry_call = Mock()
mock_sleep = Mock()
with patch('boxsdk.network.default_network.time.sleep', mock_sleep):
default_network.retry_after(delay, retry_call, DEFAULT, kwarg=DEFAULT)
mock_sleep.assert_called_once_with(delay)
retry_call.assert_called_once_with(DEFAULT, kwarg=DEFAULT)
# coding: utf-8
from __future__ import absolute_import, unicode_literals
from functools import partial
from pprint import pformat
import sys
from six import text_type
from boxsdk.network.default_network import DefaultNetwork, DefaultNetworkResponse
from boxsdk.util.log import setup_logging
class LoggingNetwork(DefaultNetwork):
"""
SDK Network subclass that logs requests and responses.
"""
LOGGER_NAME = 'boxsdk.network'
REQUEST_FORMAT = '\x1b[36m%(method)s %(url)s %(request_kwargs)s\x1b[0m'
EXCEPTION_FORMAT = '\x1b[31mRequest "%(method)s %(url)s" failed with %(exc_type_name)s exception: %(exc_value)r\x1b[0m'
_COMMON_RESPONSE_FORMAT = '"%(method)s %(url)s" %(status_code)s %(content_length)s\n%(headers)s\n%(content)s\n'
SUCCESSFUL_RESPONSE_FORMAT = '\x1b[32m{0}\x1b[0m'.format(_COMMON_RESPONSE_FORMAT)
ERROR_RESPONSE_FORMAT = '\x1b[31m{0}\x1b[0m'.format(_COMMON_RESPONSE_FORMAT)
def __init__(self, logger=None):
"""
:param logger:
The logger to use. If you instantiate this class more than once, you should use the same logger
to avoid duplicate log entries.
:type logger:
:param client_config:
Object containing client information, including user agent string.
:type client_config:
:class:`Client`
:param proxy_config:
Object containing proxy information.
:type proxy_config:
:class:`Proxy` or None
"""
if translator is None:
translator = Translator(extend_default_translator=True, new_child=True)
self._api_config = api_config or API()
self._client_config = client_config or Client()
self._proxy_config = proxy_config or Proxy()
super(Session, self).__init__()
self._network_layer = network_layer or DefaultNetwork()
self._default_headers = {
'User-Agent': self._client_config.USER_AGENT_STRING,
'X-Box-UA': self._client_config.BOX_UA_STRING,
}
self._translator = translator
self._default_network_request_kwargs = {}
if default_headers:
self._default_headers.update(default_headers)
if default_network_request_kwargs:
self._default_network_request_kwargs.update(default_network_request_kwargs)
self._logger = getLogger(__name__)
def __init__(self):
super(DefaultNetwork, self).__init__()
self._session = requests.Session()
self._logger = getLogger(__name__)