original_docstring: API route for rates based on date input.
docstring: API route for rates based on date input.
docstring_tokens: API route for rates based on date input .
code: async def historical(base: Optional[str] = None, date: Optional[str] = None):
      """API route for rates based on date input."""
  
      if not base:
          base = "EUR"
  
      if date:
          currencies = (
              session.query(Currency.currency, Currency.rate)
              .filter(func.date(Currency.date) == date)
              .all()
          )
          if not currencies:
              raise HTTPException(
                  status_code=404, detail="Invalid date"
              )
  
      base_rate = (
          session.query(Currency.rate, func.max(Currency.date))
          .filter(func.date(Currency.date) == date, Currency.currency == base)
          .first()
      )
      if base_rate[0] is None:
          base = "EUR"
          base_rate = (
              session.query(Currency.rate, func.max(Currency.date))
              .filter(func.date(Currency.date) == date,
              Currency.currency == base)
              .first()
          )
      currencies = (
          session.query(Currency.currency, Currency.rate)
          .filter(func.date(Currency.date) == date)
          .all()
      )
      rates = {}
      for k, v in sorted(currencies):
          if k == base:
              pass
          elif base == "EUR":
              rates.update({k: v})
          else:
              new_rate = float(v) / float(base_rate[0])
              new_rate = round(new_rate, 5)
              rates.update({k: new_rate})
      if base != "EUR":
          eur_rate = float(1) / float(base_rate[0])
          rates.update({"EUR": round(eur_rate, 5)})
          rates = OrderedDict(sorted(rates.items(), key=lambda t: t[0]))
      return {"base": base, "date": date, "rates": rates}
code_tokens: async def historical ( base : Optional [ str ] = None , date : Optional [ str ] = None ) : if not base : base = "EUR" if date : currencies = ( session . query ( Currency . currency , Currency . rate ) . filter ( func . date ( Currency . date ) == date ) . all ( ) ) if not currencies : raise HTTPException ( status_code = 404 , detail = "Invalid date" ) base_rate = ( session . query ( Currency . rate , func . max ( Currency . date ) ) . filter ( func . date ( Currency . date ) == date , Currency . currency == base ) . first ( ) ) if base_rate [ 0 ] is None : base = "EUR" base_rate = ( session . query ( Currency . rate , func . max ( Currency . date ) ) . filter ( func . date ( Currency . date ) == date , Currency . currency == base ) . first ( ) ) currencies = ( session . query ( Currency . currency , Currency . rate ) . filter ( func . date ( Currency . date ) == date ) . all ( ) ) rates = { } for k , v in sorted ( currencies ) : if k == base : pass elif base == "EUR" : rates . update ( { k : v } ) else : new_rate = float ( v ) / float ( base_rate [ 0 ] ) new_rate = round ( new_rate , 5 ) rates . update ( { k : new_rate } ) if base != "EUR" : eur_rate = float ( 1 ) / float ( base_rate [ 0 ] ) rates . update ( { "EUR" : round ( eur_rate , 5 ) } ) rates = OrderedDict ( sorted ( rates . items ( ) , key = lambda t : t [ 0 ] ) ) return { "base" : base , "date" : date , "rates" : rates }
short_docstring: API route for rates based on date input.
short_docstring_tokens: API route for rates based on date input .
comment: """API route for rates based on date input."""