How to use the boxsdk.util.translator.Translator function in boxsdk

To help you get started, we’ve selected a few boxsdk examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github box / box-python-sdk / test / unit / util / test_translator.py View on Github external
def test_translator_converts_response_to_correct_type(response_type):
    response, object_class = _response_to_class_mapping[response_type]
    assert type(Translator().get(response.json()['type']) == object_class)
github box / box-python-sdk / test / unit / util / test_translator.py View on Github external
def test_without_extend_default_translator(new_child):
    item_type = 'foo'

    class Foo(object):
        pass

    mapping = {item_type: Foo}

    translator = Translator(mapping, extend_default_translator=False, new_child=new_child)
    assert translator == mapping
github box / box-python-sdk / test / unit / util / test_translator.py View on Github external
def test_with_extend_default_translator(default_translator, extend_default_translator, new_child):
    item_type = 'foo'

    class Foo(object):
        pass

    kwargs = {}
    if extend_default_translator is not None:
        kwargs['extend_default_translator'] = extend_default_translator
    translator = Translator({item_type: Foo}, new_child=new_child, **kwargs)
    assert set(translator.items()).issuperset(default_translator.items())
github box / box-python-sdk / test / unit / util / test_translator.py View on Github external
def test_without_new_child(extend_default_translator):
    item_type = 'foo'

    class Foo(object):
        pass

    mapping = {item_type: Foo}

    translator = Translator(mapping, new_child=False, extend_default_translator=extend_default_translator)
    assert item_type in translator
    assert translator.maps[0] is mapping

    class Bar(Foo):
        pass

    translator.register(item_type, Bar)
    assert translator.get(item_type) is Bar
    assert mapping == {item_type: Bar}

    del translator[item_type]
    assert not mapping
github box / box-python-sdk / boxsdk / util / translator.py View on Github external
# Metadata field objects are another issue; they contain a 'type' property that doesn't really
        # map to a Box object.  We probably want to treat these as just `dict`s, so they're excluded here
        if object_class is not None and '$type' not in translated_obj:
            param_values = {
                'session': session,
                'response_object': translated_obj,
                'object_id': _get_object_id(translated_obj),
            }
            params = inspect_signature(object_class.__init__).parameters
            param_values = {p: param_values[p] for p in params if p in param_values}
            return object_class(**param_values)

        return translated_obj


Translator._default_translator = Translator(extend_default_translator=False)  # pylint:disable=protected-access
github box / box-python-sdk / boxsdk / object / base_api_json_object.py View on Github external
def __init__(cls, name, bases, attrs):
        super(BaseAPIJSONObjectMeta, cls).__init__(name, bases, attrs)
        item_type = attrs.get('_item_type', None)
        if item_type is not None:
            Translator._default_translator.register(item_type, cls)   # pylint:disable=protected-access
github box / box-python-sdk / boxsdk / session / session.py View on Github external
`dict` or None
        :param api_config:
            Object containing URLs for the Box API.
        :type api_config:
            :class:`API`
        :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__)