Example #1
0
 def decorator(fnc):
     if hasattr(fnc, "transmute"):
         fnc.transmute = fnc.transmute | attrs
     else:
         fnc.transmute = attrs
     add_route(blueprint, fnc)
     return fnc
Example #2
0
def app():
    app = Sanic('sanic_test_app')

    # App Handlers
    async def hello_world(request):
        return HTTPResponse(body="Hello World")

    @describe(paths="/api/v1/user/{user}", methods="GET")
    async def get_path_parameters(request, user: str) -> str:
        return user

    @describe(paths="/api/v1/group/", methods="GET")
    async def get_path_parameters_list(request, group: [str] = None) -> str:
        if not group:
            group = []
        group = sorted(group)
        return ",".join(group)

    @describe(paths="/api/v1/env/", methods="GET")
    async def get_parameters_optional(request, exist: bool = False) -> bool:
        return exist

    @describe(paths="/multiply")
    async def get_query_parameters(request, left: int, right: int) -> int:
        return left * right

    @describe(paths="/killme")
    async def handle_exceptions(request) -> User:
        raise ServerError("Something bad happened", status_code=500)

    @describe(paths="/api/v1/user/missing")
    async def handle_api_exception(request) -> User:
        raise APIException("Something bad happened", code=404)

    @describe(paths="/api/v1/headers/",
              response_types={
                  200: {
                      "type": str,
                      "description": "success",
                      "headers": {
                          "location": {
                              "description": "url to the location",
                              "type": str
                          }
                      }
                  }
              })
    async def get_headers(request):
        return Response(
            "headers",
            headers={"location": "foooo"},
        )

    @describe(paths="/body_and_header",
              methods="POST",
              body_parameters=["body"],
              header_parameters=["header"])
    async def body_and_header_params(request, body: str, header: str) -> bool:
        return body == header

    # Routes
    app.add_route(hello_world, "/", methods=["GET"])
    add_route(app, get_path_parameters)
    add_route(app, get_parameters_optional)
    add_route(app, get_path_parameters_list)
    add_route(app, get_query_parameters)
    add_route(app, handle_exceptions)
    add_route(app, handle_api_exception)
    add_route(app, get_headers)
    add_route(app, body_and_header_params)

    # Blueprints
    @describe(paths="/multiply")
    async def get_blueprint_params(request, left: int, right: int) -> str:
        res = left * right
        return "{left}*{right}={res}".format(left=left, right=right, res=res)

    bp = Blueprint("test_blueprints", url_prefix="/blueprint")
    add_route(bp, get_blueprint_params)
    app.blueprint(bp)

    # Swagger
    add_swagger(app, "/api/v1/swagger.json", "/api/v1/")

    return app
Example #3
0
        {'char_code': data.to_currency.upper()})

    to_cur_value = None
    from_cur_value = None

    if data.from_currency.upper() == "RUB":
        from_cur_value = 1
    else:
        if not from_cur:
            raise APIException('from_currency not found', code=400)
        else:
            from_cur_value = from_cur['value']

    if data.to_currency.upper() == "RUB":
        to_cur_value = 1
    else:
        if not to_cur:
            raise APIException('to_currency not found', code=400)
        else:
            to_cur_value = to_cur['value']

    result = from_cur_value * data.amount / to_cur_value

    return ConvertResponseModel({
        'currency': data.to_currency,
        'amount': result
    })


add_route(api_blueprint, get_currency)
add_route(api_blueprint, convert)
Example #4
0
from sanic import Blueprint
from sanic_transmute import add_route
from .views import (
    get_all,
    get_status_by_country_id,
    get_status_by_country_name,
    get_deaths,
    get_active_cases,
    get_recovered_cases,
    get_confirmed_cases,
    list_countries,
)

cases = Blueprint("cases", url_prefix="/cases")
add_route(cases, get_all)
add_route(cases, get_status_by_country_id)
add_route(cases, get_status_by_country_name)
add_route(cases, get_deaths)
add_route(cases, get_active_cases)
add_route(cases, get_recovered_cases)
add_route(cases, get_confirmed_cases)
add_route(cases, list_countries)
Example #5
0
    raise ServerError("Something bad happened", status_code=500)


@describe(paths="/api/v1/user/missing")
async def handle_api_exception(request) -> User:
    """
    API Description: Handle APIException. This will show in the swagger page (localhost:8000/api/v1/).
    """
    raise APIException("Something bad happened", code=404)


@describe(paths="/multiply")
async def get_blueprint_params(request, left: int, right: int) -> str:
    """
    API Description: Multiply, left * right. This will show in the swagger page (localhost:8000/api/v1/).
    """
    res = left * right
    return "{left}*{right}={res}".format(left=left, right=right, res=res)


if __name__ == "__main__":
    add_route(app, test_transmute_get)
    add_route(app, test_transmute_post)
    add_route(app, handle_exception)
    add_route(app, handle_api_exception)
    # register blueprints
    add_route(bp, get_blueprint_params)
    app.blueprint(bp)
    # add swagger
    add_swagger(app, "/api/v1/swagger.json", "/api/v1/")
    app.run(host="0.0.0.0", port=8000)
Example #6
0
from sanic import Blueprint
from sanic_transmute import add_route

from app.api.v5.users import get_all_users_method, update_user_by_id_method, add_user_method, get_user_by_id_method, \
    delete_user_by_id_method

api_v5_blueprint = Blueprint('api_v5', url_prefix='/v5')

add_route(api_v5_blueprint, get_all_users_method)
add_route(api_v5_blueprint, get_user_by_id_method)
add_route(api_v5_blueprint, update_user_by_id_method)
add_route(api_v5_blueprint, add_user_method)
add_route(api_v5_blueprint, delete_user_by_id_method)