Skip to content

Commit

Permalink
Refactor httpRoutes get using map
Browse files Browse the repository at this point in the history
  • Loading branch information
billy-yuan committed Mar 10, 2024
1 parent ffdddee commit 5a54bda
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 35 deletions.
38 changes: 16 additions & 22 deletions forex-mtl/src/main/scala/forex/http/rates/RatesHttpRoutes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,23 @@ class RatesHttpRoutes[F[_]: Sync](rates: RatesProgram[F]) extends Http4sDsl[F] {

private val httpRoutes: HttpRoutes[F] = HttpRoutes.of[F] {
case GET -> Root :? FromQueryParam(maybeFrom) +& ToQueryParam(maybeTo) =>

val maybeValidatedParams = validateParams(new Params(maybeFrom.toOption, maybeTo.toOption))
.fold(
e => Left(Protocol.GetApiError(ErrorType.InvalidRate, e.sanitized)),
p => Right(p)
validateParams(new Params(maybeFrom.toOption, maybeTo.toOption))
.fold(
err => Left(BadRequest(Protocol.GetApiError(ErrorType.InvalidRate, err.sanitized))),
validatedParams => Right(validatedParams)
)
.map(validatedParams => rates.get(
RatesProgramProtocol.GetRatesRequest(validatedParams.from, validatedParams.to)
))
.map(maybeRate => maybeRate.flatMap { r =>
r.fold(
_ => InternalServerError(Protocol.GetApiError(
ErrorType.InterpreterError, "Error has occurred. Please try again later."
)),
rate => Ok(rate.asGetApiResponse)
)

maybeValidatedParams match {
case Left(e) => BadRequest(e)
case Right(validatedParams) => {
val maybeRate = rates.get(RatesProgramProtocol.GetRatesRequest(validatedParams.from, validatedParams.to))

maybeRate.flatMap { r =>
r match {
case Left(_) =>
InternalServerError(Protocol.GetApiError(
ErrorType.InterpreterError, "Error has occurred. Please try again later."
))
case Right(rate) => Ok(rate.asGetApiResponse)
}
}
}
}
})
.merge
}

val routes: HttpRoutes[F] = Router(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,26 @@ class OneFrameInterpreter[F[_]: Applicative](oneFrameApiClient: OneFrameApiClien
}

private def getFromApi(pair: Rate.Pair): Either[Error, Rate] = {
val ratesFromClient = oneFrameApiClient.getAll()
val ratesFromApi = oneFrameApiClient.getAll()

val rateFromMap = ratesFromClient match {
case Left(_) => Left(OneFrameLookupFailed("Error with getting response from OneFrameApiClient"))
case Right(r) =>
val rateFromApi = ratesFromApi.fold(
_ => Left(OneFrameLookupFailed("Error with getting response from OneFrameApiClient")),
r => {
ratesCache.setAll(r.values.toSet)
Right(r.get(pair))
}
}
)

val rate = rateFromMap match {
case Left(e) => Left(e)
case Right(value) =>
if (value.isEmpty) {
rateFromApi.fold(
e => Left(e),
maybeRate =>
if (maybeRate.isEmpty) {
logger.error(s"Received a valid response from OneFrame API but could not find rate for ${pair}")
Left(OneFrameLookupFailed("Pair does not exist"))
} else {
Right(value.get)
Right(maybeRate.get)
}
}

return rate
)
}

}

0 comments on commit 5a54bda

Please sign in to comment.