forked from stac-utils/stac4s
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add a client module * Consolidate tests
- Loading branch information
Showing
22 changed files
with
874 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
modules/client/js/src/main/scala/com/azavea/stac4s/api/client/SearchFilters.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.azavea.stac4s.api.client | ||
|
||
import com.azavea.stac4s.Bbox | ||
import com.azavea.stac4s.api.client.utils.ClientCodecs | ||
import com.azavea.stac4s.geometry.Geometry | ||
import com.azavea.stac4s.types.TemporalExtent | ||
|
||
import eu.timepit.refined.types.numeric.NonNegInt | ||
import io.circe._ | ||
import io.circe.generic.semiauto._ | ||
import io.circe.refined._ | ||
|
||
case class SearchFilters( | ||
bbox: Option[Bbox] = None, | ||
datetime: Option[TemporalExtent] = None, | ||
intersects: Option[Geometry] = None, | ||
collections: List[String] = Nil, | ||
items: List[String] = Nil, | ||
limit: Option[NonNegInt] = None, | ||
query: Map[String, List[Query]] = Map.empty, | ||
next: Option[PaginationToken] = None | ||
) | ||
|
||
object SearchFilters extends ClientCodecs { | ||
|
||
implicit val searchFilterDecoder: Decoder[SearchFilters] = { c => | ||
for { | ||
bbox <- c.downField("bbox").as[Option[Bbox]] | ||
datetime <- c.downField("datetime").as[Option[TemporalExtent]] | ||
intersects <- c.downField("intersects").as[Option[Geometry]] | ||
collectionsOption <- c.downField("collections").as[Option[List[String]]] | ||
itemsOption <- c.downField("items").as[Option[List[String]]] | ||
limit <- c.downField("limit").as[Option[NonNegInt]] | ||
query <- c.get[Option[Map[String, List[Query]]]]("query") | ||
paginationToken <- c.get[Option[PaginationToken]]("next") | ||
} yield { | ||
SearchFilters( | ||
bbox, | ||
datetime, | ||
intersects, | ||
collectionsOption.getOrElse(Nil), | ||
itemsOption.getOrElse(Nil), | ||
limit, | ||
query getOrElse Map.empty, | ||
paginationToken | ||
) | ||
} | ||
} | ||
|
||
implicit val searchFilterEncoder: Encoder[SearchFilters] = deriveEncoder | ||
} |
14 changes: 14 additions & 0 deletions
14
modules/client/js/src/main/scala/com/azavea/stac4s/api/client/SttpStacClient.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.azavea.stac4s.api.client | ||
|
||
import cats.MonadError | ||
import sttp.client3.SttpBackend | ||
import sttp.model.Uri | ||
|
||
object SttpStacClient { | ||
|
||
def apply[F[_]: MonadError[*[_], Throwable]]( | ||
client: SttpBackend[F, Any], | ||
baseUri: Uri | ||
): SttpStacClient[F] = | ||
SttpStacClientF.instance[F, SearchFilters](client, baseUri) | ||
} |
5 changes: 5 additions & 0 deletions
5
modules/client/js/src/main/scala/com/azavea/stac4s/api/client/package.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.azavea.stac4s.api | ||
|
||
package object client { | ||
type SttpStacClient[F[_]] = SttpStacClientF.Aux[F, SearchFilters] | ||
} |
9 changes: 9 additions & 0 deletions
9
modules/client/js/src/test/scala/com/azavea/stac4s/api/client/SttpStacClientSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.azavea.stac4s.api.client | ||
|
||
import com.azavea.stac4s.testing.JsInstances | ||
|
||
import sttp.client3.UriContext | ||
|
||
class SttpStacClientSpec extends SttpStacClientFSpec with JsInstances { | ||
lazy val client = SttpStacClient(backend, uri"http://localhost:9090") | ||
} |
51 changes: 51 additions & 0 deletions
51
modules/client/jvm/src/main/scala/com/azavea/stac4s/api/client/SearchFilters.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.azavea.stac4s.api.client | ||
|
||
import com.azavea.stac4s.Bbox | ||
import com.azavea.stac4s.api.client.utils.ClientCodecs | ||
import com.azavea.stac4s.types.TemporalExtent | ||
|
||
import eu.timepit.refined.types.numeric.NonNegInt | ||
import geotrellis.vector.{io => _, _} | ||
import io.circe._ | ||
import io.circe.generic.semiauto._ | ||
import io.circe.refined._ | ||
|
||
case class SearchFilters( | ||
bbox: Option[Bbox] = None, | ||
datetime: Option[TemporalExtent] = None, | ||
intersects: Option[Geometry] = None, | ||
collections: List[String] = Nil, | ||
items: List[String] = Nil, | ||
limit: Option[NonNegInt] = None, | ||
query: Map[String, List[Query]] = Map.empty, | ||
next: Option[PaginationToken] = None | ||
) | ||
|
||
object SearchFilters extends ClientCodecs { | ||
|
||
implicit val searchFilterDecoder: Decoder[SearchFilters] = { c => | ||
for { | ||
bbox <- c.downField("bbox").as[Option[Bbox]] | ||
datetime <- c.downField("datetime").as[Option[TemporalExtent]] | ||
intersects <- c.downField("intersects").as[Option[Geometry]] | ||
collectionsOption <- c.downField("collections").as[Option[List[String]]] | ||
itemsOption <- c.downField("items").as[Option[List[String]]] | ||
limit <- c.downField("limit").as[Option[NonNegInt]] | ||
query <- c.get[Option[Map[String, List[Query]]]]("query") | ||
paginationToken <- c.get[Option[PaginationToken]]("next") | ||
} yield { | ||
SearchFilters( | ||
bbox, | ||
datetime, | ||
intersects, | ||
collectionsOption.getOrElse(Nil), | ||
itemsOption.getOrElse(Nil), | ||
limit, | ||
query getOrElse Map.empty, | ||
paginationToken | ||
) | ||
} | ||
} | ||
|
||
implicit val searchFilterEncoder: Encoder[SearchFilters] = deriveEncoder | ||
} |
14 changes: 14 additions & 0 deletions
14
modules/client/jvm/src/main/scala/com/azavea/stac4s/api/client/SttpStacClient.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.azavea.stac4s.api.client | ||
|
||
import cats.MonadError | ||
import sttp.client3.SttpBackend | ||
import sttp.model.Uri | ||
|
||
object SttpStacClient { | ||
|
||
def apply[F[_]: MonadError[*[_], Throwable]]( | ||
client: SttpBackend[F, Any], | ||
baseUri: Uri | ||
): SttpStacClient[F] = | ||
SttpStacClientF.instance[F, SearchFilters](client, baseUri) | ||
} |
5 changes: 5 additions & 0 deletions
5
modules/client/jvm/src/main/scala/com/azavea/stac4s/api/client/client.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.azavea.stac4s.api | ||
|
||
package object client { | ||
type SttpStacClient[F[_]] = SttpStacClientF.Aux[F, SearchFilters] | ||
} |
9 changes: 9 additions & 0 deletions
9
modules/client/jvm/src/test/scala/com/azavea/stac4s/api/client/SttpStacClientSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.azavea.stac4s.api.client | ||
|
||
import com.azavea.stac4s.testing.JvmInstances | ||
|
||
import sttp.client3.UriContext | ||
|
||
class SttpStacClientSpec extends SttpStacClientFSpec with JvmInstances { | ||
lazy val client = SttpStacClient(backend, uri"http://localhost:9090") | ||
} |
43 changes: 43 additions & 0 deletions
43
modules/client/shared/src/main/scala/com/azavea/stac4s/api/client/PaginationToken.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.azavea.stac4s.api.client | ||
|
||
import cats.syntax.either._ | ||
import eu.timepit.refined.types.numeric.PosInt | ||
import io.circe | ||
import io.circe.generic.semiauto._ | ||
import io.circe.parser.parse | ||
import io.circe.refined._ | ||
import io.circe.syntax._ | ||
import io.circe.{Decoder, Encoder} | ||
|
||
import java.time.Instant | ||
import java.util.Base64 | ||
|
||
final case class PaginationToken(timestampAtLeast: Instant, serialIdGreaterThan: PosInt) | ||
|
||
/** Circe codecs should encode token into a base64 string | ||
* https://github.com/azavea/franklin/blob/f5be8ddf48661c5bc43cbd22cb7277e961641803/application/src/main/scala/com/azavea/franklin/api/schemas/package.scala#L84-L85 | ||
*/ | ||
object PaginationToken { | ||
val b64Encoder = Base64.getEncoder | ||
val b64Decoder = Base64.getDecoder | ||
|
||
val defaultDecoder: Decoder[PaginationToken] = deriveDecoder | ||
val defaultEncoder: Encoder[PaginationToken] = deriveEncoder | ||
|
||
def encPaginationToken(token: PaginationToken): String = b64Encoder.encodeToString( | ||
token.asJson(defaultEncoder).noSpaces.getBytes | ||
) | ||
|
||
def decPaginationToken(encoded: String): Either[circe.Error, PaginationToken] = { | ||
val jsonString = new String(b64Decoder.decode(encoded)) | ||
for { | ||
js <- parse(jsonString) | ||
decoded <- js.as[PaginationToken](defaultDecoder) | ||
} yield decoded | ||
} | ||
|
||
implicit val dec: Decoder[PaginationToken] = | ||
Decoder.decodeString.emap(str => decPaginationToken(str).leftMap(_.getMessage)) | ||
|
||
implicit val enc: Encoder[PaginationToken] = { encPaginationToken(_).asJson } | ||
} |
Oops, something went wrong.