From 9abf03805a1c1ca7f9041ba9a0550baca989b7a2 Mon Sep 17 00:00:00 2001 From: Pete Gadomski Date: Thu, 5 Dec 2024 08:22:57 -0700 Subject: [PATCH] feat: use pgstac --- Cargo.lock | 149 ++++++++++++++++---------------- Cargo.toml | 20 +++-- src/lib.rs | 200 ++++++++++++++++++++----------------------- tests/test_search.py | 2 - 4 files changed, 183 insertions(+), 188 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f8de509..5b3c533 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "addr2line" @@ -192,15 +192,15 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" [[package]] name = "bytes" -version = "1.8.0" +version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ac0150caa2ae65ca5bd83f25c7de183dea78d4d366469f148435e2acfbad0da" +checksum = "325918d6fe32f23b19878fe4b34794ae41fc19ddbe53b10571a4874d44ffd39b" [[package]] name = "cc" -version = "1.2.1" +version = "1.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd9de9f2205d5ef3fd67e685b0df337994ddd4495e2a28d185500d0e1edfea47" +checksum = "f34d93e62b03caf570cccc334cbc6c2fceca82f39211051345108adcba3eebdc" dependencies = [ "shlex", ] @@ -258,7 +258,7 @@ dependencies = [ "serde", "serde_derive", "serde_json", - "thiserror 2.0.3", + "thiserror 2.0.4", ] [[package]] @@ -489,12 +489,6 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" -[[package]] -name = "hermit-abi" -version = "0.3.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" - [[package]] name = "hmac" version = "0.12.1" @@ -678,9 +672,9 @@ dependencies = [ [[package]] name = "indexmap" -version = "2.6.0" +version = "2.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "707907fe3c25f5424cce2cb7e1cbcafee6bdbe735ca90ef77c29e84591e5b9da" +checksum = "62f822373a4fe84d4bb149bf54e584a7f4abec90e072ed49cda0edea5b95471f" dependencies = [ "equivalent", "hashbrown", @@ -700,10 +694,11 @@ checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674" [[package]] name = "js-sys" -version = "0.3.72" +version = "0.3.74" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a88f1bda2bd75b0452a14784937d796722fdebfe50df998aeb3f0b7603019a9" +checksum = "a865e038f7f6ed956f788f0d7d60c541fff74c7bd74272c5d4cf15c63743e705" dependencies = [ + "once_cell", "wasm-bindgen", ] @@ -715,9 +710,9 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" [[package]] name = "libc" -version = "0.2.164" +version = "0.2.167" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "433bfe06b8c75da9b2e3fbea6e5329ff87748f0b144ef75306e674c3f6f7c13f" +checksum = "09d6582e104315a817dff97f75133544b2e094ee22447d2acf4a74e189ba06fc" [[package]] name = "libm" @@ -789,11 +784,10 @@ dependencies = [ [[package]] name = "mio" -version = "1.0.2" +version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "80e04d1dcff3aae0704555fe5fee3bcfaf3d1fdf8a7e521d5b9d2b42acb52cec" +checksum = "2886843bf800fba2e3377cff24abf6379b4c4d5c6681eaf9ea5b0d15090450bd" dependencies = [ - "hermit-abi", "libc", "wasi", "windows-sys", @@ -907,6 +901,19 @@ dependencies = [ "phf", ] +[[package]] +name = "pgstac" +version = "0.2.2" +source = "git+https://github.com/stac-utils/stac-rs#ab577151ebe5c45bf0059d90b6c370bd54818ad7" +dependencies = [ + "serde", + "serde_json", + "stac", + "stac-api", + "thiserror 2.0.4", + "tokio-postgres", +] + [[package]] name = "pgstacrs" version = "0.1.0" @@ -914,6 +921,7 @@ dependencies = [ "bb8", "bb8-postgres", "geojson", + "pgstac", "pyo3", "pyo3-async-runtimes", "pythonize", @@ -921,7 +929,7 @@ dependencies = [ "serde_json", "stac", "stac-api", - "thiserror 2.0.3", + "thiserror 2.0.4", "tokio", "tokio-postgres", ] @@ -982,9 +990,9 @@ checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" [[package]] name = "portable-atomic" -version = "1.9.0" +version = "1.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc9c68a3f6da06753e9335d63e27f6b9754dd1920d941135b7ea8224f141adb2" +checksum = "280dc24453071f1b63954171985a0b0d30058d287960968b9b2aca264c8d4ee6" [[package]] name = "postgres-protocol" @@ -1037,9 +1045,9 @@ dependencies = [ [[package]] name = "pyo3" -version = "0.23.2" +version = "0.23.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f54b3d09cbdd1f8c20650b28e7b09e338881482f4aa908a5f61a00c98fba2690" +checksum = "e484fd2c8b4cb67ab05a318f1fd6fa8f199fcc30819f08f07d200809dba26c15" dependencies = [ "cfg-if", "indoc", @@ -1068,9 +1076,9 @@ dependencies = [ [[package]] name = "pyo3-build-config" -version = "0.23.2" +version = "0.23.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3015cf985888fe66cfb63ce0e321c603706cd541b7aec7ddd35c281390af45d8" +checksum = "dc0e0469a84f208e20044b98965e1561028180219e35352a2afaf2b942beff3b" dependencies = [ "once_cell", "target-lexicon", @@ -1078,9 +1086,9 @@ dependencies = [ [[package]] name = "pyo3-ffi" -version = "0.23.2" +version = "0.23.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6fca7cd8fd809b5ac4eefb89c1f98f7a7651d3739dfb341ca6980090f554c270" +checksum = "eb1547a7f9966f6f1a0f0227564a9945fe36b90da5a93b3933fc3dc03fae372d" dependencies = [ "libc", "pyo3-build-config", @@ -1088,9 +1096,9 @@ dependencies = [ [[package]] name = "pyo3-macros" -version = "0.23.2" +version = "0.23.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34e657fa5379a79151b6ff5328d9216a84f55dc93b17b08e7c3609a969b73aa0" +checksum = "fdb6da8ec6fa5cedd1626c886fc8749bdcbb09424a86461eb8cdf096b7c33257" dependencies = [ "proc-macro2", "pyo3-macros-backend", @@ -1100,9 +1108,9 @@ dependencies = [ [[package]] name = "pyo3-macros-backend" -version = "0.23.2" +version = "0.23.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "295548d5ffd95fd1981d2d3cf4458831b21d60af046b729b6fd143b0ba7aee2f" +checksum = "38a385202ff5a92791168b1136afae5059d3ac118457bb7bc304c197c2d33e7d" dependencies = [ "heck", "proc-macro2", @@ -1301,9 +1309,9 @@ checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" [[package]] name = "socket2" -version = "0.5.7" +version = "0.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce305eb0b4296696835b71df73eb912e0f1ffd2556a501fcede6e0c50349191c" +checksum = "c970269d99b64e60ec3bd6ad27270092a5394c4e309314b18ae3fe575695fbe8" dependencies = [ "libc", "windows-sys", @@ -1318,8 +1326,7 @@ checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" [[package]] name = "stac" version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "645204dbc1530cc11a70a8fd56cb881868f9d656acbf70ee04bae869320333f1" +source = "git+https://github.com/stac-utils/stac-rs#ab577151ebe5c45bf0059d90b6c370bd54818ad7" dependencies = [ "bytes", "chrono", @@ -1329,7 +1336,7 @@ dependencies = [ "serde_json", "stac-derive", "stac-types", - "thiserror 1.0.69", + "thiserror 2.0.4", "tracing", "url", ] @@ -1337,8 +1344,7 @@ dependencies = [ [[package]] name = "stac-api" version = "0.6.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "35caf5d6f6556e0f5b6d6f767a41f74c74cf1fec7be682cf0ecdd9d7eb056fac" +source = "git+https://github.com/stac-utils/stac-rs#ab577151ebe5c45bf0059d90b6c370bd54818ad7" dependencies = [ "chrono", "cql2", @@ -1349,7 +1355,7 @@ dependencies = [ "stac", "stac-derive", "stac-types", - "thiserror 1.0.69", + "thiserror 2.0.4", "tracing", "url", ] @@ -1357,8 +1363,7 @@ dependencies = [ [[package]] name = "stac-derive" version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d8a9a71949e33dd922de39c213647e84bf401ea4a3100eabe4cb690dba2fcd9" +source = "git+https://github.com/stac-utils/stac-rs#ab577151ebe5c45bf0059d90b6c370bd54818ad7" dependencies = [ "quote", "syn", @@ -1367,13 +1372,13 @@ dependencies = [ [[package]] name = "stac-types" version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6f411e0d07b229199353a3a42dbd008fc0728d575e6b1966bd5c51f09062119" +source = "git+https://github.com/stac-utils/stac-rs#ab577151ebe5c45bf0059d90b6c370bd54818ad7" dependencies = [ "mime", "serde", "serde_json", - "thiserror 1.0.69", + "stac-derive", + "thiserror 2.0.4", "tracing", "url", ] @@ -1397,9 +1402,9 @@ checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" [[package]] name = "syn" -version = "2.0.89" +version = "2.0.90" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44d46482f1c1c87acd84dea20c1bf5ebff4c757009ed6bf19cfd36fb10e92c4e" +checksum = "919d3b74a5dd0ccd15aeb8f93e7006bd9e14c295087c9896a110f490752bcf31" dependencies = [ "proc-macro2", "quote", @@ -1434,11 +1439,11 @@ dependencies = [ [[package]] name = "thiserror" -version = "2.0.3" +version = "2.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c006c85c7651b3cf2ada4584faa36773bd07bac24acfb39f3c431b36d7e667aa" +checksum = "2f49a1853cf82743e3b7950f77e0f4d622ca36cf4317cba00c767838bac8d490" dependencies = [ - "thiserror-impl 2.0.3", + "thiserror-impl 2.0.4", ] [[package]] @@ -1454,9 +1459,9 @@ dependencies = [ [[package]] name = "thiserror-impl" -version = "2.0.3" +version = "2.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f077553d607adc1caf65430528a576c757a71ed73944b66ebb58ef2bbd243568" +checksum = "8381894bb3efe0c4acac3ded651301ceee58a15d47c2e34885ed1908ad667061" dependencies = [ "proc-macro2", "quote", @@ -1490,9 +1495,9 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "tokio" -version = "1.41.1" +version = "1.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22cfb5bee7a6a52939ca9224d6ac897bb669134078daa8735560897f69de4d33" +checksum = "5cec9b21b0450273377fc97bd4c33a8acffc8c996c987a7c5b319a0083707551" dependencies = [ "backtrace", "bytes", @@ -1532,9 +1537,9 @@ dependencies = [ [[package]] name = "tokio-util" -version = "0.7.12" +version = "0.7.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61e7c3654c13bcd040d4a03abee2c75b1d14a37b423cf5a813ceae1cc903ec6a" +checksum = "d7fcaa8d55a2bdd6b83ace262b016eca0d79ee02818c5c1bcdf0305114081078" dependencies = [ "bytes", "futures-core", @@ -1545,9 +1550,9 @@ dependencies = [ [[package]] name = "tracing" -version = "0.1.40" +version = "0.1.41" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" +checksum = "784e0ac535deb450455cbfa28a6f0df145ea1bb7ae51b821cf5e7927fdcfbdd0" dependencies = [ "pin-project-lite", "tracing-attributes", @@ -1663,9 +1668,9 @@ checksum = "b8dad83b4f25e74f184f64c43b150b91efe7647395b42289f38e50566d82855b" [[package]] name = "wasm-bindgen" -version = "0.2.95" +version = "0.2.97" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "128d1e363af62632b8eb57219c8fd7877144af57558fb2ef0368d0087bddeb2e" +checksum = "d15e63b4482863c109d70a7b8706c1e364eb6ea449b201a76c5b89cedcec2d5c" dependencies = [ "cfg-if", "once_cell", @@ -1674,9 +1679,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.95" +version = "0.2.97" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb6dd4d3ca0ddffd1dd1c9c04f94b868c37ff5fac97c30b97cff2d74fce3a358" +checksum = "8d36ef12e3aaca16ddd3f67922bc63e48e953f126de60bd33ccc0101ef9998cd" dependencies = [ "bumpalo", "log", @@ -1689,9 +1694,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.95" +version = "0.2.97" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e79384be7f8f5a9dd5d7167216f022090cf1f9ec128e6e6a482a2cb5c5422c56" +checksum = "705440e08b42d3e4b36de7d66c944be628d579796b8090bfa3471478a2260051" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -1699,9 +1704,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.95" +version = "0.2.97" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26c6ab57572f7a24a4985830b120de1594465e5d500f24afe89e16b4e833ef68" +checksum = "98c9ae5a76e46f4deecd0f0255cc223cfa18dc9b261213b8aa0c7b36f61b3f1d" dependencies = [ "proc-macro2", "quote", @@ -1712,15 +1717,15 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.95" +version = "0.2.97" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "65fc09f10666a9f147042251e0dda9c18f166ff7de300607007e96bdebc1068d" +checksum = "6ee99da9c5ba11bd675621338ef6fa52296b76b83305e9b6e5c77d4c286d6d49" [[package]] name = "web-sys" -version = "0.3.72" +version = "0.3.74" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6488b90108c040df0fe62fa815cbdee25124641df01814dd7282749234c6112" +checksum = "a98bc3c33f0fe7e59ad7cd041b89034fa82a7c2d4365ca538dda6cdaf513863c" dependencies = [ "js-sys", "wasm-bindgen", diff --git a/Cargo.toml b/Cargo.toml index 894b441..b0cf23f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,25 +2,27 @@ name = "pgstacrs" version = "0.1.0" edition = "2021" +publish = false [lib] name = "pgstacrs" crate-type = ["cdylib"] [dependencies] +bb8 = "0.8.6" +bb8-postgres = "0.8.1" +geojson = "0.24.1" +pgstac = { version = "0.2.2", git = "https://github.com/stac-utils/stac-rs" } pyo3 = "0.23.2" -tokio-postgres = { version = "0.7.12", features = ["with-serde_json-1"] } pyo3-async-runtimes = { version = "0.23.0", features = [ "tokio", "tokio-runtime", ] } -tokio = "1.41.1" -thiserror = "2.0.3" -bb8 = "0.8.6" -bb8-postgres = "0.8.1" -serde_json = "1.0.133" pythonize = "0.23.0" -stac-api = "0.6.2" -stac = "0.11.0" serde = "1.0.215" -geojson = "0.24.1" +serde_json = "1.0.133" +stac-api = { version = "0.6.2", git = "https://github.com/stac-utils/stac-rs" } +stac = { version = "0.11.0", git = "https://github.com/stac-utils/stac-rs" } +thiserror = "2.0.4" +tokio = "1.41.1" +tokio-postgres = { version = "0.7.12", features = ["with-serde_json-1"] } diff --git a/src/lib.rs b/src/lib.rs index 1aa1529..710d2aa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,7 @@ use bb8::{Pool, RunError}; use bb8_postgres::PostgresConnectionManager; use geojson::Geometry; +use pgstac::Pgstac; use pyo3::{ create_exception, exceptions::{PyException, PyValueError}, @@ -12,7 +13,7 @@ use stac::Bbox; use stac_api::{Fields, Filter, Items, Search, Sortby}; use std::{future::Future, str::FromStr}; use thiserror::Error; -use tokio_postgres::{types::ToSql, Config, NoTls}; +use tokio_postgres::{Config, NoTls}; create_exception!(pgstacrs, PgstacError, PyException); create_exception!(pgstacrs, StacError, PyException); @@ -31,63 +32,6 @@ pub enum StringOrList { List(Vec), } -macro_rules! pgstac { - (string $client:expr,$py:expr,$function:expr) => { - let function = $function.to_string(); - $client.run($py, |pool: PgstacPool| async move { - let connection = pool.get().await?; - let query = format!("SELECT pgstac.{}()", function); - let row = connection.query_one(&query, &[]).await?; - let value: String = row.try_get(function.as_str())?; - Ok(value) - }) - }; - - (json $client:expr,$py:expr,$function:expr,$params:expr) => { - let function = $function.to_string(); - $client.run($py, |pool: PgstacPool| async move { - let param_string = (0..$params.len()) - .map(|i| format!("${}", i + 1)) - .collect::>() - .join(", "); - let query = format!("SELECT pgstac.{}({})", function, param_string); - let connection = pool.get().await?; - let row = connection.query_one(&query, &$params).await?; - let value: Value = row.try_get(function.as_str())?; - Ok(Json(value)) - }) - }; - - (option $client:expr,$py:expr,$function:expr,$params:expr) => { - let function = $function.to_string(); - $client.run($py, |pool: PgstacPool| async move { - let param_string = (0..$params.len()) - .map(|i| format!("${}", i + 1)) - .collect::>() - .join(", "); - let query = format!("SELECT pgstac.{}({})", function, param_string); - let connection = pool.get().await?; - let row = connection.query_one(&query, &$params).await?; - let value: Option = row.try_get(function.as_str())?; - Ok(value.map(Json)) - }) - }; - - (void $client:expr,$py:expr,$function:expr,$params:expr) => { - let function = $function.to_string(); - $client.run($py, |pool: PgstacPool| async move { - let param_string = (0..$params.len()) - .map(|i| format!("${}", i + 1)) - .collect::>() - .join(", "); - let query = format!("SELECT pgstac.{}({})", function, param_string); - let connection = pool.get().await?; - let _ = connection.query_one(&query, &$params).await?; - Ok(()) - }) - }; -} - #[derive(Debug, Error)] enum Error { #[error(transparent)] @@ -102,6 +46,9 @@ enum Error { #[error(transparent)] Stac(#[from] stac::Error), + #[error(transparent)] + Pgstac(#[from] pgstac::Error), + #[error(transparent)] StacApi(#[from] stac_api::Error), @@ -152,15 +99,19 @@ impl Client { } fn get_version<'a>(&self, py: Python<'a>) -> PyResult> { - pgstac! { - string self,py,"get_version" - } + self.run(py, |pool| async move { + let connection = pool.get().await?; + let value = connection.pgstac_version().await?; + Ok(value) + }) } fn get_collection<'a>(&self, py: Python<'a>, id: String) -> PyResult> { - pgstac! { - option self,py,"get_collection",[&id] - } + self.run(py, |pool| async move { + let connection = pool.get().await?; + let value = connection.collection(&id).await?; + Ok(value.map(Json)) + }) } fn create_collection<'a>( @@ -169,9 +120,11 @@ impl Client { collection: Bound<'a, PyDict>, ) -> PyResult> { let collection: Value = pythonize::depythonize(&collection)?; - pgstac! { - void self,py,"create_collection",[&collection] - } + self.run(py, |pool| async move { + let connection = pool.get().await?; + connection.add_collection(collection).await?; + Ok(()) + }) } fn update_collection<'a>( @@ -180,9 +133,11 @@ impl Client { collection: Bound<'a, PyDict>, ) -> PyResult> { let collection: Value = pythonize::depythonize(&collection)?; - pgstac! { - void self,py,"update_collection",[&collection] - } + self.run(py, |pool| async move { + let connection = pool.get().await?; + connection.update_collection(collection).await?; + Ok(()) + }) } fn upsert_collection<'a>( @@ -191,21 +146,27 @@ impl Client { collection: Bound<'a, PyDict>, ) -> PyResult> { let collection: Value = pythonize::depythonize(&collection)?; - pgstac! { - void self,py,"upsert_collection",[&collection] - } + self.run(py, |pool| async move { + let connection = pool.get().await?; + connection.upsert_collection(collection).await?; + Ok(()) + }) } fn delete_collection<'a>(&self, py: Python<'a>, id: String) -> PyResult> { - pgstac! { - void self,py,"delete_collection",[&id] - } + self.run(py, |pool| async move { + let connection = pool.get().await?; + connection.delete_collection(&id).await?; + Ok(()) + }) } fn all_collections<'a>(&self, py: Python<'a>) -> PyResult> { - pgstac! { - json self,py,"all_collections",[] as [&(dyn ToSql + Sync); 0] - } + self.run(py, |pool| async move { + let connection = pool.get().await?; + let collections = connection.collections().await?; + Ok(Json(collections.into())) + }) } #[pyo3(signature = (id, collection_id=None))] @@ -215,9 +176,11 @@ impl Client { id: String, collection_id: Option, ) -> PyResult> { - pgstac! { - option self,py,"get_item",[&Some(id.as_str()), &collection_id.as_deref()] - } + self.run(py, |pool| async move { + let connection = pool.get().await?; + let value = connection.item(&id, collection_id.as_deref()).await?; + Ok(value.map(Json)) + }) } fn create_item<'a>( @@ -226,9 +189,11 @@ impl Client { item: Bound<'a, PyDict>, ) -> PyResult> { let item: Value = pythonize::depythonize(&item)?; - pgstac! { - void self,py,"create_item",[&item] - } + self.run(py, |pool| async move { + let connection = pool.get().await?; + connection.add_item(item).await?; + Ok(()) + }) } fn create_items<'a>( @@ -237,9 +202,16 @@ impl Client { items: Bound<'a, PyList>, ) -> PyResult> { let items: Value = pythonize::depythonize(&items)?; - pgstac! { - void self,py,"create_items",[&items] - } + let items = if let Value::Array(items) = items { + items + } else { + return Err(PyValueError::new_err("items is not a list")); + }; + self.run(py, |pool| async move { + let connection = pool.get().await?; + connection.add_items(&items).await?; + Ok(()) + }) } fn update_item<'a>( @@ -248,9 +220,11 @@ impl Client { item: Bound<'a, PyDict>, ) -> PyResult> { let item: Value = pythonize::depythonize(&item)?; - pgstac! { - void self,py,"update_item",[&item] - } + self.run(py, |pool| async move { + let connection = pool.get().await?; + connection.update_item(item).await?; + Ok(()) + }) } fn upsert_item<'a>( @@ -259,9 +233,11 @@ impl Client { item: Bound<'a, PyDict>, ) -> PyResult> { let item: Value = pythonize::depythonize(&item)?; - pgstac! { - void self,py,"upsert_item",[&item] - } + self.run(py, |pool| async move { + let connection = pool.get().await?; + connection.upsert_item(item).await?; + Ok(()) + }) } fn upsert_items<'a>( @@ -270,9 +246,16 @@ impl Client { items: Bound<'a, PyList>, ) -> PyResult> { let items: Value = pythonize::depythonize(&items)?; - pgstac! { - void self,py,"upsert_items",[&items] - } + let items = if let Value::Array(items) = items { + items + } else { + return Err(PyValueError::new_err("items is not a list")); + }; + self.run(py, |pool| async move { + let connection = pool.get().await?; + connection.upsert_items(&items).await?; + Ok(()) + }) } #[pyo3(signature = (id, collection_id=None))] @@ -282,9 +265,13 @@ impl Client { id: String, collection_id: Option, ) -> PyResult> { - pgstac! { - void self,py,"delete_item",[&Some(id.as_str()), &collection_id.as_deref()] - } + self.run(py, |pool| async move { + let connection = pool.get().await?; + connection + .delete_item(&id, collection_id.as_deref()) + .await?; + Ok(()) + }) } #[pyo3(signature = (*, collections=None, ids=None, intersects=None, bbox=None, datetime=None, include=None, exclude=None, sortby=None, filter=None, query=None, limit=None))] @@ -368,10 +355,12 @@ impl Client { ids, collections, }; - let search = serde_json::to_value(search).map_err(Error::from)?; - pgstac! { - json self,py,"search",[&search] - } + self.run(py, |pool| async move { + let connection = pool.get().await?; + let page = connection.search(search).await?; + let value = serde_json::to_value(page)?; + Ok(Json(value)) + }) } } @@ -379,7 +368,7 @@ impl Client { fn run<'a, F, T>( &self, py: Python<'a>, - f: impl FnOnce(PgstacPool) -> F + Send + 'static, + f: impl FnOnce(Pool>) -> F + Send + 'static, ) -> PyResult> where F: Future> + Send, @@ -409,6 +398,7 @@ impl From for PyErr { Error::StacApi(err) => StacError::new_err(err.to_string()), Error::Geojson(err) => PyValueError::new_err(format!("geojson: {}", err)), Error::SerdeJson(err) => PyValueError::new_err(err.to_string()), + Error::Pgstac(err) => PgstacError::new_err(err.to_string()), Error::Pythonize(err) => PyValueError::new_err(err.to_string()), Error::Run(err) => PgstacError::new_err(err.to_string()), Error::TokioPostgres(err) => PgstacError::new_err(format!("postgres: {err}")), diff --git a/tests/test_search.py b/tests/test_search.py index b57c984..9184d79 100644 --- a/tests/test_search.py +++ b/tests/test_search.py @@ -20,8 +20,6 @@ async def test_empty_search(client: Client) -> None: } elif version.startswith("0.8"): assert search == { - "next": None, - "prev": None, "type": "FeatureCollection", "context": {"limit": 10, "returned": 0}, "features": [],