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
impl Animality
Sourcepub fn new<K: AsRef<str> + Display>(key: &K) -> Self
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");
Sourcepub fn image(&self, animal: Animal) -> Result<String, RequestError>
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();
Sourcepub fn fact(&self, animal: Animal) -> Result<String, RequestError>
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();
Sourcepub async fn image_async(&self, animal: Animal) -> Result<String, RequestError>
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?;
Sourcepub async fn fact_async(&self, animal: Animal) -> Result<String, RequestError>
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?;