wuapi 0.17.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::error::Error;
use crate::http::{ClientBuilder, HttpClient, Method, Request, RequestParts};
use crate::types;
use crate::resources::accounts::AccountsResource;
use crate::resources::bots::BotsResource;
use crate::resources::branding::BrandingResource;
use crate::resources::calls::CallsResource;
use crate::resources::channels::ChannelsResource;
use crate::resources::chats::ChatsResource;
use crate::resources::contacts::ContactsResource;
use crate::resources::events::EventsResource;
use crate::resources::favorite_stickers::FavoriteStickersResource;
use crate::resources::groups::GroupsResource;
use crate::resources::incoming_webhooks::IncomingWebhooksResource;
use crate::resources::invitations::InvitationsResource;
use crate::resources::labels::LabelsResource;
use crate::resources::messages::MessagesResource;
use crate::resources::orders::OrdersResource;
use crate::resources::privacy::PrivacyResource;
use crate::resources::profile::ProfileResource;
use crate::resources::projects::ProjectsResource;
use crate::resources::proxy_locations::ProxyLocationsResource;
use crate::resources::sticker_packs::StickerPacksResource;
use crate::resources::stories::StoriesResource;
use crate::resources::uploads::UploadsResource;
use crate::resources::usage::UsageResource;
use crate::resources::webhook_endpoints::WebhookEndpointsResource;

/// The wuapi API client. Secure, fast and scalable WhatsApp API for developers.
///
/// Cheap to clone: clones share one connection pool.
#[derive(Clone, Debug)]
pub struct Wuapi {
    http: HttpClient,
}

impl From<HttpClient> for Wuapi {
    fn from(http: HttpClient) -> Self {
        Self { http }
    }
}

impl Wuapi {
    /// A client with this API key and every default: the production URL, a
    /// 30 s timeout per attempt and 2 retries.
    ///
    /// # Errors
    ///
    /// [`Error::Config`] when the key is blank.
    pub fn new(api_key: impl Into<String>) -> Result<Self, Error> {
        ClientBuilder::new().api_key(api_key).build()
    }

    /// A client that reads its API key from `WUAPI_API_KEY`.
    ///
    /// # Errors
    ///
    /// [`Error::Config`] when the variable is not set.
    pub fn from_env() -> Result<Self, Error> {
        ClientBuilder::new().build()
    }

    /// Configures a client: base URL, timeout, retry policy, project.
    #[must_use]
    pub fn builder() -> ClientBuilder {
        ClientBuilder::new()
    }

    /// The low-level client, to call an endpoint this SDK does not cover.
    #[must_use]
    pub fn http(&self) -> &HttpClient {
        &self.http
    }

    /// The API base URL.
    #[must_use]
    pub fn base_url(&self) -> &str {
        self.http.base_url()
    }

    /// The project this client is scoped to (`Wuapi-Project`), if any.
    #[must_use]
    pub fn project(&self) -> Option<&str> {
        self.http.project()
    }

    /// A client scoped to one project (its id or `ext:<externalId>`), sharing this
    /// client's key and configuration. Every request carries `Wuapi-Project`.
    ///
    /// # Errors
    ///
    /// [`Error::Config`] when `project` is blank or longer than 200 characters.
    pub fn with_project(&self, project: &str) -> Result<Self, Error> {
        self.http.with_project(project).map(Self::from)
    }

    /// An account is one WhatsApp number linked as a device, by QR code or by pairing code. Create one, show `qrCodeUrl` to the phone owner or have them type `pairingCode`, and wait for `ready`. Routes under `/v1/accounts/{accountId}` that talk to WhatsApp need the account `ready` (otherwise `409 account_not_ready`).
    #[must_use]
    pub fn accounts(&self) -> AccountsResource {
        AccountsResource::new(self.http.clone())
    }

    /// The countries and cities an account's residential proxy can be placed in: the values `proxyLocation` accepts on `POST /v1/accounts`.
    #[must_use]
    pub fn proxy_locations(&self) -> ProxyLocationsResource {
        ProxyLocationsResource::new(self.http.clone())
    }

    /// The `messages` methods.
    #[must_use]
    pub fn messages(&self) -> MessagesResource {
        MessagesResource::new(self.http.clone())
    }

    /// WhatsApp Status posts.
    #[must_use]
    pub fn stories(&self) -> StoriesResource {
        StoriesResource::new(self.http.clone())
    }

    /// Send a file you have, not a URL: a local file, a pasted image, a recorded voice note. Create an upload, get its bytes to wuapi, then send it with `media: { uploadId }` on `POST /v1/messages` or a story.
    #[must_use]
    pub fn uploads(&self) -> UploadsResource {
        UploadsResource::new(self.http.clone())
    }

