From d49d887e04d62b543587496883941bc7b26648be Mon Sep 17 00:00:00 2001 From: Grigory Pomadchin Date: Thu, 1 Jul 2021 16:34:40 -0400 Subject: [PATCH 1/5] Add a correct content type to POST requests --- CHANGELOG.md | 2 ++ .../azavea/stac4s/api/client/SttpStacClientF.scala | 12 ++++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b421cb4c..e5bffb61 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ## [Unreleased] +### Fixed +- Add a correct content type to POST requests [#359](https://github.com/azavea/stac4s/pull/359) ## [0.6.0] - 2021-07-01 ### Added diff --git a/modules/client/shared/src/main/scala/com/azavea/stac4s/api/client/SttpStacClientF.scala b/modules/client/shared/src/main/scala/com/azavea/stac4s/api/client/SttpStacClientF.scala index c64feac9..17179dbf 100644 --- a/modules/client/shared/src/main/scala/com/azavea/stac4s/api/client/SttpStacClientF.scala +++ b/modules/client/shared/src/main/scala/com/azavea/stac4s/api/client/SttpStacClientF.scala @@ -17,7 +17,7 @@ import io.circe.{Encoder, Json, JsonObject} import monocle.Lens import sttp.client3.circe.asJson import sttp.client3.{Response, SttpBackend, UriContext, basicRequest} -import sttp.model.Uri +import sttp.model.{MediaType, Uri} case class SttpStacClientF[F[_]: MonadThrow, S: Lens[*, Option[PaginationToken]]: Encoder]( client: SttpBackend[F, Any], @@ -40,7 +40,13 @@ case class SttpStacClientF[F[_]: MonadThrow, S: Lens[*, Option[PaginationToken]] Stream .unfoldLoopEval((baseUri.addPath("search"), initialBody)) { case (link, request) => client - .send(basicRequest.body(request.noSpaces).post(link).response(asJson[Json])) + .send( + basicRequest + .post(link) + .contentType(MediaType.ApplicationJson) + .body(request.noSpaces) + .response(asJson[Json]) + ) .flatMap { response => val items = response.stacItems val next = response.nextLink.nested.map(_ -> noPaginationBody).value @@ -77,6 +83,7 @@ case class SttpStacClientF[F[_]: MonadThrow, S: Lens[*, Option[PaginationToken]] .send( basicRequest .post(baseUri.addPath("collections")) + .contentType(MediaType.ApplicationJson) .body(collection.asJson.noSpaces) .response(asJson[StacCollection]) ) @@ -109,6 +116,7 @@ case class SttpStacClientF[F[_]: MonadThrow, S: Lens[*, Option[PaginationToken]] .send( basicRequest .post(baseUri.addPath("collections", collectionId.value, "items")) + .contentType(MediaType.ApplicationJson) .body(item.asJson.noSpaces) .response(asJson[StacItem]) ) From dd11293a6fc7aa220e751b23e03e4f49e91ccb50 Mon Sep 17 00:00:00 2001 From: Grigory Pomadchin Date: Thu, 1 Jul 2021 17:13:36 -0400 Subject: [PATCH 2/5] Make SearchFilters more flexible and drop null values --- CHANGELOG.md | 4 ++-- .../com/azavea/stac4s/api/client/SearchFilters.scala | 12 ++++++------ .../com/azavea/stac4s/api/client/SearchFilters.scala | 12 ++++++------ .../azavea/stac4s/api/client/SttpStacClientF.scala | 6 +++--- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e5bffb61..033341d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,8 +4,8 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ## [Unreleased] -### Fixed -- Add a correct content type to POST requests [#359](https://github.com/azavea/stac4s/pull/359) +### Changed +- Add a correct content type to POST requests and SearchFilters adjustments [#359](https://github.com/azavea/stac4s/pull/359) ## [0.6.0] - 2021-07-01 ### Added diff --git a/modules/client/js/src/main/scala/com/azavea/stac4s/api/client/SearchFilters.scala b/modules/client/js/src/main/scala/com/azavea/stac4s/api/client/SearchFilters.scala index 0fde0605..a7b12e7a 100644 --- a/modules/client/js/src/main/scala/com/azavea/stac4s/api/client/SearchFilters.scala +++ b/modules/client/js/src/main/scala/com/azavea/stac4s/api/client/SearchFilters.scala @@ -14,10 +14,10 @@ case class SearchFilters( bbox: Option[Bbox] = None, datetime: Option[TemporalExtent] = None, intersects: Option[Geometry] = None, - collections: List[String] = Nil, - items: List[String] = Nil, + collections: Option[List[String]] = None, + items: Option[List[String]] = None, limit: Option[NonNegInt] = None, - query: Map[String, List[Query]] = Map.empty, + query: Option[Map[String, List[Query]]] = None, next: Option[PaginationToken] = None ) @@ -39,10 +39,10 @@ object SearchFilters extends ClientCodecs { bbox, datetime, intersects, - collectionsOption.getOrElse(Nil), - itemsOption.getOrElse(Nil), + collectionsOption, + itemsOption, limit, - query getOrElse Map.empty, + query, paginationToken ) } diff --git a/modules/client/jvm/src/main/scala/com/azavea/stac4s/api/client/SearchFilters.scala b/modules/client/jvm/src/main/scala/com/azavea/stac4s/api/client/SearchFilters.scala index f64088d6..0758bc28 100644 --- a/modules/client/jvm/src/main/scala/com/azavea/stac4s/api/client/SearchFilters.scala +++ b/modules/client/jvm/src/main/scala/com/azavea/stac4s/api/client/SearchFilters.scala @@ -14,10 +14,10 @@ case class SearchFilters( bbox: Option[Bbox] = None, datetime: Option[TemporalExtent] = None, intersects: Option[Geometry] = None, - collections: List[String] = Nil, - items: List[String] = Nil, + collections: Option[List[String]] = None, + items: Option[List[String]] = None, limit: Option[NonNegInt] = None, - query: Map[String, List[Query]] = Map.empty, + query: Option[Map[String, List[Query]]] = None, next: Option[PaginationToken] = None ) @@ -39,10 +39,10 @@ object SearchFilters extends ClientCodecs { bbox, datetime, intersects, - collectionsOption.getOrElse(Nil), - itemsOption.getOrElse(Nil), + collectionsOption, + itemsOption, limit, - query getOrElse Map.empty, + query, paginationToken ) } diff --git a/modules/client/shared/src/main/scala/com/azavea/stac4s/api/client/SttpStacClientF.scala b/modules/client/shared/src/main/scala/com/azavea/stac4s/api/client/SttpStacClientF.scala index 17179dbf..f0e16b30 100644 --- a/modules/client/shared/src/main/scala/com/azavea/stac4s/api/client/SttpStacClientF.scala +++ b/modules/client/shared/src/main/scala/com/azavea/stac4s/api/client/SttpStacClientF.scala @@ -44,7 +44,7 @@ case class SttpStacClientF[F[_]: MonadThrow, S: Lens[*, Option[PaginationToken]] basicRequest .post(link) .contentType(MediaType.ApplicationJson) - .body(request.noSpaces) + .body(request.deepDropNullValues.noSpaces) .response(asJson[Json]) ) .flatMap { response => @@ -84,7 +84,7 @@ case class SttpStacClientF[F[_]: MonadThrow, S: Lens[*, Option[PaginationToken]] basicRequest .post(baseUri.addPath("collections")) .contentType(MediaType.ApplicationJson) - .body(collection.asJson.noSpaces) + .body(collection.asJson.deepDropNullValues.noSpaces) .response(asJson[StacCollection]) ) .flatMap(_.body.liftTo[F]) @@ -117,7 +117,7 @@ case class SttpStacClientF[F[_]: MonadThrow, S: Lens[*, Option[PaginationToken]] basicRequest .post(baseUri.addPath("collections", collectionId.value, "items")) .contentType(MediaType.ApplicationJson) - .body(item.asJson.noSpaces) + .body(item.asJson.deepDropNullValues.noSpaces) .response(asJson[StacItem]) ) .flatMap(_.bodyETag.liftTo[F]) From 47233d567a226027cb252474a25a9d8a3c282e87 Mon Sep 17 00:00:00 2001 From: Grigory Pomadchin Date: Thu, 1 Jul 2021 18:41:39 -0400 Subject: [PATCH 3/5] dropNulls only in SearchFilters --- .../scala/com/azavea/stac4s/api/client/SttpStacClientF.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/client/shared/src/main/scala/com/azavea/stac4s/api/client/SttpStacClientF.scala b/modules/client/shared/src/main/scala/com/azavea/stac4s/api/client/SttpStacClientF.scala index f0e16b30..8234fd63 100644 --- a/modules/client/shared/src/main/scala/com/azavea/stac4s/api/client/SttpStacClientF.scala +++ b/modules/client/shared/src/main/scala/com/azavea/stac4s/api/client/SttpStacClientF.scala @@ -84,7 +84,7 @@ case class SttpStacClientF[F[_]: MonadThrow, S: Lens[*, Option[PaginationToken]] basicRequest .post(baseUri.addPath("collections")) .contentType(MediaType.ApplicationJson) - .body(collection.asJson.deepDropNullValues.noSpaces) + .body(collection.asJson.noSpaces) .response(asJson[StacCollection]) ) .flatMap(_.body.liftTo[F]) @@ -117,7 +117,7 @@ case class SttpStacClientF[F[_]: MonadThrow, S: Lens[*, Option[PaginationToken]] basicRequest .post(baseUri.addPath("collections", collectionId.value, "items")) .contentType(MediaType.ApplicationJson) - .body(item.asJson.deepDropNullValues.noSpaces) + .body(item.asJson.noSpaces) .response(asJson[StacItem]) ) .flatMap(_.bodyETag.liftTo[F]) From a8c3ee37a4a4cef1a3cef1eedb0e2414e404451e Mon Sep 17 00:00:00 2001 From: Grigory Pomadchin Date: Tue, 6 Jul 2021 11:36:16 -0400 Subject: [PATCH 4/5] Make items and collections non optional lists --- .../com/azavea/stac4s/api/client/SearchFilters.scala | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/client/js/src/main/scala/com/azavea/stac4s/api/client/SearchFilters.scala b/modules/client/js/src/main/scala/com/azavea/stac4s/api/client/SearchFilters.scala index a7b12e7a..04c48f22 100644 --- a/modules/client/js/src/main/scala/com/azavea/stac4s/api/client/SearchFilters.scala +++ b/modules/client/js/src/main/scala/com/azavea/stac4s/api/client/SearchFilters.scala @@ -14,8 +14,8 @@ case class SearchFilters( bbox: Option[Bbox] = None, datetime: Option[TemporalExtent] = None, intersects: Option[Geometry] = None, - collections: Option[List[String]] = None, - items: Option[List[String]] = None, + collections: List[String] = Nil, + items: List[String] = Nil, limit: Option[NonNegInt] = None, query: Option[Map[String, List[Query]]] = None, next: Option[PaginationToken] = None @@ -39,8 +39,8 @@ object SearchFilters extends ClientCodecs { bbox, datetime, intersects, - collectionsOption, - itemsOption, + collectionsOption getOrElse Nil, + itemsOption getOrElse Nil, limit, query, paginationToken From 69b0edaf6f2452e61c14c82824824a9e790da439 Mon Sep 17 00:00:00 2001 From: Grigory Pomadchin Date: Tue, 6 Jul 2021 11:51:50 -0400 Subject: [PATCH 5/5] Instead of changing SearchFilters definition we can change circe codecs --- .../stac4s/api/client/SearchFilters.scala | 11 ++++++----- .../stac4s/api/client/SearchFilters.scala | 19 ++++++++++--------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/modules/client/js/src/main/scala/com/azavea/stac4s/api/client/SearchFilters.scala b/modules/client/js/src/main/scala/com/azavea/stac4s/api/client/SearchFilters.scala index 04c48f22..7375aa8a 100644 --- a/modules/client/js/src/main/scala/com/azavea/stac4s/api/client/SearchFilters.scala +++ b/modules/client/js/src/main/scala/com/azavea/stac4s/api/client/SearchFilters.scala @@ -4,6 +4,7 @@ import com.azavea.stac4s.api.client.util.ClientCodecs import com.azavea.stac4s.geometry.Geometry import com.azavea.stac4s.{Bbox, TemporalExtent} +import cats.syntax.option._ import eu.timepit.refined.types.numeric.NonNegInt import io.circe._ import io.circe.refined._ @@ -17,7 +18,7 @@ case class SearchFilters( collections: List[String] = Nil, items: List[String] = Nil, limit: Option[NonNegInt] = None, - query: Option[Map[String, List[Query]]] = None, + query: Map[String, List[Query]] = Map.empty, next: Option[PaginationToken] = None ) @@ -42,7 +43,7 @@ object SearchFilters extends ClientCodecs { collectionsOption getOrElse Nil, itemsOption getOrElse Nil, limit, - query, + query getOrElse Map.empty, paginationToken ) } @@ -62,10 +63,10 @@ object SearchFilters extends ClientCodecs { filters.bbox, filters.datetime, filters.intersects, - filters.collections, - filters.items, + filters.collections.some.filter(_.nonEmpty), + filters.items.some.filter(_.nonEmpty), filters.limit, - filters.query, + filters.query.some.filter(_.nonEmpty), filters.next ) ) diff --git a/modules/client/jvm/src/main/scala/com/azavea/stac4s/api/client/SearchFilters.scala b/modules/client/jvm/src/main/scala/com/azavea/stac4s/api/client/SearchFilters.scala index 0758bc28..ce65366d 100644 --- a/modules/client/jvm/src/main/scala/com/azavea/stac4s/api/client/SearchFilters.scala +++ b/modules/client/jvm/src/main/scala/com/azavea/stac4s/api/client/SearchFilters.scala @@ -3,6 +3,7 @@ package com.azavea.stac4s.api.client import com.azavea.stac4s.api.client.util.ClientCodecs import com.azavea.stac4s.{Bbox, TemporalExtent} +import cats.syntax.option._ import eu.timepit.refined.types.numeric.NonNegInt import geotrellis.vector.{io => _, _} import io.circe._ @@ -14,10 +15,10 @@ case class SearchFilters( bbox: Option[Bbox] = None, datetime: Option[TemporalExtent] = None, intersects: Option[Geometry] = None, - collections: Option[List[String]] = None, - items: Option[List[String]] = None, + collections: List[String] = Nil, + items: List[String] = Nil, limit: Option[NonNegInt] = None, - query: Option[Map[String, List[Query]]] = None, + query: Map[String, List[Query]] = Map.empty, next: Option[PaginationToken] = None ) @@ -39,10 +40,10 @@ object SearchFilters extends ClientCodecs { bbox, datetime, intersects, - collectionsOption, - itemsOption, + collectionsOption getOrElse Nil, + itemsOption getOrElse Nil, limit, - query, + query getOrElse Map.empty, paginationToken ) } @@ -62,10 +63,10 @@ object SearchFilters extends ClientCodecs { filters.bbox, filters.datetime, filters.intersects, - filters.collections, - filters.items, + filters.collections.some.filter(_.nonEmpty), + filters.items.some.filter(_.nonEmpty), filters.limit, - filters.query, + filters.query.some.filter(_.nonEmpty), filters.next ) )