pub struct Client { /* private fields */ }
Expand description
A client for accessing the Direct Decisions API.
This struct provides methods to interact with various endpoints of the Direct Decisions API, including creating votings, voting, and fetching results. The api specification can be found at https://api.directdecisions.com/v1. All possible Error responses are described in the ApiError enum and the above documentation.
§Examples
use ddclient_rs::Client;
#[tokio::main]
async fn main() {
let client = Client::new("my-api-key".to_string());
// Use client to interact with the API...
}
Implementations§
Source§impl Client
impl Client
Sourcepub fn new(token: String) -> Self
pub fn new(token: String) -> Self
Constructs a new Client
with the given API token, and the default API URL.
The default API URL is https://api.directdecisions.com
.
If you need to use a custom API URL, use Client::builder
instead.
The default Reqwest client is created. If you need to use a custom Reqwest client,
use Client::builder
instead.
Client parses and stores received rate limit information which is updated after each request.
To access the rate limit information, use Client::get_rate
.
§Arguments
token
- The API token used for authenticating with the Direct Decisions API.
§Examples
use ddclient_rs::Client;
let client = Client::new("my-api-key".to_string());
Sourcepub fn builder(token: String) -> ClientBuilder
pub fn builder(token: String) -> ClientBuilder
Creates a new ClientBuilder
for constructing a Client
.
This method initializes a builder with the provided API token.
Additional configurations, such as a custom API URL or Reqwest client,
can be set using the builder’s methods before building the Client
.
§Examples
Basic usage:
use ddclient_rs::Client;
let client = Client::builder("my-api-key".to_string())
.build();
Advanced usage with custom configurations:
use ddclient_rs::Client;
let client = Client::builder("my-api-key".to_string())
.api_url("https://custom-api.directdecisions.com".to_string())
.build();
Sourcepub fn get_rate(&self) -> Option<Rate>
pub fn get_rate(&self) -> Option<Rate>
Retrieves the current rate limit information.
This method returns the most recent rate limit information as received
from the Direct Decisions API, if available.
If no rate limit information is available, None
is returned.
§Examples
use ddclient_rs::Client;
#[tokio::main]
async fn main() {
let client = Client::builder("my-api-key".to_string())
.build();
if let Some(rate) = client.get_rate() {
println!("Current rate limit: {:?}", rate);
} else {
println!("No rate limit information available.");
}
}
Sourcepub async fn create_voting(
&self,
choices: Vec<String>,
) -> Result<Voting, ApiError>
pub async fn create_voting( &self, choices: Vec<String>, ) -> Result<Voting, ApiError>
Creates a new voting.
Sends a POST request to the Direct Decisions API to create a new voting with the specified choices.
Returns a Result
which is Ok
containing the created Voting
if successful,
or an Err
with an ApiError
if the request fails.
§Examples
use ddclient_rs::Client;
#[tokio::main]
async fn main() {
let client = Client::builder("my-api-key".to_string()).build();
let result = client.create_voting(vec!["Option 1".into(), "Option 2".into()]).await;
// Handle result...
}
Sourcepub async fn get_voting(&self, id: &str) -> Result<Voting, ApiError>
pub async fn get_voting(&self, id: &str) -> Result<Voting, ApiError>
Retrieves a voting by its ID.
Returns a Result
which is Ok
containing the Voting
if found,
or an Err
with an ApiError
if the voting is not found or the request fails.
Sourcepub async fn delete_voting(&self, id: &str) -> Result<(), ApiError>
pub async fn delete_voting(&self, id: &str) -> Result<(), ApiError>
Deletes a voting by its ID.
Returns a Result
which is Ok
if the voting was deleted successfully,
or an Err
with an ApiError
if the voting is not found or the request fails.
Sourcepub async fn set_choice(
&self,
voting_id: &str,
choice: &str,
index: i32,
) -> Result<Vec<String>, ApiError>
pub async fn set_choice( &self, voting_id: &str, choice: &str, index: i32, ) -> Result<Vec<String>, ApiError>
Sets or updates a choice in a voting. This endpoint combines all possible modifications of the choices list elements. To add a new choice, provide its value as a string and an index where it should be placed in the list. For example, index 0 will append a new choice, while index equal to the number of choices will prepend it. For any other index number between, the choice will be inserted at that position. To remove a choice, provide the exact choice value as the string and set index to -1 value. To move an existing choice to a new position, provide the exact choice value as the string and its new position as the index.
Returns a Result
with the updated list of choices if successful,
or an Err
with an ApiError
if the request fails.
§Examples
use ddclient_rs::Client;
#[tokio::main]
async fn main() {
let client = Client::builder("my-api-key".to_string()).build();
let result = client.set_choice("voting_id", "New Choice", 0).await;
// Handle result...
}
Sourcepub async fn vote(
&self,
voting_id: &str,
voter_id: &str,
ballot: HashMap<String, i32>,
) -> Result<bool, ApiError>
pub async fn vote( &self, voting_id: &str, voter_id: &str, ballot: HashMap<String, i32>, ) -> Result<bool, ApiError>
Submits a vote on a specific voting.
Votes are submitted as a ballot, which is a map of choices to their ranks. The ranks are integers starting from 1, where 1 is the highest rank. Not all choices need to be included in the ballot.
Returns a Result
which is Ok
indicating whether the vote was a revote,
or an Err
with an ApiError
if the voting is not found or the request fails.
§Examples
use ddclient_rs::Client;
use std::collections::HashMap;
#[tokio::main]
async fn main() {
let client = Client::builder("my-api-key".to_string()).build();
let ballot = HashMap::from([
("Choice 1".to_string(), 1),
("Choice 2".to_string(), 2),
]);
let result = client.vote("voting_id", "voter_id", ballot).await;
// Handle result...
}
Sourcepub async fn unvote(
&self,
voting_id: &str,
voter_id: &str,
) -> Result<(), ApiError>
pub async fn unvote( &self, voting_id: &str, voter_id: &str, ) -> Result<(), ApiError>
Removes a voter’s ballot from a specific voting.
Sourcepub async fn get_ballot(
&self,
voting_id: &str,
voter_id: &str,
) -> Result<HashMap<String, i32>, ApiError>
pub async fn get_ballot( &self, voting_id: &str, voter_id: &str, ) -> Result<HashMap<String, i32>, ApiError>
Retrieves a ballot for a specific voting and voter. The ballot is returned as a map of choices to their ranks. The ranks are integers starting from 1, where 1 is the highest rank.
Sourcepub async fn get_voting_results(
&self,
voting_id: &str,
) -> Result<VotingResults, ApiError>
pub async fn get_voting_results( &self, voting_id: &str, ) -> Result<VotingResults, ApiError>
Retrieves the results of a specific voting. The results are returned as a list of choices with their wins, percentage, and index. It does not include the duels information.
Sourcepub async fn get_voting_results_duels(
&self,
voting_id: &str,
) -> Result<VotingResults, ApiError>
pub async fn get_voting_results_duels( &self, voting_id: &str, ) -> Result<VotingResults, ApiError>
Retrieves the results of a specific voting. The results are returned as a list of choices with their wins, percentage, and index. The results also include the duels information between choices.