pub struct Client { /* private fields */ }Expand description
The client that acts as a convenient way to query models.
§Example
use warframe::worldstate::{
Client,
Error,
queryable::{
Cetus,
Fissure,
},
};
#[tokio::main]
async fn main() -> Result<(), Error> {
let client = Client::default();
let cetus: Cetus = client.fetch::<Cetus>().await?;
let fissures: Vec<Fissure> = client.fetch::<Fissure>().await?;
Ok(())
}Check the queryable module for all queryable types.
Implementations§
Source§impl Client
impl Client
Sourcepub fn new(
reqwest_client: Client,
base_url: &str,
config: ClientConfig,
type_cache: Cache<(Language, TypeId), Arc<dyn Any + Send + Sync>>,
items_cache: Cache<(Language, Box<str>), Option<Item>>,
) -> Self
pub fn new( reqwest_client: Client, base_url: &str, config: ClientConfig, type_cache: Cache<(Language, TypeId), Arc<dyn Any + Send + Sync>>, items_cache: Cache<(Language, Box<str>), Option<Item>>, ) -> Self
Creates a new Client with the option to supply a custom reqwest client and a base url.
Sourcepub async fn fetch<T>(&self) -> Result<T::Return, Error>where
T: Queryable,
pub async fn fetch<T>(&self) -> Result<T::Return, Error>where
T: Queryable,
Fetches an instance of a specified model.
§Example
use warframe::worldstate::{
Client,
Error,
queryable::{
Cetus,
Fissure,
},
};
#[tokio::main]
async fn main() -> Result<(), Error> {
let client = Client::default();
let cetus: Cetus = client.fetch::<Cetus>().await?;
let fissures: Vec<Fissure> = client.fetch::<Fissure>().await?;
Ok(())
}Sourcepub async fn fetch_using_lang<T>(
&self,
language: Language,
) -> Result<T::Return, Error>where
T: Queryable,
pub async fn fetch_using_lang<T>(
&self,
language: Language,
) -> Result<T::Return, Error>where
T: Queryable,
Fetches an instance of a specified model in a supplied Language.
§Examples
use warframe::worldstate::{
Client,
Error,
Language,
queryable::{
Cetus,
Fissure,
},
};
#[tokio::main]
async fn main() -> Result<(), Error> {
let client = Client::default();
let cetus: Cetus = client.fetch_using_lang::<Cetus>(Language::ZH).await?;
let fissures: Vec<Fissure> = client.fetch_using_lang::<Fissure>(Language::ZH).await?;
Ok(())
}Sourcepub async fn query_item(&self, query: &str) -> Result<Option<Item>, Error>
pub async fn query_item(&self, query: &str) -> Result<Option<Item>, Error>
Queries an item by its name and returns the closest matching item.
§Examples
use warframe::worldstate::{
Client,
Error,
items::{
Item,
weapon::Weapon,
},
};
#[tokio::main]
async fn main() -> Result<(), Error> {
let client = Client::default();
let weapon = client.query_item("Acceltra Prime").await?;
assert!(match weapon {
Some(Item::Weapon(weapon)) => matches!(*weapon, Weapon::Rifle(_)),
_ => false,
});
Ok(())
}Sourcepub async fn query_item_using_lang(
&self,
query: &str,
language: Language,
) -> Result<Option<Item>, Error>
pub async fn query_item_using_lang( &self, query: &str, language: Language, ) -> Result<Option<Item>, Error>
Queries an item by its name and returns the closest matching item.
§Examples
use warframe::worldstate::{
Client,
Error,
Language,
items::Item,
};
#[tokio::main]
async fn main() -> Result<(), Error> {
let client = Client::default();
let nano_spores = client
.query_item_using_lang("Nanosporen", Language::DE)
.await?;
assert!(matches!(nano_spores, Some(Item::Misc(_))));
Ok(())
}Source§impl Client
impl Client
Sourcepub async fn call_on_update<T, Callback>(
&self,
callback: Callback,
) -> Result<(), ListenerError>
pub async fn call_on_update<T, Callback>( &self, callback: Callback, ) -> Result<(), ListenerError>
Asynchronous method that continuously fetches updates for a given type T and invokes a
callback function.
§Example
use std::error::Error;
use warframe::worldstate::{
Client,
queryable::Cetus,
};
async fn on_cetus_update(before: &Cetus, after: &Cetus) {
println!("BEFORE : {before:?}");
println!("AFTER : {after:?}");
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let client = Client::default();
client.call_on_update(on_cetus_update); // don't forget to start it as a bg task (or .await it)
Ok(())
}Sourcepub async fn call_on_update_with_state<S, T, Callback>(
&self,
callback: Callback,
state: S,
) -> Result<(), ListenerError>
pub async fn call_on_update_with_state<S, T, Callback>( &self, callback: Callback, state: S, ) -> Result<(), ListenerError>
Asynchronous method that calls a callback function with state on update.
§Example
use std::{error::Error, sync::Arc};
use warframe::worldstate::{Client, queryable::Cetus};
// Define some state
#[derive(Debug)]
struct MyState {
_num: i32,
_s: String,
}
async fn on_cetus_update(state: Arc<MyState>, before: &Cetus, after: &Cetus) {
println!("STATE : {state:?}");
println!("BEFORE : {before:?}");
println!("AFTER : {after:?}");
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let client = Client::default();
// Note that the state will be cloned into the handler, so Arc is preferred
let state = Arc::new(MyState {
_num: 69,
_s: "My ginormous ass".into(),
});
client
.call_on_update_with_state(on_cetus_update, state); // don't forget to start it as a bg task (or .await it)
Ok(())
}Source§impl Client
impl Client
Sourcepub async fn call_on_nested_update<T, Callback>(
&self,
callback: Callback,
) -> Result<(), Error>
pub async fn call_on_nested_update<T, Callback>( &self, callback: Callback, ) -> Result<(), Error>
Asynchronous method that calls a callback function on nested updates with a given state. Used on types that yield many data at once - such as fissures.
§Example
use std::error::Error;
use warframe::worldstate::{
Client,
Change,
queryable::Fissure,
};
/// This function will be called once a fissure updates.
/// This will send a request to the corresponding endpoint once every 30s
/// and compare the results for changes.
async fn on_fissure_update(fissure: &Fissure, change: Change) {
match change {
Change::Added => println!("Fissure ADDED : {fissure:?}"),
Change::Removed => println!("Fissure REMOVED : {fissure:?}"),
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
// initialize a client (included in the prelude)
let client = Client::default();
// Pass the function to the handler
// (will return a Future)
client.call_on_nested_update(on_fissure_update); // don't forget to start it as a bg task (or .await it)
Ok(())
}Sourcepub async fn call_on_nested_update_with_state<S, T, Callback>(
&self,
callback: Callback,
state: S,
) -> Result<(), Error>
pub async fn call_on_nested_update_with_state<S, T, Callback>( &self, callback: Callback, state: S, ) -> Result<(), Error>
Same as Client::call_on_nested_update, but with an additional provided state.
§Example
use std::{error::Error, sync::Arc};
use warframe::worldstate::{Change, Client, queryable::Fissure};
// Define some state
#[derive(Debug)]
struct MyState {
_num: i32,
_s: String,
}
async fn on_fissure_update(state: Arc<MyState>, fissure: &Fissure, change: Change) {
println!("STATE : {state:?}");
match change {
Change::Added => println!("FISSURE ADDED : {fissure:?}"),
Change::Removed => println!("FISSURE REMOVED : {fissure:?}"),
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let client = Client::default();
// Note that the state will be cloned into the handler, so Arc is preferred
let state = Arc::new(MyState {
_num: 69,
_s: "My ginormous ass".into(),
});
client
.call_on_nested_update_with_state(on_fissure_update, state); // don't forget to start it as a bg task (or .await it)
Ok(())
}