bridge.rs
Prima bridge pattern implementation for rust
Example
use Deserialize;
use *;
use OnceCell;
// using OnceCell we make sure that `Bridge` gets instantiated only once
To understand this example you should know:
Prima bridge pattern implementation for rust
use serde::Deserialize;
use prima_bridge::prelude::*;
use once_cell::sync::OnceCell;
#[derive(Deserialize, Debug)]
pub struct MyCustomData {
name: String
}
// using OnceCell we make sure that `Bridge` gets instantiated only once
fn bridge() -> &'static Bridge {
static BRIDGE: OnceCell<Bridge> = OnceCell::new();
BRIDGE.get_or_init(|| Bridge::new("https://swapi.dev/api".parse().unwrap()))
}
pub fn fetch_data() -> Result<MyCustomData, PrimaBridgeError> {
Request::get(bridge())
.to("people/1")
.send()?
.get_data(&[])
}
fn main() {
let data = fetch_data().expect("there was an error while fetching data");
println!("the name is {}", data.name);
}
To understand this example you should know: