Skip to content

Commit

Permalink
Merge pull request #355 from hearchco/as/feat/exchange-currencies
Browse files Browse the repository at this point in the history
feat(exchange): supported currencies route
  • Loading branch information
aleksasiriski authored Jul 18, 2024
2 parents e41144c + fadfd63 commit 259f17f
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/router/routes/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,10 @@ type ExchangeResponse struct {
Amount float64 `json:"amount"`
Result float64 `json:"result"`
}

type CurrenciesResponse struct {
responseBase

Base currency.Currency `json:"base"`
Currencies currency.Currencies `json:"currencies"`
}
57 changes: 57 additions & 0 deletions src/router/routes/route_currencies.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package routes

import (
"fmt"
"net/http"
"time"

"github.com/hearchco/agent/src/cache"
"github.com/hearchco/agent/src/config"
"github.com/hearchco/agent/src/exchange"
"github.com/rs/zerolog/log"
)

func routeCurrencies(w http.ResponseWriter, ver string, conf config.Exchange, db cache.DB, ttl time.Duration) error {
// Capture start time.
startTime := time.Now()

// Get the cached currencies.
currencies, err := db.GetCurrencies(conf.BaseCurrency, conf.Engines)
if err != nil {
log.Error().
Err(err).
Str("base", conf.BaseCurrency.String()).
Str("engines", fmt.Sprintf("%v", conf.Engines)).
Msg("Error while getting currencies from cache")
}

// Create the exchange.
var exch exchange.Exchange
if currencies == nil {
// Fetch the currencies from the enabled engines.
exch = exchange.NewExchange(conf)
// Cache the currencies if any have been fetched.
if len(exch.Currencies()) > 0 {
err := db.SetCurrencies(conf.BaseCurrency, conf.Engines, exch.Currencies(), ttl)
if err != nil {
log.Error().
Err(err).
Str("base", conf.BaseCurrency.String()).
Str("engines", fmt.Sprintf("%v", conf.Engines)).
Msg("Error while setting currencies in cache")
}
}
} else {
// Use the cached currencies.
exch = exchange.NewExchange(conf, currencies)
}

return writeResponseJSON(w, http.StatusOK, CurrenciesResponse{
responseBase{
ver,
time.Since(startTime).Milliseconds(),
},
conf.BaseCurrency,
exch.Currencies(),
})
}
22 changes: 22 additions & 0 deletions src/router/routes/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,28 @@ func Setup(mux *chi.Mux, ver string, db cache.DB, conf config.Config) {
}
})

// /currencies
mux.Get("/currencies", func(w http.ResponseWriter, r *http.Request) {
err := routeCurrencies(w, ver, conf.Exchange, db, conf.Server.Cache.TTL.Currencies)
if err != nil {
log.Error().
Err(err).
Str("path", r.URL.Path).
Str("method", r.Method).
Msg("Failed to send response")
}
})
mux.Post("/currencies", func(w http.ResponseWriter, r *http.Request) {
err := routeCurrencies(w, ver, conf.Exchange, db, conf.Server.Cache.TTL.Currencies)
if err != nil {
log.Error().
Err(err).
Str("path", r.URL.Path).
Str("method", r.Method).
Msg("Failed to send response")
}
})

// /proxy
mux.Get("/proxy", func(w http.ResponseWriter, r *http.Request) {
err := routeProxy(w, r, conf.Server.ImageProxy.Salt, conf.Server.ImageProxy.Timeout)
Expand Down

0 comments on commit 259f17f

Please sign in to comment.