def do_conversion(from_currency: str, to_currency: str, amount: float) -> Dict: from_currency = from_currency.upper() to_currency = to_currency.upper() currencies = list( CurrencyRepository.get_by_iso_codes([from_currency, to_currency])) currencies_iso_codes = list(map(lambda c: c.iso_code, currencies)) if from_currency not in currencies_iso_codes: raise BravoException(CRY002(from_currency)) if to_currency not in currencies_iso_codes: raise BravoException(CRY002(to_currency)) standard_currency = CurrencyRepository.get_standard_currency() if standard_currency.iso_code != "BRL": raise BravoException(CRP003) if standard_currency.iso_code != to_currency: currency_pairs = CurrencyService._get_currency_pair( from_currencies=[from_currency, to_currency], to_currency=standard_currency.iso_code, ) from_currency_value = float(currency_pairs[from_currency]["bid"]) to_currency_value = float(currency_pairs[to_currency]["ask"]) currency_value = from_currency_value / to_currency_value else: currency_pairs = CurrencyService._get_currency_pair( from_currencies=[from_currency], to_currency=to_currency) currency_value = float(currency_pairs[from_currency]["bid"]) return {"amount": round(amount * currency_value, 4)}
def _get_currency_pair(from_currencies: List[str], to_currency: str) -> Dict: currency_pair = CurrencyPairIntegration.get_currency_pair( from_currencies=from_currencies, to_currency=to_currency) if not currency_pair: raise BravoException(CRP001) if "status" in currency_pair and currency_pair["status"] == 404: raise BravoException(CRP002) return currency_pair
def update_by_id(self, currency_id: str, new_data: Dict): standard_currency = CurrencyRepository.get_standard_currency() if (standard_currency and str(standard_currency.id) != currency_id and new_data.get("standard")): raise BravoException(CRY001) if "iso_code" in new_data: new_data.update({"iso_code": new_data["iso_code"].upper()}) new_data.update({"update_date": datetime.utcnow()}) currency = super(CurrencyService, self).update_by_id(currency_id, new_data) if not currency: raise BravoException(REP001) return currency
def create(self, data: Dict) -> Dict: standard_currency = CurrencyRepository.get_standard_currency() if standard_currency and data.get("standard"): raise BravoException(CRY001) new_data = {**data, "iso_code": data["iso_code"].upper()} currency = super(CurrencyService, self).create(new_data) return currency
def build_response(data: [BaseDocument, Dict, List], http_status: int = 200) -> Response: new_data = dumps(data, cls=CustomEncoder) res = dumps(serialize_data(loads(new_data))) if not res: return BravoException(GEE004).http_response return Response(res, status=http_status, content_type="application/json")
def func(*args, **kwargs): http_method = request.method query_args = dict(request.args) body = request.get_json(silent=True) if http_method in ["GET"]: data = query_args elif http_method in ["POST", "PUT", "PATCH", "DELETE"]: data = body if http_method == "DELETE" and query_args and not body: data = query_args else: raise BravoException(GEE003) errors = is_valid(schema=validation_schema, data=data) if errors: raise BravoException(GEE005(errors)) return f(*args, **kwargs)
def handler_404(_): return BravoException(GEE006, http_status=404).http_response
def delete_by_id(self, currency_id: str): currency_obj_id = super(CurrencyService, self).delete_by_id(currency_id) if not currency_obj_id: raise BravoException(REP001)