How to use the uplink.converters function in uplink

To help you get started, we’ve selected a few uplink 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 prkumar / uplink / tests / unit / test_converters.py View on Github external
def test_dict_converter(self):
        # Setup
        converter = converters.typing_.DictConverter(int, str)
        converter.set_chain(lambda x: x)

        # Verify
        output = converter({"1": 1, "2": 2})
        assert output == {1: "1", 2: "2"}

        # Verify with non-map: use value converter
        output = converter(1)
        assert output == "1"
github prkumar / uplink / tests / unit / test_converters.py View on Github external
def test_create_response_body_converter_with_unsupported_response(
        self, schema_mock_and_argument
    ):
        # Setup
        schema_mock, argument = schema_mock_and_argument
        converter = converters.MarshmallowConverter()

        # Run
        c = converter.create_response_body_converter(argument)
        result = c.convert("unsupported response")

        # Verify
        return result is None
github prkumar / uplink / tests / unit / test_converters.py View on Github external
def test_convert_not_sequence(self):
        # Setup
        registry = converters.ConverterFactoryRegistry(
            (converters.StandardConverter(),)
        )
        key = converters.keys.Sequence(converters.keys.CONVERT_TO_STRING)

        # Run
        converter = registry[key](None)
        value = converter("1")

        # Verify
        assert value == "1"
github prkumar / uplink / tests / unit / test_commands.py View on Github external
def test_make_converter_registry(self, annotation_handler_mock):
        definition = commands.RequestDefinition(
            "method",
            "uri",
            None,
            annotation_handler_mock,
            annotation_handler_mock,
        )
        annotation_handler_mock.annotations = ("annotation",)
        registry = definition.make_converter_registry(())
        assert isinstance(registry, converters.ConverterFactoryRegistry)
github prkumar / uplink / tests / unit / test_converters.py View on Github external
defaults = registry.get_converter_factories()
        assert return_value is converter
        assert len(defaults) == 1
        assert isinstance(defaults[0], converters.StandardConverter)

    def test_register_converter_factory_fail(self):
        # Setup
        registry = register.Register()

        # Verify failure when registered factory is not proper type.
        with pytest.raises(TypeError):
            registry.register_converter_factory(object())


class TestTypingConverter(object):
    singleton = converters.TypingConverter()
    inject_methods = pytest.mark.parametrize(
        "method, use_typing",
        [
            (singleton.create_request_body_converter, True),
            (singleton.create_request_body_converter, False),
            (singleton.create_response_body_converter, True),
            (singleton.create_response_body_converter, False),
        ],
    )

    @inject_methods
    def test_methods(self, method, use_typing):
        List, Dict = converters.typing_._get_types(use_typing)

        # Verify with sequence
        converter = method(List[str])
github prkumar / uplink / uplink / builder.py View on Github external
def converters(self, converters):
        if isinstance(converters, converters_.interfaces.Factory):
            converters = (converters,)
        self._converters = tuple(converters)
        self._converters += converters_.get_default_converter_factories()
github prkumar / uplink / uplink / models.py View on Github external
# Standard library imports
import functools

# Local imports
from uplink import converters, decorators, returns, utils

__all__ = ["loads", "dumps"]

_get_classes = functools.partial(map, type)


class ResponseBodyConverterFactory(converters.ConverterFactory):
    def __init__(self, delegate):
        self.make_response_body_converter = delegate


class RequestBodyConverterFactory(converters.ConverterFactory):
    def __init__(self, delegate):
        self.make_request_body_converter = delegate


class _Delegate(object):

    def __init__(self, model_class, annotations, func):
        self._model_class = model_class
        self._annotations = annotations
        self._func = func
github prkumar / uplink / uplink / models.py View on Github external
self._annotations = set(annotations)

    def using(self, func):
        """Sets the converter strategy to the given function."""
        delegate = _Delegate(self._model_class, self._annotations, func)
        return self._wrap_delegate(delegate)

    def _wrap_delegate(self, delegate):  # pragma: no cover
        raise NotImplementedError

    def __call__(self, func):
        converter = _Wrapper(self.using(func), func)
        functools.update_wrapper(converter, func)
        return converter

    install = converters.register_default_converter_factory

    @classmethod
    def _make_builder(cls, base_class, annotations, *more_annotations):
        annotations = set(annotations)
        annotations.update(more_annotations)
        return cls(base_class=base_class, annotations=annotations)


# noinspection PyPep8Naming
class loads(_ModelConverterBuilder):
    """
    Builds a custom object deserializer.

    This class takes a single argument, the base model class, and
    registers the decorated function as a deserializer for that base
    class and all subclasses.
github prkumar / uplink / uplink / commands.py View on Github external
def make_converter_registry(self, converters_):
        return converters.ConverterFactoryRegistry(converters_, self)
github prkumar / uplink / uplink / builder.py View on Github external
def __init__(self):
        self._base_url = ""
        self._hooks = []
        self._client = clients.get_client()
        self._converters = converters_.get_default_converter_factories()
        self._auth = auth_.get_auth()