Client

Struct Client 

Source
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

Source

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.

Source

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(())
}
Source

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(())
}
Source

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(())
}
Source

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

Source

pub async fn call_on_update<T, Callback>( &self, callback: Callback, ) -> Result<(), ListenerError>
where T: TimedEvent + Queryable<Return = T>, for<'a, 'b> Callback: AsyncFn(&'a T, &'b T),

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(())
}
Source

pub async fn call_on_update_with_state<S, T, Callback>( &self, callback: Callback, state: S, ) -> Result<(), ListenerError>
where S: Sized + Send + Sync + Clone, T: TimedEvent + Queryable<Return = T>, for<'a, 'b> Callback: AsyncFn(S, &'a T, &'b T),

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

Source

pub async fn call_on_nested_update<T, Callback>( &self, callback: Callback, ) -> Result<(), Error>
where T: TimedEvent + Queryable<Return = Vec<T>> + PartialEq, for<'any> Callback: AsyncFn(&'any T, Change),

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(())
}
Source

pub async fn call_on_nested_update_with_state<S, T, Callback>( &self, callback: Callback, state: S, ) -> Result<(), Error>
where S: Sized + Send + Sync + Clone, T: Queryable<Return = Vec<T>> + TimedEvent + PartialEq, for<'any> Callback: AsyncFn(S, &'any T, Change),

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(())
}

Trait Implementations§

Source§

impl Clone for Client

Source§

fn clone(&self) -> Client

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Client

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Client

Source§

fn default() -> Self

Creates a new Client.

Requests are sent to https://api.warframestat.us/ by default.

Auto Trait Implementations§

§

impl Freeze for Client

§

impl !RefUnwindSafe for Client

§

impl Send for Client

§

impl Sync for Client

§

impl Unpin for Client

§

impl !UnwindSafe for Client

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more