-
Notifications
You must be signed in to change notification settings - Fork 0
/
fxrate.sc
27 lines (23 loc) · 980 Bytes
/
fxrate.sc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// scala 2.13.4
import $file.currency, currency._
import atto.Atto.toParserOps
import cats.effect.{BracketThrow, Sync}
import cats.syntax.all._
import eu.timepit.refined.api.Refined
import eu.timepit.refined.string.Url
import eu.timepit.refined.types.numeric.PosBigDecimal
import io.circe.KeyDecoder
import io.circe.refined._
import org.http4s._
import org.http4s.circe.{JsonDecoder, jsonOf}
import org.http4s.client.Client
implicit val currencyPairDecoder: KeyDecoder[CurrencyPair] = currencyPair.parseOnly(_).option
trait FXRatesClient[F[_]] {
def getFXRates(uri: String Refined Url): F[Map[CurrencyPair, PosBigDecimal]]
}
final class FXRatesClientInterpreter[F[_] : JsonDecoder : BracketThrow : Sync](client: Client[F]) extends FXRatesClient[F] {
override def getFXRates(uri: String Refined Url): F[Map[CurrencyPair, PosBigDecimal]] =
Uri.fromString(uri.value).liftTo[F].flatMap { uri =>
client.expect(uri)(jsonOf[F, Map[CurrencyPair, PosBigDecimal]])
}
}