ddclient-rs 0.1.3

A Rust client library for the Direct Decisions API.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
// Copyright (c) 2023, Direct Decisions Rust client AUTHORS.
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

use crate::{
    handle_api_response, ApiError, ClientError, Rate, Voting, VotingResults, CONTENT_TYPE,
    DEFAULT_BASE_URL, USER_AGENT,
};

use reqwest::{Method, Response};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::{Arc, Mutex};

#[derive(Debug, Serialize, Deserialize)]
struct VotingRequest {
    choices: Vec<String>,
}

#[derive(Debug, Serialize, Deserialize)]
struct SetChoiceRequest {
    choice: String,
    index: i32,
}

#[derive(Debug, Serialize, Deserialize)]
struct SetChoiceResponse {
    choices: Vec<String>,
}

#[derive(Debug, Serialize, Deserialize)]
struct VoteResponse {
    revoted: bool,
}

#[derive(Debug, Serialize, Deserialize)]
struct Ballot {
    ballot: HashMap<String, i32>,
}

#[derive(Debug, Serialize, Deserialize)]
struct OkResponse {
    code: i32,
    message: String,
}

/// 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
///
/// ```no_run
/// 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...
/// }
/// ```

pub struct Client {
    token: String,
    client: reqwest::Client,
    api_url: String,
    rate: Arc<Mutex<Option<Rate>>>,
}

impl Client {
    /// 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
    ///
    /// ```no_run
    /// use ddclient_rs::Client;
    ///
    /// let client = Client::new("my-api-key".to_string());
    /// ```
    pub fn new(token: String) -> Self {
        Self::builder(token).build()
    }

    /// 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();
    /// ```
    pub fn builder(token: String) -> ClientBuilder {
        ClientBuilder::new(token)
    }

    /// 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.");
    ///     }
    /// }
    /// ```
    pub fn get_rate(&self) -> Option<Rate> {
        let rate = self.rate.lock().unwrap();
        rate.clone()
    }

    async fn request<T: serde::Serialize>(
        &self,
        method: Method,
        path: &str,
        body: Option<T>,
    ) -> Result<Response, ClientError> {
        let url = format!("{}{}", self.api_url, path);

        let mut request = self
            .client
            .request(method, url)
            .header("Authorization", format!("Bearer {}", self.token))
            .header("Accept", CONTENT_TYPE)
            .header("User-Agent", USER_AGENT);

        if let Some(b) = body {
            request = request.header("Content-Type", CONTENT_TYPE);
            request = request.json(&b);
        }

        let response = request
            .send()
            .await
            .map_err(|err| ClientError::HttpRequestError(err.without_url()));

        if let Ok(response) = &response {
            let rate_update = Rate::from_headers(response.headers());
            let mut rate = self.rate.lock().unwrap();
            *rate = rate_update;
        }

        response
    }

    /// 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
    ///
    /// ```no_run
    /// 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...
    /// }
    /// ```
    pub async fn create_voting(&self, choices: Vec<String>) -> Result<Voting, ApiError> {
        let response = self
            .request(Method::POST, "v1/votings", Some(VotingRequest { choices }))
            .await?;

        handle_api_response(response).await
    }

    /// 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.
    pub async fn get_voting(&self, id: &str) -> Result<Voting, ApiError> {
        let mut uri = "v1/votings/".to_string();
        url_escape::encode_path_to_string(id, &mut uri);

        let response = self.request::<Voting>(Method::GET, &uri, None).await?;

        handle_api_response(response).await
    }

    /// 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.
    pub async fn delete_voting(&self, id: &str) -> Result<(), ApiError> {
        let mut uri = "v1/votings/".to_string();
        url_escape::encode_path_to_string(id, &mut uri);

        let response = self
            .request::<OkResponse>(Method::DELETE, &uri, None)
            .await?;

        let _ = handle_api_response::<OkResponse>(response).await?;

        Ok(())
    }

    /// 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
    ///
    /// ```no_run
    /// 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...
    /// }
    /// ```
    pub async fn set_choice(
        &self,
        voting_id: &str,
        choice: &str,
        index: i32,
    ) -> Result<Vec<String>, ApiError> {
        let mut uri = "v1/votings/".to_string();
        url_escape::encode_path_to_string(voting_id, &mut uri);
        uri.push_str("/choices");

        let response = self
            .request(
                Method::POST,
                &uri,
                Some(SetChoiceRequest {
                    choice: choice.to_string(),
                    index,
                }),
            )
            .await?;

        let resp = handle_api_response::<SetChoiceResponse>(response).await?;

        Ok(resp.choices)
    }

