reevit 0.2.0

Official Rust SDK for the Reevit payments API
Documentation
use reqwest::Method;

use crate::{
    Client, PaginationOptions, RequestOptions, Result, RoutingRule, RoutingRuleCreateRequest,
    RoutingRuleUpdateRequest,
};

use super::path_segment;

/// Routing rule operations.
#[derive(Debug, Clone)]
pub struct RoutingRules {
    client: Client,
}

impl RoutingRules {
    pub(crate) fn new(client: Client) -> Self {
        Self { client }
    }

    /// Lists routing rules.
    pub async fn list(&self, options: &PaginationOptions) -> Result<Vec<RoutingRule>> {
        let request = self
            .client
            .request(Method::GET, "/v1/routing-rules")?
            .query(options);
        self.client.send_collection(request, "rules").await
    }

    /// Creates a routing rule.
    pub async fn create(
        &self,
        input: &RoutingRuleCreateRequest,
        options: RequestOptions,
    ) -> Result<RoutingRule> {
        let request = self
            .client
            .request(Method::POST, "/v1/routing-rules")?
            .json(input);
        self.client.send(request, options).await
    }

    /// Retrieves a routing rule by ID.
    pub async fn get(&self, id: &str) -> Result<RoutingRule> {
        let request = self.client.request(
            Method::GET,
            &format!("/v1/routing-rules/{}", path_segment(id)),
        )?;
        self.client.send(request, RequestOptions::default()).await
    }

    /// Updates a routing rule.
    pub async fn update(
        &self,
        id: &str,
        input: &RoutingRuleUpdateRequest,
        options: RequestOptions,
    ) -> Result<RoutingRule> {
        let request = self
            .client
            .request(
                Method::PATCH,
                &format!("/v1/routing-rules/{}", path_segment(id)),
            )?
            .json(input);
        self.client.send(request, options).await
    }

    /// Deletes a routing rule.
    pub async fn delete(&self, id: &str, options: RequestOptions) -> Result<()> {
        let request = self.client.request(
            Method::DELETE,
            &format!("/v1/routing-rules/{}", path_segment(id)),
        )?;
        self.client.send(request, options).await
    }
}