wuapi 0.10.0

Rust SDK for wuapi, a WhatsApp API for developers. Link numbers by QR or pairing code, send and receive messages, manage chats, contacts, groups and channels, split them into projects and verify webhooks.
Documentation
// Generated by wuapi-codegen from the OpenAPI spec. Do not edit: run `bun run codegen`.

use crate::http::{HttpClient, Method, Request, RequestParts, encode_path};
use crate::pagination::Paginator;
use crate::types;

/// API keys scoped to one project.
#[derive(Clone, Debug)]
pub struct ProjectsApiKeysResource {
    http: HttpClient,
}

impl ProjectsApiKeysResource {
    pub(crate) fn new(http: HttpClient) -> Self {
        Self { http }
    }

    /// List a project's API keys
    ///
    /// Every key of the project, revoked ones included. Never the key itself. Organization keys only (project keys get `403 forbidden`); the `Wuapi-Project` header does not change the result.
    ///
    /// `GET /v1/projects/{projectId}/api-keys`
    ///
    /// Safe to repeat: retried on timeouts, network errors, `429` and `5xx`.
    pub fn list(
        &self,
        project_id: &str,
        params: types::ProjectsApiKeysListParams,
    ) -> Paginator<types::ApiKeyList> {
        let parts = RequestParts::new(Method::Get, format!("/v1/projects/{}/api-keys", encode_path(project_id)))
            .query_opt("limit", params.limit.as_ref())
            .retryable();
        Paginator::new(self.http.clone(), parts, "cursor", params.cursor)
    }

    /// Create a project API key
    ///
    /// A key that reaches only this project's resources. At most 20 active keys per project. The `key` is shown only here. Organization keys only (project keys get `403 forbidden`); the `Wuapi-Project` header does not change the result.
    ///
    /// `POST /v1/projects/{projectId}/api-keys`
    ///
    /// Sends `Idempotency-Key` (a random key, or your own with `.idempotency_key(..)`), so it is retried on timeouts, network errors, `429` and `5xx` without repeating its effect.
    pub fn create(
        &self,
        project_id: &str,
        params: types::ProjectsApiKeysCreateParams,
    ) -> Request<types::ApiKey> {
        let parts = RequestParts::new(Method::Post, format!("/v1/projects/{}/api-keys", encode_path(project_id)))
            .body(&params)
            .keyed();
        self.http.request(parts)
    }

    /// Revoke a project API key
    ///
    /// The key stops working at once. Organization keys only (project keys get `403 forbidden`); the `Wuapi-Project` header does not change the result.
    ///
    /// `DELETE /v1/projects/{projectId}/api-keys/{apiKeyId}`
    ///
    /// Safe to repeat: retried on timeouts, network errors, `429` and `5xx`.
    pub fn revoke(&self, project_id: &str, api_key_id: &str) -> Request<()> {
        let parts = RequestParts::new(Method::Delete, format!("/v1/projects/{}/api-keys/{}", encode_path(project_id), encode_path(api_key_id)))
            .retryable()
            .no_content();
        self.http.request(parts)
    }
}

/// The `projects` methods.
#[derive(Clone, Debug)]
pub struct ProjectsResource {
    http: HttpClient,
}

impl ProjectsResource {
    pub(crate) fn new(http: HttpClient) -> Self {
        Self { http }
    }

    /// API keys scoped to one project.
    #[must_use]
    pub fn api_keys(&self) -> ProjectsApiKeysResource {
        ProjectsApiKeysResource::new(self.http.clone())
    }

    /// Create a project
    ///
    /// Fires `project.created`. Organization keys only (project keys get `403 forbidden`); the `Wuapi-Project` header does not change the result.
    ///
    /// `POST /v1/projects`
    ///
    /// Sends `Idempotency-Key` (a random key, or your own with `.idempotency_key(..)`), so it is retried on timeouts, network errors, `429` and `5xx` without repeating its effect.
    pub fn create(&self, params: types::ProjectsCreateParams) -> Request<types::Project> {
        let parts = RequestParts::new(Method::Post, "/v1/projects").body(&params).keyed();
        self.http.request(parts)
    }

    /// List projects
    ///
    /// Live projects, newest first. Organization keys only (project keys get `403 forbidden`); the `Wuapi-Project` header does not change the result.
    ///
    /// `GET /v1/projects`
    ///
    /// Safe to repeat: retried on timeouts, network errors, `429` and `5xx`.
    pub fn list(&self, params: types::ProjectsListParams) -> Paginator<types::ProjectList> {
        let parts = RequestParts::new(Method::Get, "/v1/projects")
            .query_opt("limit", params.limit.as_ref())
            .query_opt("externalId", params.external_id.as_ref())
            .query_opt("status", params.status.as_ref())
            .retryable();
        Paginator::new(self.http.clone(), parts, "cursor", params.cursor)
    }

    /// Get a project
    ///
    /// A project, by id or `ext:<externalId>`. Organization keys only (project keys get `403 forbidden`); the `Wuapi-Project` header does not change the result.
    ///
    /// `GET /v1/projects/{projectId}`
    ///
    /// Safe to repeat: retried on timeouts, network errors, `429` and `5xx`.
    pub fn get(&self, project_id: &str) -> Request<types::Project> {
        let parts = RequestParts::new(Method::Get, format!("/v1/projects/{}", encode_path(project_id)))
            .retryable();
        self.http.request(parts)
    }

    /// Update, suspend or resume a project
    ///
    /// `status: suspended` refuses every send and write in the project (`403 project_suspended`) while reads and inbound keep working. Fires `project.updated`. Organization keys only (project keys get `403 forbidden`); the `Wuapi-Project` header does not change the result.
    ///
    /// `PATCH /v1/projects/{projectId}`
    ///
    /// Not idempotent: retried only when the request never reached the API or was refused with `429`.
    pub fn update(
        &self,
        project_id: &str,
        params: types::ProjectsUpdateParams,
    ) -> Request<types::Project> {
        let parts = RequestParts::new(Method::Patch, format!("/v1/projects/{}", encode_path(project_id)))
            .body(&params);
        self.http.request(parts)
    }

    /// Delete a project
    ///
    /// Answers at once: from then on the project is 404, its keys answer 401 and its `externalId` can be reused. In the background its accounts are logged out and deleted, then its webhook endpoints removed, and `project.deleted` fires. Organization keys only (project keys get `403 forbidden`); the `Wuapi-Project` header does not change the result.
    ///
    /// `DELETE /v1/projects/{projectId}`
    ///
    /// Safe to repeat: retried on timeouts, network errors, `429` and `5xx`.
    pub fn delete(&self, project_id: &str) -> Request<()> {
        let parts = RequestParts::new(Method::Delete, format!("/v1/projects/{}", encode_path(project_id)))
            .retryable()
            .no_content();
        self.http.request(parts)
    }

    /// Get a project's usage
    ///
    /// The project's usage for `month`. Organization keys only (project keys get `403 forbidden`); the `Wuapi-Project` header does not change the result.
    ///
    /// `GET /v1/projects/{projectId}/usage`
    ///
    /// Safe to repeat: retried on timeouts, network errors, `429` and `5xx`.
    pub fn get_usage(
        &self,
        project_id: &str,
        params: types::ProjectsGetUsageParams,
    ) -> Request<types::ProjectUsage> {
        let parts = RequestParts::new(Method::Get, format!("/v1/projects/{}/usage", encode_path(project_id)))
            .query_opt("month", params.month.as_ref())
            .retryable();
        self.http.request(parts)
    }
}