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`.

#![allow(unused_imports)]

use crate::sdk::{self, types};
use crate::support;

#[tokio::test]
async fn reports_api_errors() {
    let body = support::json(r#"{"code":"not_found","message":"No such resource.","details":{"field":"id"}}"#);
    let reply = support::Reply::json(404, body).header("x-request-id", "req_test");
    let api = support::MockApi::start(vec![reply]).await;
    let params: types::WebhookEndpointsListParams = support::from_json(r#"{"limit":50,"cursor":"cursor 1","projectId":"ext:customer_8812"}"#);
    let error = api.client().webhook_endpoints().list(params).page().await.unwrap_err();
    assert!(matches!(error, sdk::Error::Api { .. }), "{error:?}");
    assert_eq!(error.status(), Some(404));
    assert_eq!(error.code(), "not_found");
    assert_eq!(error.request_id(), Some("req_test"));
    assert_eq!(error.details().and_then(|d| d.get("field")), Some(&"id".into()));
    assert_eq!(error.to_string(), "No such resource. (404 not_found, request req_test)");
    assert_eq!(api.calls().await.len(), 1);
}

#[tokio::test]
async fn list_streams_every_page() {
    let first = support::json(r#"{
      "object": "list",
      "items": [
        {
          "object": "webhook_endpoint",
          "id": "id 1",
          "projectId": "projectId 1",
          "url": "https://example.com/url",
          "events": [
            "account.qr_code_issued"
          ],
          "active": true,
          "createdAt": "2026-09-24T09:00:00.000Z",
          "updatedAt": "2026-09-24T09:00:00.000Z"
        }
      ],
      "nextCursor": "cursor_2"
    }"#);
    let last = support::json(r#"{
      "object": "list",
      "items": [
        {
          "object": "webhook_endpoint",
          "id": "id 1",
          "projectId": "projectId 1",
          "url": "https://example.com/url",
          "events": [
            "account.qr_code_issued"
          ],
          "active": true,
          "createdAt": "2026-09-24T09:00:00.000Z",
          "updatedAt": "2026-09-24T09:00:00.000Z"
        }
      ],
      "nextCursor": null
    }"#);
    let api = support::MockApi::start(vec![
        support::Reply::json(200, first),
        support::Reply::json(200, last),
    ])
    .await;
    let params: types::WebhookEndpointsListParams = support::from_json(r#"{"limit":50,"cursor":"cursor 1","projectId":"ext:customer_8812"}"#);
    let items = api.client().webhook_endpoints().list(params).to_vec().await.unwrap();
    support::assert_json(&items, &support::json(r#"[
      {
        "object": "webhook_endpoint",
        "id": "id 1",
        "projectId": "projectId 1",
        "url": "https://example.com/url",
        "events": [
          "account.qr_code_issued"
        ],
        "active": true,
        "createdAt": "2026-09-24T09:00:00.000Z",
        "updatedAt": "2026-09-24T09:00:00.000Z"
      },
      {
        "object": "webhook_endpoint",
        "id": "id 1",
        "projectId": "projectId 1",
        "url": "https://example.com/url",
        "events": [
          "account.qr_code_issued"
        ],
        "active": true,
        "createdAt": "2026-09-24T09:00:00.000Z",
        "updatedAt": "2026-09-24T09:00:00.000Z"
      }
    ]"#));
    let calls = api.calls().await;
    assert_eq!(calls.len(), 2);
    assert_eq!(calls[0].method, "GET");
    assert_eq!(calls[0].path, "/v1/webhook-endpoints");
    assert_eq!(calls[0].query, support::pairs(&[("limit", "50"), ("projectId", "ext:customer_8812"), ("cursor", "cursor 1")]));
    assert_eq!(calls[1].query, support::pairs(&[("limit", "50"), ("projectId", "ext:customer_8812"), ("cursor", "cursor_2")]));
}

#[tokio::test]
async fn list_fetches_one_page() {
    let page = support::json(r#"{
      "object": "list",
      "items": [
        {
          "object": "webhook_endpoint",
          "id": "id 1",
          "projectId": "projectId 1",
          "url": "https://example.com/url",
          "events": [
            "account.qr_code_issued"
          ],
          "active": true,
          "createdAt": "2026-09-24T09:00:00.000Z",
          "updatedAt": "2026-09-24T09:00:00.000Z"
        }
      ],
      "nextCursor": null
    }"#);
    let api = support::MockApi::start(vec![support::Reply::json(200, page.clone())]).await;
    let result = api.client().webhook_endpoints().list(Default::default()).page().await.unwrap();
    support::assert_json(&result, &page);
    let calls = api.calls().await;
    assert_eq!(calls.len(), 1);
    assert_eq!(calls[0].method, "GET");
    assert_eq!(calls[0].path, "/v1/webhook-endpoints");
    assert_eq!(calls[0].query, support::pairs(&[]));
}

#[tokio::test]
async fn create() {
    let response = support::json(r#"{
      "object": "webhook_endpoint",
      "id": "id 1",
      "projectId": "projectId 1",
      "url": "https://example.com/url",
      "events": [
        "account.qr_code_issued"
      ],
      "active": true,
      "createdAt": "2026-09-24T09:00:00.000Z",
      "updatedAt": "2026-09-24T09:00:00.000Z"
    }"#);
    let api = support::MockApi::start(vec![support::Reply::json(201, response.clone())]).await;
    let params: types::WebhookEndpointsCreateParams = support::from_json(r#"{
      "url": "https://example.com/url",
      "events": [
        "account.qr_code_issued"
      ],
      "projectId": "projectId 1"
    }"#);
    let result = api.client().webhook_endpoints().create(params).await.unwrap();
    support::assert_json(&result, &response);
    let calls = api.calls().await;
    assert_eq!(calls.len(), 1);
    assert_eq!(calls[0].method, "POST");
    assert_eq!(calls[0].path, "/v1/webhook-endpoints");
    assert_eq!(calls[0].query, support::pairs(&[]));
    support::assert_json(&calls[0].body, &support::json(r#"{
      "url": "https://example.com/url",
      "events": [
        "account.qr_code_issued"
      ],
      "projectId": "projectId 1"
    }"#));
    assert!(calls[0].header(sdk::IDEMPOTENCY_HEADER).is_some_and(|key| !key.is_empty()));
}

#[tokio::test]
async fn create_retries_with_one_key() {
    let response = support::json(r#"{
      "object": "webhook_endpoint",
      "id": "id 1",
      "projectId": "projectId 1",
      "url": "https://example.com/url",
      "events": [
        "account.qr_code_issued"
      ],
      "active": true,
      "createdAt": "2026-09-24T09:00:00.000Z",
      "updatedAt": "2026-09-24T09:00:00.000Z"
    }"#);
    let api = support::MockApi::start(vec![support::Reply::status(503), support::Reply::json(201, response.clone())]).await;
    let params: types::WebhookEndpointsCreateParams = support::from_json(r#"{
      "url": "https://example.com/url",
      "events": [
        "account.qr_code_issued"
      ],
      "projectId": "projectId 1"
    }"#);
    let result = api.client().webhook_endpoints().create(params).await.unwrap();
    support::assert_json(&result, &response);
    let calls = api.calls().await;
    assert_eq!(calls.len(), 2);
    let key = calls[0].header(sdk::IDEMPOTENCY_HEADER).unwrap();
    assert!(!key.is_empty());
    assert_eq!(calls[1].header(sdk::IDEMPOTENCY_HEADER), Some(key));
    assert_eq!(calls[0].body, calls[1].body);
}

#[tokio::test]
async fn get() {
    let response = support::json(r#"{
      "object": "webhook_endpoint",
      "id": "id 1",
      "projectId": "projectId 1",
      "url": "https://example.com/url",
      "events": [
        "account.qr_code_issued"
      ],
      "active": true,
      "createdAt": "2026-09-24T09:00:00.000Z",
      "updatedAt": "2026-09-24T09:00:00.000Z"
    }"#);
    let api = support::MockApi::start(vec![support::Reply::json(200, response.clone())]).await;
    let result = api.client().webhook_endpoints().get("n97p31n8b5c7z2y4u6ebt5gr1ew3qa8s").await.unwrap();
    support::assert_json(&result, &response);
    let calls = api.calls().await;
    assert_eq!(calls.len(), 1);
    assert_eq!(calls[0].method, "GET");
    assert_eq!(calls[0].path, "/v1/webhook-endpoints/n97p31n8b5c7z2y4u6ebt5gr1ew3qa8s");
    assert_eq!(calls[0].query, support::pairs(&[]));
    assert!(calls[0].body.is_none());
    assert!(calls[0].header(sdk::IDEMPOTENCY_HEADER).is_none());
}

#[tokio::test]
async fn update() {
    let response = support::json(r#"{
      "object": "webhook_endpoint",
      "id": "id 1",
      "projectId": "projectId 1",
      "url": "https://example.com/url",
      "events": [
        "account.qr_code_issued"
      ],
      "active": true,
      "createdAt": "2026-09-24T09:00:00.000Z",
      "updatedAt": "2026-09-24T09:00:00.000Z"
    }"#);
    let api = support::MockApi::start(vec![support::Reply::json(200, response.clone())]).await;
    let params: types::WebhookEndpointsUpdateParams = support::from_json(r#"{"url":"https://example.com/url","events":["account.qr_code_issued"],"active":true}"#);
    let result = api.client().webhook_endpoints().update("n97p31n8b5c7z2y4u6ebt5gr1ew3qa8s", params).await.unwrap();
    support::assert_json(&result, &response);
    let calls = api.calls().await;
    assert_eq!(calls.len(), 1);
    assert_eq!(calls[0].method, "PATCH");
    assert_eq!(calls[0].path, "/v1/webhook-endpoints/n97p31n8b5c7z2y4u6ebt5gr1ew3qa8s");
    assert_eq!(calls[0].query, support::pairs(&[]));
    support::assert_json(&calls[0].body, &support::json(r#"{"url":"https://example.com/url","events":["account.qr_code_issued"],"active":true}"#));
    assert!(calls[0].header(sdk::IDEMPOTENCY_HEADER).is_none());
}

#[tokio::test]
async fn update_without_params() {
    let response = support::json(r#"{
      "object": "webhook_endpoint",
      "id": "id 1",
      "projectId": "projectId 1",
      "url": "https://example.com/url",
      "events": [
        "account.qr_code_issued"
      ],
      "active": true,
      "createdAt": "2026-09-24T09:00:00.000Z",
      "updatedAt": "2026-09-24T09:00:00.000Z"
    }"#);
    let api = support::MockApi::start(vec![support::Reply::json(200, response.clone())]).await;
    let result = api.client().webhook_endpoints().update("n97p31n8b5c7z2y4u6ebt5gr1ew3qa8s", Default::default()).await.unwrap();
    support::assert_json(&result, &response);
    let calls = api.calls().await;
    assert_eq!(calls.len(), 1);
    assert_eq!(calls[0].method, "PATCH");
    assert_eq!(calls[0].path, "/v1/webhook-endpoints/n97p31n8b5c7z2y4u6ebt5gr1ew3qa8s");
    assert_eq!(calls[0].query, support::pairs(&[]));
    support::assert_json(&calls[0].body, &support::json(r#"{}"#));
    assert!(calls[0].header(sdk::IDEMPOTENCY_HEADER).is_none());
}

#[tokio::test]
async fn update_is_not_repeated() {
    let api = support::MockApi::start(vec![
        support::Reply::status(503),
        support::Reply::status(503),
    ])
    .await;
    let params: types::WebhookEndpointsUpdateParams = support::from_json(r#"{"url":"https://example.com/url","events":["account.qr_code_issued"],"active":true}"#);
    let error = api.client().webhook_endpoints().update("n97p31n8b5c7z2y4u6ebt5gr1ew3qa8s", params).await.unwrap_err();
    assert_eq!(error.status(), Some(503));
    assert_eq!(api.calls().await.len(), 1);
}

#[tokio::test]
async fn delete() {
    let api = support::MockApi::start(vec![support::Reply::status(204)]).await;
    api.client().webhook_endpoints().delete("n97p31n8b5c7z2y4u6ebt5gr1ew3qa8s").await.unwrap();
    let calls = api.calls().await;
    assert_eq!(calls.len(), 1);
    assert_eq!(calls[0].method, "DELETE");
    assert_eq!(calls[0].path, "/v1/webhook-endpoints/n97p31n8b5c7z2y4u6ebt5gr1ew3qa8s");
    assert_eq!(calls[0].query, support::pairs(&[]));
    assert!(calls[0].body.is_none());
    assert!(calls[0].header(sdk::IDEMPOTENCY_HEADER).is_none());
}

#[tokio::test]
async fn rotate_secret() {
    let response = support::json(r#"{
      "object": "webhook_endpoint",
      "id": "id 1",
      "projectId": "projectId 1",
      "url": "https://example.com/url",
      "events": [
        "account.qr_code_issued"
      ],
      "active": true,
      "createdAt": "2026-09-24T09:00:00.000Z",
      "updatedAt": "2026-09-24T09:00:00.000Z"
    }"#);
    let api = support::MockApi::start(vec![support::Reply::json(200, response.clone())]).await;
    let result = api.client().webhook_endpoints().rotate_secret("n97p31n8b5c7z2y4u6ebt5gr1ew3qa8s").await.unwrap();
    support::assert_json(&result, &response);
    let calls = api.calls().await;
    assert_eq!(calls.len(), 1);
    assert_eq!(calls[0].method, "POST");
    assert_eq!(calls[0].path, "/v1/webhook-endpoints/n97p31n8b5c7z2y4u6ebt5gr1ew3qa8s/rotate-secret");
    assert_eq!(calls[0].query, support::pairs(&[]));
    assert!(calls[0].body.is_none());
    assert!(calls[0].header(sdk::IDEMPOTENCY_HEADER).is_some_and(|key| !key.is_empty()));
}

#[tokio::test]
async fn rotate_secret_retries_with_one_key() {
    let response = support::json(r#"{
      "object": "webhook_endpoint",
      "id": "id 1",
      "projectId": "projectId 1",
      "url": "https://example.com/url",
      "events": [
        "account.qr_code_issued"
      ],
      "active": true,
      "createdAt": "2026-09-24T09:00:00.000Z",
      "updatedAt": "2026-09-24T09:00:00.000Z"
    }"#);
    let api = support::MockApi::start(vec![support::Reply::status(503), support::Reply::json(200, response.clone())]).await;
    let result = api.client().webhook_endpoints().rotate_secret("n97p31n8b5c7z2y4u6ebt5gr1ew3qa8s").await.unwrap();
    support::assert_json(&result, &response);
    let calls = api.calls().await;
    assert_eq!(calls.len(), 2);
    let key = calls[0].header(sdk::IDEMPOTENCY_HEADER).unwrap();
    assert!(!key.is_empty());
    assert_eq!(calls[1].header(sdk::IDEMPOTENCY_HEADER), Some(key));
    assert_eq!(calls[0].body, calls[1].body);
}