Return API responses as JSON
This commit is contained in:
@@ -10,7 +10,6 @@ import fetch.csv.FileNameService
|
|||||||
import fetch.lvgmc.FetchService
|
import fetch.lvgmc.FetchService
|
||||||
import fs2.io.file.{Files, Path}
|
import fs2.io.file.{Files, Path}
|
||||||
import server.ValidateRoutes.{AggFieldList, AggKey, CityList, DateTimeRange, Granularity, ValidateDate, ValidateDateTime, ValidateInt, ValidateMonths, ValidateZonedDateTime}
|
import server.ValidateRoutes.{AggFieldList, AggKey, CityList, DateTimeRange, Granularity, ValidateDate, ValidateDateTime, ValidateInt, ValidateMonths, ValidateZonedDateTime}
|
||||||
import io.circe.{Encoder, Json, Printer}
|
|
||||||
import org.http4s._
|
import org.http4s._
|
||||||
import org.http4s.dsl.io._
|
import org.http4s.dsl.io._
|
||||||
import org.http4s.implicits._
|
import org.http4s.implicits._
|
||||||
@@ -20,7 +19,6 @@ import org.http4s.server.middleware.CORSConfig
|
|||||||
import org.http4s.server.staticcontent.FileService
|
import org.http4s.server.staticcontent.FileService
|
||||||
import org.http4s.ember.server.EmberServerBuilder
|
import org.http4s.ember.server.EmberServerBuilder
|
||||||
import io.circe.generic.auto._
|
import io.circe.generic.auto._
|
||||||
import io.circe.generic.semiauto.deriveEncoder
|
|
||||||
import io.circe.syntax._
|
import io.circe.syntax._
|
||||||
import parse.csv.Aggregate.AggregateValueImplicits.aggregateValueEncoder
|
import parse.csv.Aggregate.AggregateValueImplicits.aggregateValueEncoder
|
||||||
import parse.csv.Aggregate.userQueryEncoder
|
import parse.csv.Aggregate.userQueryEncoder
|
||||||
@@ -44,10 +42,6 @@ object Server {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class Server(postgresService: PostgresService, dataService: DataService, fetch: FetchService, log: Logger[IO]) {
|
class Server(postgresService: PostgresService, dataService: DataService, fetch: FetchService, log: Logger[IO]) {
|
||||||
implicit class JsonPrettyPrinter(json: Json) {
|
|
||||||
def pretty: String = Printer.spaces2.copy(dropNullValues = true).print(json)
|
|
||||||
}
|
|
||||||
|
|
||||||
private case class ResponseWrapper(result: Map[String, Option[Aggregate.AggregateValue]], query: UserQuery)
|
private case class ResponseWrapper(result: Map[String, Option[Aggregate.AggregateValue]], query: UserQuery)
|
||||||
private case class LatestTemperature(city: String, observedAt: String, value: Option[Double])
|
private case class LatestTemperature(city: String, observedAt: String, value: Option[Double])
|
||||||
|
|
||||||
@@ -82,7 +76,7 @@ class Server(postgresService: PostgresService, dataService: DataService, fetch:
|
|||||||
|
|
||||||
// http://0.0.0.0:8080/api/show/grib-name/harmonie_2025-02-01T1500Z_2025-02-01T180000Z.grib
|
// http://0.0.0.0:8080/api/show/grib-name/harmonie_2025-02-01T1500Z_2025-02-01T180000Z.grib
|
||||||
case GET -> Root / "show" / "grib" / fileName =>
|
case GET -> Root / "show" / "grib" / fileName =>
|
||||||
dataService.getGribStucture(fileName).flatMap(response => Ok(response.asJson.pretty))
|
dataService.getGribStucture(fileName).flatMap(response => Ok(response.asJson))
|
||||||
|
|
||||||
case GET -> Root / "grib" / "binary-chunk" / ValidateInt(binaryOffset) / ValidateInt(binaryLength) / fileName =>
|
case GET -> Root / "grib" / "binary-chunk" / ValidateInt(binaryOffset) / ValidateInt(binaryLength) / fileName =>
|
||||||
dataService.getBinaryChunk(binaryOffset, binaryLength, fileName).flatMap(buffer => Ok(buffer))
|
dataService.getBinaryChunk(binaryOffset, binaryLength, fileName).flatMap(buffer => Ok(buffer))
|
||||||
@@ -126,13 +120,13 @@ class Server(postgresService: PostgresService, dataService: DataService, fetch:
|
|||||||
|
|
||||||
postgresService.query(userQuery)
|
postgresService.query(userQuery)
|
||||||
.map(result => ResponseWrapper(result, userQuery))
|
.map(result => ResponseWrapper(result, userQuery))
|
||||||
.flatMap(responseWrapper => Ok(responseWrapper.asJson.pretty))
|
.flatMap(responseWrapper => Ok(responseWrapper.asJson))
|
||||||
|
|
||||||
// Latest observation-time temperatures for a fixed production station set.
|
// Latest observation-time temperatures for a fixed production station set.
|
||||||
case GET -> Root / "query" / "latest-temperatures" / CityList(cities) =>
|
case GET -> Root / "query" / "latest-temperatures" / CityList(cities) =>
|
||||||
postgresService.queryLatestTemperatures(cities)
|
postgresService.queryLatestTemperatures(cities)
|
||||||
.map(_.map { case (city, observedAt, value) => LatestTemperature(city, observedAt.toString, value) })
|
.map(_.map { case (city, observedAt, value) => LatestTemperature(city, observedAt.toString, value) })
|
||||||
.flatMap(result => Ok(result.asJson.pretty))
|
.flatMap(result => Ok(result.asJson))
|
||||||
|
|
||||||
// http://0.0.0.0:8080/api/query/city/Kolka/20230414_2200-20230501_1230/allFields
|
// http://0.0.0.0:8080/api/query/city/Kolka/20230414_2200-20230501_1230/allFields
|
||||||
case GET -> Root / "query" / "city" / (city: String) / DateTimeRange(from, to) / "allFields" =>
|
case GET -> Root / "query" / "city" / (city: String) / DateTimeRange(from, to) / "allFields" =>
|
||||||
@@ -141,7 +135,7 @@ class Server(postgresService: PostgresService, dataService: DataService, fetch:
|
|||||||
// http://0.0.0.0:8080/api/query/country/20230414_2200-20230501_1230/tempMax,tempMin,tempAvg,precipitation
|
// http://0.0.0.0:8080/api/query/country/20230414_2200-20230501_1230/tempMax,tempMin,tempAvg,precipitation
|
||||||
case GET -> Root / "query" / "country" / DateTimeRange(from, to) / AggFieldList(fieldList) =>
|
case GET -> Root / "query" / "country" / DateTimeRange(from, to) / AggFieldList(fieldList) =>
|
||||||
postgresService.queryCountry(from, to, fieldList)
|
postgresService.queryCountry(from, to, fieldList)
|
||||||
.flatMap(result => Ok(result.asJson.pretty))
|
.flatMap(result => Ok(result.asJson))
|
||||||
|
|
||||||
// // http://0.0.0.0:8080/api/fetch/date/20230514
|
// // http://0.0.0.0:8080/api/fetch/date/20230514
|
||||||
// case GET -> Root / "fetch" / "date" / ValidateDate(date) =>
|
// case GET -> Root / "fetch" / "date" / ValidateDate(date) =>
|
||||||
@@ -170,13 +164,13 @@ class Server(postgresService: PostgresService, dataService: DataService, fetch:
|
|||||||
// http://0.0.0.0:8080/api/show/months/202304,202305,202306
|
// http://0.0.0.0:8080/api/show/months/202304,202305,202306
|
||||||
case GET -> Root / "show" / "months" / ValidateMonths(monthList) =>
|
case GET -> Root / "show" / "months" / ValidateMonths(monthList) =>
|
||||||
postgresService.getDatesByMonths(monthList).flatMap(dates =>
|
postgresService.getDatesByMonths(monthList).flatMap(dates =>
|
||||||
Ok(dates.asJson.pretty)
|
Ok(dates.asJson)
|
||||||
)
|
)
|
||||||
|
|
||||||
// http://0.0.0.0:8080/api/show/date/20230423
|
// http://0.0.0.0:8080/api/show/date/20230423
|
||||||
case GET -> Root / "show" / "date" / ValidateDate(date) =>
|
case GET -> Root / "show" / "date" / ValidateDate(date) =>
|
||||||
postgresService.getDateFileNames(date).flatMap(fileNames =>
|
postgresService.getDateFileNames(date).flatMap(fileNames =>
|
||||||
Ok(fileNames.asJson.pretty)
|
Ok(fileNames.asJson)
|
||||||
)
|
)
|
||||||
|
|
||||||
// http://0.0.0.0:8080/api/show/datetime/20230423_1300
|
// http://0.0.0.0:8080/api/show/datetime/20230423_1300
|
||||||
|
|||||||
Reference in New Issue
Block a user