    /// 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...
    /// }
    /// ```
    pub async fn vote(
        &self,
        voting_id: &str,
        voter_id: &str,
        ballot: HashMap<String, i32>,
    ) -> Result<bool, ApiError> {
        let mut uri = "v1/votings/".to_string();
        url_escape::encode_path_to_string(voting_id, &mut uri);
        uri.push_str("/ballots/");
        url_escape::encode_path_to_string(voter_id, &mut uri);

        let response = self
            .request(Method::POST, &uri, Some(Ballot { ballot }))
            .await?;

        let response = handle_api_response::<VoteResponse>(response).await?;

        Ok(response.revoted)
    }

    /// Removes a voter's ballot from a specific voting.
    pub async fn unvote(&self, voting_id: &str, voter_id: &str) -> Result<(), ApiError> {
        let mut uri = "v1/votings/".to_string();
        url_escape::encode_path_to_string(voting_id, &mut uri);
        uri.push_str("/ballots/");
        url_escape::encode_path_to_string(voter_id, &mut uri);

        let response = self
            .request::<OkResponse>(Method::DELETE, &uri, None)
            .await?;

        let _ = handle_api_response::<OkResponse>(response).await?;

        Ok(())
    }

    /// 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.
    pub async fn get_ballot(
        &self,
        voting_id: &str,
        voter_id: &str,
    ) -> Result<HashMap<String, i32>, ApiError> {
        let mut uri = "v1/votings/".to_string();
        url_escape::encode_path_to_string(voting_id, &mut uri);
        uri.push_str("/ballots/");
        url_escape::encode_path_to_string(voter_id, &mut uri);

        let response = self.request::<Ballot>(Method::GET, &uri, None).await?;

        let response = handle_api_response::<Ballot>(response).await?;

        Ok(response.ballot)
    }

    /// 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.
    pub async fn get_voting_results(&self, voting_id: &str) -> Result<VotingResults, ApiError> {
        let mut uri = "v1/votings/".to_string();
        url_escape::encode_path_to_string(voting_id, &mut uri);
        uri.push_str("/results");

        let response = self
            .request::<VotingResults>(Method::GET, &uri, None)
            .await?;

        handle_api_response(response).await
    }

    /// 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.
    pub async fn get_voting_results_duels(
        &self,
        voting_id: &str,
    ) -> Result<VotingResults, ApiError> {
        let mut uri = "v1/votings/".to_string();
        url_escape::encode_path_to_string(voting_id, &mut uri);
        uri.push_str("/results/duels");

        let response = self
            .request::<VotingResults>(Method::GET, &uri, None)
            .await?;

        handle_api_response(response).await
    }
}

/// A builder for creating an instance of `Client`.
///
/// This builder allows for configuring optional parameters for `Client`,
/// such as a custom API URL or a custom Reqwest client.
///
/// # Examples
///
/// ```
/// use ddclient_rs::{Client, ClientBuilder};
///
/// let client = Client::builder("my-api-key".to_string())
///     .api_url("https://custom-api.directdecisions.com".to_string())
///     .build();
/// ```
pub struct ClientBuilder {
    token: String,
    api_url: Option<String>,
    reqwest_client: Option<reqwest::Client>,
}

impl ClientBuilder {
    fn new(token: String) -> Self {
        ClientBuilder {
            token,
            api_url: None,
            reqwest_client: None,
        }
    }

    /// Sets a custom API URL for the `Client`.
    ///
    /// If not set, a default URL is used.
    ///
    /// # Arguments
    ///
    /// * `api_url` - A string representing the custom API URL.
    pub fn api_url(mut self, api_url: String) -> Self {
        self.api_url = Some(api_url);
        self
    }

    /// Sets a custom Reqwest client for the `Client`.
    ///
    /// If not set, a default Reqwest client is used.
    ///
    /// # Arguments
    ///
    /// * `client` - An instance of `reqwest::Client` to be used with the `Client`.
    pub fn reqwest_client(mut self, client: reqwest::Client) -> Self {
        self.reqwest_client = Some(client);
        self
    }

    /// Builds and returns a new `Client` instance.
    ///
    /// This method consumes the builder, applies URL validation and formatting,
    /// and uses the provided configurations to create a `Client`.
    /// If certain configurations are not provided, default values are used.
    ///
    /// # Panics
    ///
    /// Panics if the provided API URL is invalid.
    ///
    /// # Returns
    ///
    /// Returns a `Client` instance with the configured options.
    ///
    /// # Examples
    ///
    /// ```
    /// use ddclient_rs::Client;
    ///
    /// let client = Client::builder("my-api-key".to_string())
    ///     .api_url("https://custom-api.directdecisions.com".to_string())
    ///     .build();
    /// ```
    pub fn build(self) -> Client {
        let mut api_url = match self.api_url {
            Some(url) => {
                let _ = reqwest::Url::parse(&url).expect("Invalid API URL");
                url
            }
            None => DEFAULT_BASE_URL.to_string(),
        };

        if !api_url.ends_with('/') {
            api_url.push('/');
        }

        let client = self.reqwest_client.unwrap_or_default();

        Client {
            token: self.token,
            client,
            api_url,
            rate: Arc::new(Mutex::new(None)),
        }
    }
}