Exemplo n.º 1
0
    def add_transmute_route(self, *args):
        """
        two formats are accepted, for transmute routes. One allows
        for a more traditional aiohttp syntax, while the other
        allows for a flask-like variant.

        .. code-block:: python

            # if the path and method are not added in describe.
            add_transmute_route("GET", "/route", fn)

            # if the path and method are already added in describe
            add_transmute_route(fn)
        """
        if len(args) == 1:
            fn = args[0]
        elif len(args) == 3:
            methods, paths, fn = args
            describe(methods=methods, paths=paths)(fn)
        else:
            raise ValueError(
                "expected one or three arguments for add_transmute_route!"
            )
        transmute_func = TransmuteFunction(
            fn,
            args_not_from_request=["request"]
        )
        handler = create_handler(
            transmute_func, context=self._transmute_context
        )
        swagger_path = transmute_func.get_swagger_path(self._transmute_context)
        for p in transmute_func.paths:
            # add to swagger
            if p not in self._swagger:
                self._swagger[p] = swagger_path
            else:
                for method, definition in swagger_path.items():
                    setattr(self._swagger[p], method, definition)

            # add to aiohttp
            aiohttp_path = self._convert_to_aiohttp_path(p)
            resource = self.add_resource(aiohttp_path)
            for method in transmute_func.methods:
                resource.add_route(method, handler)
Exemplo n.º 2
0
 def decorator(fn):
     fn = describe(**kwargs)(fn)
     transmute_func = TransmuteFunction(fn)
     routes, handler = create_routes_and_handler(transmute_func, context)
     for r in routes:
         # push swagger info.
         if not hasattr(app_or_blueprint, SWAGGER_ATTR_NAME):
             setattr(app_or_blueprint, SWAGGER_ATTR_NAME, SwaggerSpec())
         swagger_obj = getattr(app_or_blueprint, SWAGGER_ATTR_NAME)
         swagger_obj.add_func(transmute_func, context)
         app_or_blueprint.route(r, methods=transmute_func.methods)(handler)
     return handler
Exemplo n.º 3
0
 def decorator(fn):
     fn = describe(**kwargs)(fn)
     transmute_func = TransmuteFunction(fn)
     routes, handler = create_routes_and_handler(transmute_func, context)
     for r in routes:
         # push swagger info.
         if not hasattr(app_or_blueprint, SWAGGER_ATTR_NAME):
             setattr(app_or_blueprint, SWAGGER_ATTR_NAME, SwaggerSpec())
         swagger_obj = getattr(app_or_blueprint, SWAGGER_ATTR_NAME)
         swagger_obj.add_func(transmute_func, context)
         app_or_blueprint.route(r, methods=transmute_func.methods)(handler)
     return handler
    def add_transmute_route(self, *args):
        """
        two formats are accepted, for transmute routes. One allows
        for a more traditional aiohttp syntax, while the other
        allows for a flask-like variant.

        .. code-block:: python

            # if the path and method are not added in describe.
            add_transmute_route("GET", "/route", fn)

            # if the path and method are already added in describe
            add_transmute_route(fn)
        """
        if len(args) == 1:
            fn = args[0]
        elif len(args) == 3:
            methods, paths, fn = args
            fn = describe(methods=methods, paths=paths)(fn)
        else:
            raise ValueError(
                "expected one or three arguments for add_transmute_route!")

        add_route(self._app, fn, context=self._transmute_context)
Exemplo n.º 5
0
        'description': 'Wrong request format etc'
    },
    400: {
        'type': str,
        'description':
        'Application level error like user already exists and so on'
    },
}

user_delete = route(paths='/users/{user_id}', methods=['DELETE'])(
    describe(  # we have to duplicate due to bug https://github.com/toumorokoshi/flask-transmute/issues/11
        paths='/users/{user_id}',
        methods=['DELETE'],
        header_parameters=['Authorization'],
        parameter_descriptions={'user_id': 'ID of user to delete'},
        response_types=dict(
            chain(error_responces.items(),
                  {200: {
                      'type': str,
                      'description': 'Success'
                  }}.items())))(api(delete_user)))

user_create = route(paths='/users', methods=['POST'])(describe(
    paths='/users',
    methods=['POST'],
    body_parameters=['new_user'],
    header_parameters=['Authorization'],
    parameter_descriptions={'new_user': '******'},
    response_types=dict(
        chain(error_responces.items(),
              {200: {