pub struct Client { /* private fields */ }Expand description
A client to make requests with.
Implementations§
Source§impl Client
impl Client
Sourcepub fn set_token(&mut self, token: impl ToString)
pub fn set_token(&mut self, token: impl ToString)
Sets the provided token to be used with http requests.
Sourcepub async fn generate_token(&self) -> Result<String>
pub async fn generate_token(&self) -> Result<String>
Generates a new OTDB token, this allows the client to not receive twice the same question.
Sourcepub fn trivia(&self) -> Request<'_, BaseResponse<Vec<Trivia>>>
pub fn trivia(&self) -> Request<'_, BaseResponse<Vec<Trivia>>>
Creates a new http request used to retrieve trivia questions, all options can be set before sending the request.
§Example
use otdb::Client;
#[tokio::main]
async fn main() {
let client = Client::new();
let mut request = client.trivia();
// We can set various request options here.
request.question_number(10);
match request.send().await {
Ok(response) => {
// Do something with the response
},
Err(error) => {
// Do something with the error
}
}
}Sourcepub fn category_details(
&self,
category: Category,
) -> Request<'_, CategoryDetails>
pub fn category_details( &self, category: Category, ) -> Request<'_, CategoryDetails>
Creates a new http request used to retrieve trivia questions, all options can be set before sending the request.
§Example
use otdb::{Category, Client};
#[tokio::main]
async fn main() {
let client = Client::new();
match client.category_details(Category::Animals).send().await {
Ok(response) => {
// Do something with the response
}
Err(error) => {
// Do something with the error
}
}
}Sourcepub fn global_details(&self) -> Request<'_, GlobalDetails>
pub fn global_details(&self) -> Request<'_, GlobalDetails>
Creates a new http request that fetches the global OTDB API details.
§Example
use otdb::Client;
#[tokio::main]
async fn main() {
let client = Client::new();
match client.global_details().send().await {
Ok(response) => {
// Do something with the response
},
Err(error) => {
// Do something with the error
}
}
}Sourcepub fn new_request<T: DeserializeOwned>(
&self,
endpoint: impl ToString,
) -> Request<'_, T>
pub fn new_request<T: DeserializeOwned>( &self, endpoint: impl ToString, ) -> Request<'_, T>
Creates a new http request with a custom endpoint and a custom return body.
§Example
use otdb::Client;
#[derive(serde::Deserialize)]
struct SuperCoolResponse {
// ...
}
#[tokio::main]
async fn main() {
let client = Client::new();
match client.new_request::<SuperCoolResponse>("<ENDPOINT>").send().await {
Ok(response) => {
// Do something with the response
},
Err(error) => {
// Do something with the error
}
}
}Sourcepub async fn reset_token(&mut self) -> Result<String>
pub async fn reset_token(&mut self) -> Result<String>
Resets the token the client has, this clears the past memory of the token, and allows the client to receive all the available questions again. If the client doesn’t have a token, this method will create one and set it.
This method returns the token used by the client or the generated one in case the client didn’t have one. However, it is NOT required to set the token again, because this operation only resets the token if it was present, it doesn’t change. In case it wasn’t present it will also be set in the client.
§Example
use otdb::Client;
#[tokio::main]
async fn main() {
let mut client = Client::new();
client.set_token(client.generate_token().await.unwrap());
client.reset_token().await.unwrap();
}