Skip to content

Commit

Permalink
refactor(pgstac): return json, not stac (#550)
Browse files Browse the repository at this point in the history
This makes it easier to use in upstream Python bindings
  • Loading branch information
gadomski authored Dec 3, 2024
1 parent 2b97d76 commit 3f5bb3b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 27 deletions.
4 changes: 4 additions & 0 deletions crates/pgstac/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## [Unreleased]

### Changed

- Return JSON, not STAC ([#550](https://github.com/stac-utils/stac-rs/pull/550))

## [0.2.2] - 2024-11-12

Bump dependencies.
Expand Down
31 changes: 9 additions & 22 deletions crates/pgstac/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::JsonValue;
use crate::{Error, Page, Result};
use serde::de::DeserializeOwned;
use stac::{Collection, Item};
Expand Down Expand Up @@ -108,12 +109,12 @@ impl<'a, C: GenericClient> Client<'a, C> {
}

/// Fetches all collections.
pub async fn collections(&self) -> Result<Vec<Collection>> {
pub async fn collections(&self) -> Result<Vec<JsonValue>> {
self.vec("all_collections", &[]).await
}

/// Fetches a collection by id.
pub async fn collection(&self, id: &str) -> Result<Option<Collection>> {
pub async fn collection(&self, id: &str) -> Result<Option<JsonValue>> {
self.opt("get_collection", &[&id]).await
}

Expand Down Expand Up @@ -141,7 +142,7 @@ impl<'a, C: GenericClient> Client<'a, C> {
}

/// Fetches an item.
pub async fn item(&self, id: &str, collection: &str) -> Result<Option<Item>> {
pub async fn item(&self, id: &str, collection: &str) -> Result<Option<JsonValue>> {
self.opt("get_item", &[&id, &collection]).await
}

Expand Down Expand Up @@ -314,13 +315,7 @@ pub(crate) mod tests {
collection.title = Some("a title".to_string());
client.upsert_collection(collection).await.unwrap();
assert_eq!(
client
.collection("an-id")
.await
.unwrap()
.unwrap()
.title
.unwrap(),
client.collection("an-id").await.unwrap().unwrap()["title"],
"a title"
);
}
Expand All @@ -334,19 +329,13 @@ pub(crate) mod tests {
.await
.unwrap()
.unwrap()
.title
.get("title")
.is_none());
collection.title = Some("a title".to_string());
client.update_collection(collection).await.unwrap();
assert_eq!(client.collections().await.unwrap().len(), 1);
assert_eq!(
client
.collection("an-id")
.await
.unwrap()
.unwrap()
.title
.unwrap(),
client.collection("an-id").await.unwrap().unwrap()["title"],
"a title"
);
}
Expand Down Expand Up @@ -397,7 +386,7 @@ pub(crate) mod tests {
.await
.unwrap()
.unwrap(),
item
serde_json::to_value(item).unwrap(),
);
}

Expand All @@ -424,9 +413,7 @@ pub(crate) mod tests {
.item("an-id", "collection-id")
.await
.unwrap()
.unwrap()
.properties
.additional_fields["foo"],
.unwrap()["properties"]["foo"],
"bar"
);
}
Expand Down
3 changes: 3 additions & 0 deletions crates/pgstac/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,6 @@ pub enum Error {

/// Crate-specific result type.
pub type Result<T> = std::result::Result<T, Error>;

/// A [serde_json::Value].
pub type JsonValue = serde_json::Value;
19 changes: 14 additions & 5 deletions crates/server/src/backend/pgstac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,21 @@ where
async fn collection(&self, id: &str) -> Result<Option<Collection>> {
let client = self.pool.get().await?;
let client = Client::new(&*client);
client.collection(id).await.map_err(Error::from)
let value = client.collection(id).await?;
value
.map(serde_json::from_value)
.transpose()
.map_err(Error::from)
}

async fn collections(&self) -> Result<Vec<Collection>> {
let client = self.pool.get().await?;
let client = Client::new(&*client);
client.collections().await.map_err(Error::from)
let values = client.collections().await?;
values
.into_iter()
.map(|v| serde_json::from_value(v).map_err(Error::from))
.collect()
}

async fn add_item(&mut self, item: Item) -> Result<()> {
Expand All @@ -133,9 +141,10 @@ where
async fn item(&self, collection_id: &str, item_id: &str) -> Result<Option<Item>> {
let client = self.pool.get().await?;
let client = Client::new(&*client);
client
.item(item_id, collection_id)
.await
let value = client.item(item_id, collection_id).await?;
value
.map(serde_json::from_value)
.transpose()
.map_err(Error::from)
}

Expand Down

0 comments on commit 3f5bb3b

Please sign in to comment.