delopay 0.20.0

Official Rust SDK for the DeloPay payments API
Documentation
//! The `poll` resource: check the status of an asynchronous operation.

use std::sync::Arc;

use crate::core::encoding::encode_segment;
use crate::core::error::Result;
use crate::core::models::poll::PollStatusResponse;
use crate::http::client::Inner;
use crate::http::transport::HttpMethod;

/// The `poll` resource, obtained via [`crate::Delopay::poll`].
#[derive(Debug, Clone)]
pub struct Poll {
    inner: Arc<Inner>,
}

impl Poll {
    pub(crate) fn new(inner: Arc<Inner>) -> Self {
        Self { inner }
    }

    /// Get the status of a poll, e.g. one returned in a payment's
    /// `next_action`.
    pub async fn status(&self, poll_id: &str) -> Result<PollStatusResponse> {
        let path = format!("/poll/status/{}", encode_segment(poll_id));
        self.inner
            .request(HttpMethod::Get, &path, None, &[], None)
            .await
    }
}

#[cfg(test)]
mod tests {
    use crate::http::mock::{Step, client_with};

    #[tokio::test]
    async fn status_gets_the_poll_status_path() {
        let (client, mock) =
            client_with(vec![Step::ok(r#"{"poll_id":"poll_1","status":"pending"}"#)]);
        let out = client.poll().status("poll_1").await.unwrap();
        assert_eq!(out.status, "pending");

        let seen = mock.seen.lock().unwrap();
        assert_eq!(seen[0].method, "GET");
        assert_eq!(seen[0].url, "https://api.test/poll/status/poll_1");
    }
}