Skip to content

Commit

Permalink
docs: Mention chaining requests in README
Browse files Browse the repository at this point in the history
  • Loading branch information
caleb-leinz committed Jul 31, 2024
1 parent d4a8f46 commit e52b5b0
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
34 changes: 32 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.secret_from_env()?
.build()?;
let mut atlas_client = Client::from_config(atlas_config);
let atlas_client = Client::from_config(atlas_config);
// Query Freedom for a list of all Satellites, printing the names of the satellite which
// passed deserialization
Expand All @@ -37,12 +37,42 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
}
```

## Chaining API returns

Many of the data types exposed in this library can be navigated to through other
resources, for instance a task request object holds links to the site object the
task was scheduled at.

Rather than making a call to fetch the request, then parse the site ID, then
request the site from the ID, you can instead fetch the site directly from the
return of the request call

```rust, no_run
use freedom_api::prelude::*;
use freedom_config::Config;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let atlas_config = Config::from_env()?;
let atlas_client = Client::from_config(atlas_config);
let site_from_request: Site = atlas_client
.get_request_by_id(42)
.await?
.get_site(&atlas_client)
.await?;
Ok(())
}
```

## Api Return Type

### Async Trait

When looking at the return type of the API methods, they may appear daunting. This is mostly
resulting from async lifetimes brought about by useage of [`async_trait`](https://docs.rs/async-trait/latest/async_trait/).
resulting from async lifetimes brought about by usage of [`async_trait`](https://docs.rs/async-trait/latest/async_trait/).
Once the [async trait feature](https://blog.rust-lang.org/inside-rust/2023/05/03/stabilizing-async-fn-in-trait.html)
is release in stable rust. This complexity will be alleviated.

Expand Down
19 changes: 19 additions & 0 deletions examples/fetch_site_from_request.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use freedom_api::prelude::*;
use freedom_config::Config;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let atlas_config = Config::from_env()?;
let atlas_client = Client::from_config(atlas_config);

let site_from_request: Site = atlas_client
.get_request_by_id(42)
.await?
.into_inner()
.get_site(&atlas_client)
.await?;

println!("{:?}", site_from_request);

Ok(())
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub mod prelude {
error::{BuilderError, Error, RuntimeError},
};
pub use freedom_models::{
account::*, satellite::*, satellite_configuration::*, task::*, user::*,
account::*, satellite::*, satellite_configuration::*, site::*, task::*, user::*,
};
}

Expand Down

0 comments on commit e52b5b0

Please sign in to comment.