Struct Client

Source
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

Source

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

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

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.");
    }
}
Source

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...
}
Source

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.

Source

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.

Source

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...
}
Source

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...
}
Source

pub async fn unvote( &self, voting_id: &str, voter_id: &str, ) -> Result<(), ApiError>

Removes a voter’s ballot from a specific voting.

Source

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.

Source

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.

Source

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.

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> 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, 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<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
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T