Skip to content

Commit d84ab2c

Browse files
committed
feat(wip): pgstac python API
1 parent ff2925d commit d84ab2c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1032
-23
lines changed

.github/workflows/python.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
build-wheels:
1818
uses: ./.github/workflows/wheels.yml
1919
with:
20-
manifest-path: python/Cargo.toml
20+
manifest-path: python/stacrs/Cargo.toml
2121

2222
sdist:
2323
runs-on: ubuntu-latest
@@ -29,7 +29,7 @@ jobs:
2929
uses: PyO3/maturin-action@v1
3030
with:
3131
command: sdist
32-
args: --out dist --manifest-path python/Cargo.toml
32+
args: --out dist --manifest-path python/stacrs/Cargo.toml
3333
- name: Upload sdist
3434
uses: actions/upload-artifact@v4
3535
with:

.markdownlint-cli2.jsonc

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
{
2-
"config": {
3-
"MD013": false
4-
},
5-
"globs": ["**/*.md"],
6-
"gitignore": true,
7-
"ignores": ["spec-examples/*/README.md", "crates/*/examples/README.md", "python/examples/README.md", "target/**/*.md"]
8-
}
2+
"config": {
3+
"MD013": false
4+
},
5+
"globs": ["**/*.md"],
6+
"gitignore": true,
7+
"ignores": [
8+
"spec-examples/*/README.md",
9+
"crates/*/examples/README.md",
10+
"python/stacrs/examples/README.md",
11+
"target/**/*.md"
12+
]
13+
}

Cargo.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ members = [
1010
"crates/pgstac",
1111
"crates/server",
1212
"crates/types",
13-
"python",
13+
"python/stacrs",
14+
"python/pgstacrs",
1415
]
1516
default-members = [
1617
"crates/api",
@@ -64,7 +65,12 @@ openssl = { version = "0.10.68", features = ["vendored"] }
6465
openssl-src = "=300.3.1" # joinked from https://github.com/iopsystems/rpc-perf/commit/705b290d2105af6f33150da04b217422c6d68701#diff-2e9d962a08321605940b5a657135052fbcef87b5e360662bb527c96d9a615542R41 to cross-compile Python
6566
parquet = { version = "52.2", default-features = false }
6667
pgstac = { version = "0.2.2", path = "crates/pgstac" }
68+
postgres = "0.19.9"
6769
pyo3 = "0.22.3"
70+
pyo3-async-runtimes = { version = "0.22.0", features = [
71+
"attributes",
72+
"tokio-runtime",
73+
] }
6874
pythonize = "0.22.0"
6975
quote = "1.0"
7076
reqwest = "0.12.8"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ stacrs.search_to("items-compressed.parquet",
3535
)
3636
```
3737

38-
See [the Python documentation](https://stac-utils.github.io/stac-rs/latest/python/) for more information.
38+
See [the Python documentation](https://stac-utils.github.io/stac-rs/latest/python/stacrs/) for more information.
3939

4040
## Command line interface
4141

crates/pgstac/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ tls = ["dep:rustls", "dep:tokio-postgres-rustls", "dep:webpki-roots"]
1616

1717
[dependencies]
1818
geojson.workspace = true
19+
postgres-types = "0.2.8"
1920
rustls = { workspace = true, features = ["ring", "std"], optional = true }
2021
serde.workspace = true
2122
serde_json.workspace = true

crates/pgstac/src/client.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,8 @@ impl<'a, C: GenericClient> Client<'a, C> {
212212
}
213213
}
214214

215-
async fn opt<T>(&self, function: &str, params: &[&(dyn ToSql + Sync)]) -> Result<Option<T>>
215+
/// Return an optional, deserializable thing.
216+
pub async fn opt<T>(&self, function: &str, params: &[&(dyn ToSql + Sync)]) -> Result<Option<T>>
216217
where
217218
T: DeserializeOwned,
218219
{
@@ -244,7 +245,8 @@ impl<'a, C: GenericClient> Client<'a, C> {
244245
serde_json::from_value(value).map_err(Error::from)
245246
}
246247

247-
async fn void(&self, function: &str, params: &[&(dyn ToSql + Sync)]) -> Result<()> {
248+
/// Run a function and return nothing if it works.
249+
pub async fn void(&self, function: &str, params: &[&(dyn ToSql + Sync)]) -> Result<()> {
248250
let _ = self.query_one(function, params).await?;
249251
Ok(())
250252
}

crates/pgstac/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
mod client;
4242
mod page;
43+
pub mod query;
4344
#[cfg(feature = "tls")]
4445
mod tls;
4546

crates/pgstac/src/query.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//! Build queries for **pgstac**.
2+
use postgres_types::ToSql;
3+
4+
/// A pgstac query.
5+
#[derive(Debug)]
6+
pub struct Query {
7+
/// The query.
8+
pub query: String,
9+
10+
/// The function that was called.
11+
pub function: String,
12+
13+
params: Vec<Box<dyn ToSql + Sync>>,
14+
}
15+
16+
/// Build the version query.
17+
pub fn version() -> Query {
18+
Query::new("get_version", Vec::new())
19+
}
20+
21+
/// Get a collection.
22+
pub fn get_collection(id: impl ToString) -> Query {
23+
Query::new("get_collection", vec![Box::new(id.to_string())])
24+
}
25+
26+
impl Query {
27+
fn new(function: &str, params: Vec<Box<(dyn ToSql + Sync)>>) -> Query {
28+
let param_string = (0..params.len())
29+
.map(|i| format!("${}", i + 1))
30+
.collect::<Vec<_>>()
31+
.join(", ");
32+
let query = format!("SELECT * from pgstac.{}({})", function, param_string);
33+
Query {
34+
query,
35+
params,
36+
function: function.to_string(),
37+
}
38+
}
39+
40+
/// Returns a vec of the references to the params.
41+
pub fn params(&self) -> Vec<&(dyn ToSql + Sync)> {
42+
self.params.iter().map(|param| param.as_ref()).collect()
43+
}
44+
}

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Command Line Interface (CLI), Rust crates, and other libraries for the [SpatioTe
66

77
## Python documentation
88

9-
Our Python package is named **stacrs** and its documentation is available [here](./python/index.md).
9+
Our Python package is named **stacrs** and its documentation is available [here](./python/stacrs/index.md).
1010

1111
## Rust documentation on docs.rs
1212

mkdocs.yml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ theme:
1818
nav:
1919
- Home: index.md
2020
- Python:
21-
- python/index.md
22-
- Example notebook: python/example.ipynb
21+
- python/stacrs/index.md
22+
- Example notebook: python/stacrs/example.ipynb
2323
- API:
24-
- python/api/index.md
25-
- migrate: python/api/migrate.md
26-
- read: python/api/read.md
27-
- search: python/api/search.md
28-
- validate: python/api/validate.md
29-
- version: python/api/version.md
30-
- write: python/api/write.md
24+
- python/stacrs/api/index.md
25+
- migrate: python/stacrs/api/migrate.md
26+
- read: python/stacrs/api/read.md
27+
- search: python/stacrs/api/search.md
28+
- validate: python/stacrs/api/validate.md
29+
- version: python/stacrs/api/version.md
30+
- write: python/stacrs/api/write.md
3131
- Command-line interface:
3232
- cli/index.md
3333

0 commit comments

Comments
 (0)