Use pureconfig instead of typesafe config
This commit is contained in:
@@ -18,8 +18,8 @@ libraryDependencies ++= Seq(
|
|||||||
"org.http4s" %% "http4s-circe" % http4sVersion,
|
"org.http4s" %% "http4s-circe" % http4sVersion,
|
||||||
|
|
||||||
"ch.qos.logback" % "logback-classic" % "1.2.9",
|
"ch.qos.logback" % "logback-classic" % "1.2.9",
|
||||||
"com.typesafe" % "config" % "1.4.1",
|
|
||||||
"org.scala-lang" % "scala-reflect" % "2.13.10",
|
"org.scala-lang" % "scala-reflect" % "2.13.10",
|
||||||
|
"com.github.pureconfig" %% "pureconfig" % "0.17.4",
|
||||||
|
|
||||||
|
|
||||||
"io.circe" %% "circe-core" % circeVersion,
|
"io.circe" %% "circe-core" % circeVersion,
|
||||||
|
|||||||
@@ -6,43 +6,45 @@ import org.http4s._
|
|||||||
import org.http4s.blaze.client.BlazeClientBuilder
|
import org.http4s.blaze.client.BlazeClientBuilder
|
||||||
import org.http4s.client.Client
|
import org.http4s.client.Client
|
||||||
import org.http4s.headers.Authorization
|
import org.http4s.headers.Authorization
|
||||||
import com.typesafe.config.ConfigFactory
|
|
||||||
|
import pureconfig._
|
||||||
|
import pureconfig.generic.auto._
|
||||||
|
|
||||||
import java.time.{LocalDate, LocalDateTime}
|
import java.time.{LocalDate, LocalDateTime}
|
||||||
import scala.concurrent.ExecutionContext.global
|
import scala.concurrent.ExecutionContext.global
|
||||||
|
|
||||||
|
final case class WeatherServerConfig(
|
||||||
|
username: String,
|
||||||
|
password: String,
|
||||||
|
url: String,
|
||||||
|
)
|
||||||
|
|
||||||
object FetchService {
|
object FetchService {
|
||||||
private val config = ConfigFactory.load()
|
private val weatherServerConfig: WeatherServerConfig = ConfigSource.default.load[WeatherServerConfig] match {
|
||||||
private def getIOString(path: String): IO[String] =
|
case Right(config) => config
|
||||||
if (config.hasPath(path)) IO.pure(config.getString(path))
|
case Left(errors) => throw new RuntimeException(s"Unable to load config: $errors")
|
||||||
else IO.raiseError(new RuntimeException(s"Missing configuration: $path"))
|
}
|
||||||
|
|
||||||
private val basicCredentialsIO: IO[BasicCredentials] =
|
private val basicCredentials: BasicCredentials =
|
||||||
for {
|
BasicCredentials(weatherServerConfig.username, weatherServerConfig.password)
|
||||||
username <- getIOString("username")
|
|
||||||
password <- getIOString("password")
|
|
||||||
} yield BasicCredentials(username, password)
|
|
||||||
|
|
||||||
private val baseUrlIO: IO[Uri] = getIOString("url").map(Uri.unsafeFromString) // 20220831_1330.csv
|
private val baseUrl: IO[Uri] = IO(Uri.unsafeFromString(weatherServerConfig.url))
|
||||||
|
|
||||||
private def makeRequest(client: Client[IO], url: Uri): IO[Either[Throwable, (String, String)]] = {
|
private def makeRequest(client: Client[IO], url: Uri): IO[Either[Throwable, (String, String)]] = {
|
||||||
val fileName = url.path.toString()
|
val fileName = url.path.toString()
|
||||||
|
val request = Request[IO](Method.GET, url).withHeaders(Authorization(basicCredentials))
|
||||||
|
|
||||||
for {
|
client.expect[String](request).redeemWith(
|
||||||
basicCredentials <- basicCredentialsIO
|
|
||||||
request = Request[IO](Method.GET, url).withHeaders(Authorization(basicCredentials))
|
|
||||||
result <- client.expect[String](request).redeemWith(
|
|
||||||
error => IO(Left(error))
|
error => IO(Left(error))
|
||||||
// .flatTap(_ => IO.println(s"Request failed to url: $url with error: ${error.getMessage}")),
|
// .flatTap(_ => IO.println(s"Request failed to url: $url with error: ${error.getMessage}")),
|
||||||
,
|
,
|
||||||
fileContent => IO(Right((fileName, fileContent)))
|
fileContent => IO(Right((fileName, fileContent)))
|
||||||
// .flatTap(_ => IO.println(s"Fetched: $fileName"))
|
// .flatTap(_ => IO.println(s"Fetched: $fileName"))
|
||||||
)
|
)
|
||||||
} yield result
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private def fetchFiles(fileNames: List[String]): IO[List[Either[Throwable, (String, String)]]] = {
|
private def fetchFiles(fileNames: List[String]): IO[List[Either[Throwable, (String, String)]]] = {
|
||||||
val IOUrls = baseUrlIO.map(baseUrl => fileNames.map(baseUrl / _))
|
val IOUrls = baseUrl.map(baseUrl => fileNames.map(baseUrl / _))
|
||||||
BlazeClientBuilder[IO](global).resource.use { client =>
|
BlazeClientBuilder[IO](global).resource.use { client =>
|
||||||
IOUrls.flatMap(_.traverse(url => makeRequest(client, url)))
|
IOUrls.flatMap(_.traverse(url => makeRequest(client, url)))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ object Server {
|
|||||||
.map(Parser.queryData(UserQuery(cities, field, key), _))
|
.map(Parser.queryData(UserQuery(cities, field, key), _))
|
||||||
.flatMap(result => Ok(result.asJson.pretty))
|
.flatMap(result => Ok(result.asJson.pretty))
|
||||||
|
|
||||||
// http://0.0.0.0:8080/api/fetch/date/20230423
|
// http://0.0.0.0:8080/api/fetch/date/20230514
|
||||||
case GET -> Root / "fetch" / "date" / ValidDate(date) =>
|
case GET -> Root / "fetch" / "date" / ValidDate(date) =>
|
||||||
val result = for {
|
val result = for {
|
||||||
fetchResultEither <- FetchService.fetchFromDate(date).attempt
|
fetchResultEither <- FetchService.fetchFromDate(date).attempt
|
||||||
|
|||||||
Reference in New Issue
Block a user