How to use the uplink.decorators.MethodAnnotation 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_decorators.py View on Github external
def method_annotation():
    return decorators.MethodAnnotation()
github prkumar / uplink / tests / unit / test_decorators.py View on Github external
def test_method_not_in_http_method_blacklist(
        self, request_definition_builder
    ):
        class DummyAnnotation(decorators.MethodAnnotation):
            _http_method_whitelist = ["POST"]

        request_definition_builder.method = "POST"
        request_definition_builder.__name__ = "dummy"
        assert DummyAnnotation().supports_http_method(
            request_definition_builder.method
        )
github prkumar / uplink / tests / unit / test_decorators.py View on Github external
def test_class_level_appears_before_method_level_annotations(
        self, method_handler_builder
    ):
        method_level1 = decorators.MethodAnnotation()
        method_level2 = decorators.MethodAnnotation()
        class_level1 = decorators.MethodAnnotation()
        class_level2 = decorators.MethodAnnotation()
        method_handler_builder.add_annotation(method_level1)
        method_handler_builder.add_annotation(method_level2)
        method_handler_builder.add_annotation(class_level1, is_class=True)
        method_handler_builder.add_annotation(class_level2, is_class=True)
        handler = method_handler_builder.build()
        assert list(handler.annotations) == [
            class_level1,
            class_level2,
            method_level1,
            method_level2,
        ]
github prkumar / uplink / uplink / decorators.py View on Github external
if self._is_consumer_class(class_or_builder):
            builders = helpers.get_api_definitions(class_or_builder)
            builders = filter(self._is_relevant_for_builder, builders)

            for name, b in builders:
                b.method_handler_builder.add_annotation(self, is_class=True)
                helpers.set_api_definition(class_or_builder, name, b)
        elif isinstance(class_or_builder, interfaces.RequestDefinitionBuilder):
            class_or_builder.method_handler_builder.add_annotation(self)
        return class_or_builder

    def modify_request(self, request_builder):
        pass


class _BaseRequestProperties(MethodAnnotation):
    _property_name = None
    _delimiter = None

    def __init__(self, arg, **kwargs):
        if isinstance(arg, list):
            self._values = dict(self._split(a) for a in arg)
        else:
            self._values = dict(arg, **kwargs)

    def _split(self, arg):
        return map(str.strip, arg.split(self._delimiter))

    def modify_request(self, request_builder):
        """Updates header contents."""
        request_builder.info[self._property_name].update(self._values)
github prkumar / uplink / uplink / decorators.py View on Github external
def update_user(self, first_name: Field, last_name: Field):
                \"""Update the current user.\"""
    """

    _http_method_blacklist = {"GET"}
    _can_be_static = True

    # XXX: Let `requests` handle building urlencoded syntax.
    # def modify_request(self, request_builder):
    #     request_builder.info.headers(
    #         {"Content-Type": "application/x-www-form-urlencoded"}
    #     )


# noinspection PyPep8Naming
class multipart(MethodAnnotation):
    """
    Sends multipart form data.

    Multipart requests are commonly used to upload files to a server.
    Further, annotate each part argument with :py:class:`Part`.

    Example:

        .. code-block:: python

            @multipart
            @put(/user/photo")
            def update_user(self, photo: Part, description: Part):
                \"""Upload a user profile photo.\"""
    """
github prkumar / uplink / uplink / decorators.py View on Github external
if inspect.isfunction(obj):
            handler = arguments.ArgumentAnnotationHandlerBuilder.from_func(obj)
            self._helper(handler)
            return obj
        else:
            return super(args, self).__call__(obj)

    def _helper(self, builder):
        builder.set_annotations(self._annotations, **self._more_annotations)

    def modify_request_definition(self, request_definition_builder):
        """Modifies dynamic requests with given annotations"""
        self._helper(request_definition_builder.argument_handler_builder)


class _InjectableMethodAnnotation(MethodAnnotation):
    def modify_request(self, request_builder):
        request_builder.add_transaction_hook(self)


class _BaseHandlerAnnotation(_InjectableMethodAnnotation):
    def __new__(cls, func=None, *args, **kwargs):
        if func is None:
            return lambda f: cls(f, *args, **kwargs)
        self = super(_BaseHandlerAnnotation, cls).__new__(cls)
        functools.update_wrapper(self, func)
        return self


# noinspection PyPep8Naming
class response_handler(_BaseHandlerAnnotation, hooks.ResponseHandler):
    """
github prkumar / uplink / uplink / decorators.py View on Github external
Args:
        seconds (int): An integer used to indicate how long should the
            request wait.
    """

    def __init__(self, seconds):
        self._seconds = seconds

    def modify_request(self, request_builder):
        """Modifies request timeout."""
        request_builder.info["timeout"] = self._seconds


# noinspection PyPep8Naming
class args(MethodAnnotation):
    """
    Annotate method arguments for Python 2.7 compatibility.

    Arrange annotations in the same order as their corresponding
    function arguments.

    Example:
        .. code-block:: python

            @args(Path, Query)
            @get("/users/{username})
            def get_user(self, username, visibility):
                \"""Get a specific user.\"""

    Use keyword args to target specific method parameters.
github prkumar / uplink / uplink / decorators.py View on Github external
def _is_static_call(cls, *args_, **kwargs):
        if super(MethodAnnotation, cls)._is_static_call(*args_, **kwargs):
            return True
        try:
            is_consumer_class = cls._is_consumer_class(args_[0])
        except IndexError:
            return False
        else:
            return is_consumer_class and not (kwargs or args_[1:])
github prkumar / uplink-protobuf / uplink_protobuf / decorators.py View on Github external
# Third party imports
from uplink import decorators

__all__ = ["configure_json_response", "configure_json_request"]


# noinspection PyPep8Naming
class configure_json_response(decorators.MethodAnnotation):
    _can_be_static = False

    def __init__(self, ignore_unknown_fields=False, **kwargs):
        self._options = {"ignore_unknown_fields": ignore_unknown_fields}
        self._options.update(kwargs)

    @property
    def options(self):
        return self._options.copy()


# noinspection PyPep8Naming
class configure_json_request(decorators.MethodAnnotation):
    _can_be_static = False

    def __init__(