Exemplo n.º 1
0
async def get_currency(request, currency: str) -> GetCurrencyModel:
    db = await utils.get_db()
    doc = await db.currency_data.find_one({'char_code': currency.upper()})
    if not doc:
        raise APIException('currency not found', code=400)
    else:
        return GetCurrencyModel({
            'currency': doc["char_code"],
            'rub_course': doc["value"]
        })
Exemplo n.º 2
0
async def convert(request, data: ConvertRequestModel) -> ConvertResponseModel:
    try:
        data.validate()
    except Exception as e:
        raise APIException(e.messages, code=400)

    db = await utils.get_db()
    from_cur = await db.currency_data.find_one(
        {'char_code': data.from_currency.upper()})
    to_cur = await db.currency_data.find_one(
        {'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
    })
Exemplo n.º 3
0
 async def handle_api_exception(request) -> User:
     raise APIException("Something bad happened", code=404)
Exemplo n.º 4
0
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)
Exemplo n.º 5
0
async def notify_not_allowed():
    return APIException(
        "Method GET not allowed. "
        "Use POST with a JSON body with the right format", code=405)