types.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package currency
  2. import (
  3. "time"
  4. )
  5. // Config holds configuration for the currency converter
  6. type Config struct {
  7. APIKey string
  8. CacheTTL time.Duration
  9. APITimeout time.Duration
  10. }
  11. // Converter interface defines currency conversion operations
  12. type Converter interface {
  13. // Convert converts an amount from one currency to another
  14. Convert(amount float64, from, to string) (float64, error)
  15. // GetRate returns the exchange rate between two currencies
  16. GetRate(from, to string) (float64, error)
  17. }
  18. // exchangeRateResponse represents the API response from exchangerate-api.com
  19. type exchangeRateResponse struct {
  20. Result string `json:"result"`
  21. Documentation string `json:"documentation"`
  22. TermsOfUse string `json:"terms_of_use"`
  23. TimeLastUpdateUnix int64 `json:"time_last_update_unix"`
  24. TimeLastUpdateUTC string `json:"time_last_update_utc"`
  25. TimeNextUpdateUnix int64 `json:"time_next_update_unix"`
  26. TimeNextUpdateUTC string `json:"time_next_update_utc"`
  27. BaseCode string `json:"base_code"`
  28. ConversionRates map[string]float64 `json:"conversion_rates"`
  29. }
  30. // cachedRates stores exchange rates with metadata
  31. type cachedRates struct {
  32. rates map[string]float64
  33. baseCode string
  34. fetchedAt time.Time
  35. validUntil time.Time
  36. }
  37. // client interface for fetching exchange rates
  38. type client interface {
  39. // fetchRates fetches current exchange rates for a base currency
  40. fetchRates(baseCurrency string) (*exchangeRateResponse, error)
  41. }
  42. // cache interface for storing exchange rates
  43. type cache interface {
  44. // get retrieves cached rates for a base currency
  45. get(baseCurrency string) (*cachedRates, bool)
  46. // set stores rates for a base currency with TTL
  47. set(baseCurrency string, rates *cachedRates)
  48. // clear removes all cached rates
  49. clear()
  50. }