wuapi 0.11.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::types;

/// 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.
///
/// **Any size up to 100 MB**: `POST /v1/uploads` with `mimeType` and `size` answers an `uploadUrl`. `POST` the raw bytes to it with the file's `Content-Type` (no API key: the URL is the credential, valid for 1 hour), read `storageId` from its JSON answer, and pass it to `POST /v1/uploads/{uploadId}/complete`. The TypeScript SDK does the three calls in one: `wuapi.uploads.upload(file)`.
///
/// **Up to 5 MB in one call**: `POST /v1/uploads` with `base64` answers an upload that is already `ready`.
///
/// A `ready` upload can be sent any number of times for 24 hours (`expiresAt`), to any account the key reaches; after that it answers `404`. The file of a message that was sent stays with the message (its `media.url`) until the message is deleted. An upload that was never sent is deleted with its file when it expires. Uploading costs nothing by itself; sending the file is proxy traffic like any other media.
///
/// Limits: 60 uploads per minute and 5 GB per day per organization (`429 rate_limited`).
#[derive(Clone, Debug)]
pub struct UploadsResource {
    http: HttpClient,
}

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

    /// Create an upload
    ///
    /// Start uploading a file to send. With `size`, the answer is `pending` and carries `uploadUrl`: `POST` the file's raw bytes to it (with the file's `Content-Type`, no `Authorization`), then call `POST /v1/uploads/{uploadId}/complete` with the `storageId` it answers. The bytes go straight to storage, so files up to 100 MB work. With `base64` (up to 5 MB), the file is stored now and the answer is already `ready`.
    ///
    /// The upload belongs to the key's organization, and to the project when the request is scoped to one: another organization or project answers `404` for it.
    ///
    /// Send `Idempotency-Key` so a retry answers the same upload (and the same `uploadUrl`) instead of creating another. At most 60 uploads per minute and 5 GB per day per organization.
    ///
    /// `POST /v1/uploads`
    ///
    /// 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: impl Into<types::UploadsCreateParams>) -> Request<types::Upload> {
        let params = params.into();
        let parts = RequestParts::new(Method::Post, "/v1/uploads").body(&params).keyed();
        self.http.request(parts)
    }

    /// Get an upload
    ///
    /// The upload and whether it is `ready` to send. `uploadUrl` is always `null` here. An expired upload answers `404`.
    ///
    /// `GET /v1/uploads/{uploadId}`
    ///
    /// Safe to repeat: retried on timeouts, network errors, `429` and `5xx`.
    pub fn get(&self, upload_id: &str) -> Request<types::Upload> {
        let parts = RequestParts::new(Method::Get, format!("/v1/uploads/{}", encode_path(upload_id)))
            .retryable();
        self.http.request(parts)
    }

    /// Complete an upload
    ///
    /// Tell wuapi the bytes are in: pass the `storageId` that the `POST` to `uploadUrl` answered. The file's real size is read from storage and must be the `size` the upload declared (a file of another size is deleted and the upload stays `pending`). The answer is the upload, `ready` to send for 24 hours.
    ///
    /// Safe to repeat: completing a `ready` upload with the same `storageId` answers it again. Any other `storageId`, or one that is not the file posted for this upload, answers `400`.
    ///
    /// `POST /v1/uploads/{uploadId}/complete`
    ///
    /// 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 complete(
        &self,
        upload_id: &str,
        params: types::UploadsCompleteParams,
    ) -> Request<types::Upload> {
        let parts = RequestParts::new(Method::Post, format!("/v1/uploads/{}/complete", encode_path(upload_id)))
            .body(&params)
            .keyed();
        self.http.request(parts)
    }
}