    /// The `chats` methods.
    #[must_use]
    pub fn chats(&self) -> ChatsResource {
        ChatsResource::new(self.http.clone())
    }

    /// The `labels` methods.
    #[must_use]
    pub fn labels(&self) -> LabelsResource {
        LabelsResource::new(self.http.clone())
    }

    /// The `contacts` methods.
    #[must_use]
    pub fn contacts(&self) -> ContactsResource {
        ContactsResource::new(self.http.clone())
    }

    /// The `bots` methods.
    #[must_use]
    pub fn bots(&self) -> BotsResource {
        BotsResource::new(self.http.clone())
    }

    /// The linked number's own About text, display name and picture.
    #[must_use]
    pub fn profile(&self) -> ProfileResource {
        ProfileResource::new(self.http.clone())
    }

    /// The linked number's privacy settings.
    #[must_use]
    pub fn privacy(&self) -> PrivacyResource {
        PrivacyResource::new(self.http.clone())
    }

    /// Reject incoming calls. Automatic rejection is a setting on the account (`PATCH /v1/accounts/{accountId}`).
    #[must_use]
    pub fn calls(&self) -> CallsResource {
        CallsResource::new(self.http.clone())
    }

    /// WhatsApp sticker packs, read live from WhatsApp.
    #[must_use]
    pub fn sticker_packs(&self) -> StickerPacksResource {
        StickerPacksResource::new(self.http.clone())
    }

    /// The account's favorite stickers: the star tab of WhatsApp's sticker picker, which WhatsApp keeps in sync between the phone and its linked devices.
    #[must_use]
    pub fn favorite_stickers(&self) -> FavoriteStickersResource {
        FavoriteStickersResource::new(self.http.clone())
    }

    /// Catalog orders contacts send as messages, read live from WhatsApp.
    #[must_use]
    pub fn orders(&self) -> OrdersResource {
        OrdersResource::new(self.http.clone())
    }

    /// The `groups` methods.
    #[must_use]
    pub fn groups(&self) -> GroupsResource {
        GroupsResource::new(self.http.clone())
    }

    /// WhatsApp channels: follow, read, react. Post with `POST /v1/messages` and `to` set to the channel id.
    #[must_use]
    pub fn channels(&self) -> ChannelsResource {
        ChannelsResource::new(self.http.clone())
    }

    /// wuapi sends events to your webhook endpoints as `POST` requests with a JSON body (schema `Event`):
    #[must_use]
    pub fn webhook_endpoints(&self) -> WebhookEndpointsResource {
        WebhookEndpointsResource::new(self.http.clone())
    }

    /// A secret URL that sends a WhatsApp message from one of your accounts to one fixed contact or group: `POST` a body to it, no API key. Manage them here; the URL in each object is the credential.
    #[must_use]
    pub fn incoming_webhooks(&self) -> IncomingWebhooksResource {
        IncomingWebhooksResource::new(self.http.clone())
    }

    /// The `projects` methods.
    #[must_use]
    pub fn projects(&self) -> ProjectsResource {
        ProjectsResource::new(self.http.clone())
    }

    /// A hosted page you send to someone else, your customer, a store manager or a sales rep, so they link their own WhatsApp into one of your projects without an account, a dashboard or an API key. The page is public at `https://wuapi.dev/invite/{token}`, `noindex`, and shows nothing about your other projects, accounts or invitations.
    #[must_use]
    pub fn invitations(&self) -> InvitationsResource {
        InvitationsResource::new(self.http.clone())
    }

    /// What the invitation page and email show: your name, logo, accent color, support link, and whether the wuapi footer appears (hiding it needs the White label add-on).
    #[must_use]
    pub fn branding(&self) -> BrandingResource {
        BrandingResource::new(self.http.clone())
    }

    /// Billable usage, and the per-project export for rebilling.
    #[must_use]
    pub fn usage(&self) -> UsageResource {
        UsageResource::new(self.http.clone())
    }

    /// Streams: your events over one connection your code opens, the same envelope a webhook carries. No public endpoint needed.
    #[must_use]
    pub fn events(&self) -> EventsResource {
        EventsResource::new(self.http.clone())
    }

    /// Get the current key
    ///
    /// The organization, the API key and, when the request is scoped to a project, that project.
    ///
    /// `GET /v1/me`
    ///
    /// Safe to repeat: retried on timeouts, network errors, `429` and `5xx`.
    pub fn me(&self) -> Request<types::AuthContext> {
        let parts = RequestParts::new(Method::Get, "/v1/me").retryable();
        self.http.request(parts)
    }
}