예제 #1
0
def getFromApiAndCommit():
    rates = []
    url = baseUrl.format(time, accessKey)
    try:
        response = requests.get(url)
        if response.status_code == 200:
            json = response.json()
            ratesKvp = json["rates"]
            date = json["date"]
            for rate in ratesKvp:
                r = Currency(name=rate, rate=ratesKvp[rate], date=date)
                rates.append(r)
                db.session.add(r)
            db.session.commit()
        else:
            print("Failed to get rates, {}".format(response.status_code))
            raise CurrencyError("Failed to get rates from API, {}".format(
                response.status_code))
    except Exception as e:
        db.session.rollback()
        db.session.flush()
        print(str(e))
        raise CurrencyError(str(e))

    return rates
예제 #2
0
def convert(from_currency: str, to: str, amount: str, date: str):
    try:
        from_currency = from_currency.upper()
        to = to.upper()
        currencies = [from_currency, to]
        rates = Currency.query.filter(Currency.name.in_(currencies),
                                      Currency.date == parse(date)).all()
        fromRate = None
        toRate = None
        for rate in rates:
            if rate.name == from_currency:
                fromRate = rate
            if rate.name == to:
                toRate = rate

        print(fromRate)
        if fromRate is None:
            raise CurrencyError('Currency {} not found'.format(from_currency))
        if toRate is None:
            raise CurrencyError('Currency {} not found'.format(to))

        amountInEuro = float(amount) / fromRate.rate
        amountInTo = amountInEuro * toRate.rate
        return {"from": from_currency,
                "to": to,
                "from amount": amount,
                "to amount": amountInTo}

    except Exception as e:
        print(str(e))
        raise CurrencyError(str(e))
예제 #3
0
def validate_convert_fields(from_currency: str, to: str, amount: str,
                            time: str):
    msg = ''
    if from_currency is None or len(from_currency.strip()) == 0:
        msg += 'From Currency param is mandatory! '
    if to is None or len(to.strip()) == 0:
        msg += 'To Currency param is mandatory! '
    if amount is None or len(amount.strip()) == 0:
        msg += 'Amount param is mandatory! '

    if len(msg.strip()) > 0:
        raise CurrencyError(msg)

    try:
        a = float(amount)
        if a < 0:
            msg += 'Amount must be positive number! '
    except ValueError:
        msg += 'Amount must be a number! '

    try:
        if len(time.strip()) > 0:
            parse(time)
    except ValueError:
        msg += 'Invalid Date Format! Valid form - YYYY-mm-dd! '

    if len(msg.strip()) > 0:
        raise CurrencyError(msg)
예제 #4
0
def getInfoForToday():
    try:
        synced: CurrencySync = CurrencySync.query.filter_by(
            date=date.today()).first()
        if synced is None:
            raise CurrencyError('Missing Status in DB!')
        return synced
    except Exception as e:
        print(str(e))
        raise CurrencyError(str(e))
예제 #5
0
def validate_rates_fields(base: str, to: str, time: str):
    msg = ''
    if base is None or len(base.strip()) == 0:
        msg += 'Base is mandatory! '

    if len(msg.strip()) > 0:
        raise CurrencyError(msg)

    try:
        if len(time.strip()) > 0:
            parse(time)
    except ValueError:
        msg += 'Invalid Date Format! Valid form - YYYY-mm-dd! '

    if len(msg.strip()) > 0:
        raise CurrencyError(msg)
예제 #6
0
def get_rates(base: str, to: str, time: str):
    to_array = []
    base = base.upper()

    if to is not None and len(to.strip()) > 0:
        to_array = to.split(',')
        to_array = [currency.upper() for currency in to_array]
    to_array.insert(0, base)

    if len(to_array) > 1:
        rates = Currency.query.filter(Currency.name.in_(to_array),
                                      Currency.date == parse(time)).all()
    else:
        rates = Currency.query.filter(Currency.date == parse(time)).all()

    fromRate = None
    for rate in rates:
        if rate.name == base:
            fromRate = rate
            break

    if fromRate is None:
        raise CurrencyError('Base Currency {} not found'.format(base))

    rates_dict = {
        rate.name: rate.rate / fromRate.rate
        for rate in rates if rate.name != fromRate.name
    }
    response = {'base': base, 'date': time, 'rates': rates_dict}

    return response
예제 #7
0
def getRatesFromDb():
    try:
        rates = Currency.query.filter_by(date=date.today()).all()
        return rates
    except Exception as e:
        print(str(e))
        raise CurrencyError(str(e))
예제 #8
0
def syncToday():
    try:
        print("Mark today as Synced")
        synced: CurrencySync = CurrencySync.query.filter_by(
            date=date.today()).first()
        synced.isSynced = True
        db.session.add(synced)
        db.session.commit()
    except Exception as e:
        db.session.rollback()
        db.session.flush()
        print(str(e))
        raise CurrencyError(str(e))
예제 #9
0
def raise_error():
    raise CurrencyError("error")