Animality

Struct Animality 

Source
pub struct Animality { /* private fields */ }
Expand description

The Animality HTTPS client. This struct handles every request sent and received to the Animality API.

§Examples

Basic usage:

extern crate animality;
use animality::Animality;
 
let animality = Animality::new("your user token");

Implementations§

Source§

impl Animality

Source

pub fn new<K: AsRef<str> + Display>(key: &K) -> Self

Creates the animality HTTP client. This requires a token to use.

§Examples

Basic usage:

extern crate animality;
use animality::Animality;
 
let animality = Animality::new("your user token");
Source

pub fn image(&self, animal: Animal) -> Result<String, RequestError>

Fetches a random animal image from the Animality API. This request blocks the current process. To use an async version, use Animality::image_async.

§Errors

Returns a RequestError if the client fails to initiate a request with the API, if the client receives a non-OK HTTP response, or if the client cannot parse the API response as JSON.

§Panics

This function panics if the API returns a response with no body, or if the JSON response structure from the API is invalid.

§Examples

Basic usage:

extern crate animality;
use animality::{Animality, Animal};
 
let client = Animality::new("your user token");
 
// request with the `Animal` enum
let dog_image = client.image(Animal::Dog).unwrap();
 
// request from a string (case-insensitive) 
let cat: Animal = "cat".parse().unwrap();
let cat_image = client.image(cat).unwrap();
Source

pub fn fact(&self, animal: Animal) -> Result<String, RequestError>

Fetches a random animal fact from the Animality API. This request blocks the current process. To use an async version, use Animality::fact_async.

§Errors

Returns a RequestError if the client fails to initiate a request with the API, if the client receives a non-OK HTTP response, or if the client cannot parse the API response as JSON.

§Panics

This function panics if the API returns a response with no body, or if the JSON response structure from the API is invalid.

§Examples

Basic usage:

extern crate animality;
use animality::{Animality, Animal};
 
let client = Animality::new("your user token");
 
// request with the `Animal` enum
let dog_fact = client.fact(Animal::Dog).unwrap();
 
// request from a string (case-insensitive) 
let cat: Animal = "cat".parse().unwrap();
let cat_fact = client.fact(cat).unwrap();
Source

pub async fn image_async(&self, animal: Animal) -> Result<String, RequestError>

Fetches a random animal image from the Animality API asynchronously. To use the blocking version, use Animality::image.

§Errors

Returns a RequestError if the client fails to initiate a request with the API, if the client receives a non-OK HTTP response, or if the client cannot parse the API response as JSON.

§Panics

This function panics if the API returns a response with no body, or if the JSON response structure from the API is invalid.

§Examples

Basic usage:

extern crate animality;
use animality::{Animality, Animal};
 
let client = Animality::new("your user token");
 
// request with the `Animal` enum
let dog_image = client.image_async(Animal::Dog).await?;
 
// request from a string (case-insensitive) 
let cat: Animal = "cat".parse().unwrap();
let cat_image = client.image_async(cat).await?;
Source

pub async fn fact_async(&self, animal: Animal) -> Result<String, RequestError>

Fetches a random animal fact from the Animality API asynchronously. To use the blocking version, use Animality::fact.

§Errors

Returns a RequestError if the client fails to initiate a request with the API, if the client receives a non-OK HTTP response, or if the client cannot parse the API response as JSON.

§Panics

This function panics if the API returns a response with no body, or if the JSON response structure from the API is invalid.

§Examples

Basic usage:

extern crate animality;
use animality::{Animality, Animal};
 
let client = Animality::new("your user token");
 
// request with the `Animal` enum
let dog_fact = client.fact_async(Animal::Dog).await?;
 
// request from a string (case-insensitive) 
let cat: Animal = "cat".parse().unwrap();
let cat_fact = client.fact_async(cat).await?;

Auto Trait Implementations§

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, 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.