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::ProxyLocationsListParams = support::from_json(r#"{"country":"CL","q":"bogo","limit":50,"cursor":"cursor 1"}"#);
    let error = api.client().proxy_locations().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": "proxy_location",
          "country": "country 1",
          "countryName": "countryName 1",
          "city": "city 1",
          "cityName": "cityName 1"
        }
      ],
      "nextCursor": "cursor_2"
    }"#);
    let last = support::json(r#"{
      "object": "list",
      "items": [
        {
          "object": "proxy_location",
          "country": "country 1",
          "countryName": "countryName 1",
          "city": "city 1",
          "cityName": "cityName 1"
        }
      ],
      "nextCursor": null
    }"#);
    let api = support::MockApi::start(vec![
        support::Reply::json(200, first),
        support::Reply::json(200, last),
    ])
    .await;
    let params: types::ProxyLocationsListParams = support::from_json(r#"{"country":"CL","q":"bogo","limit":50,"cursor":"cursor 1"}"#);
    let items = api.client().proxy_locations().list(params).to_vec().await.unwrap();
    support::assert_json(&items, &support::json(r#"[
      {
        "object": "proxy_location",
        "country": "country 1",
        "countryName": "countryName 1",
        "city": "city 1",
        "cityName": "cityName 1"
      },
      {
        "object": "proxy_location",
        "country": "country 1",
        "countryName": "countryName 1",
        "city": "city 1",
        "cityName": "cityName 1"
      }
    ]"#));
    let calls = api.calls().await;
    assert_eq!(calls.len(), 2);
    assert_eq!(calls[0].method, "GET");
    assert_eq!(calls[0].path, "/v1/proxy-locations");
    assert_eq!(calls[0].query, support::pairs(&[("country", "CL"), ("q", "bogo"), ("limit", "50"), ("cursor", "cursor 1")]));
    assert_eq!(calls[1].query, support::pairs(&[("country", "CL"), ("q", "bogo"), ("limit", "50"), ("cursor", "cursor_2")]));
}

#[tokio::test]
async fn list_fetches_one_page() {
    let page = support::json(r#"{
      "object": "list",
      "items": [
        {
          "object": "proxy_location",
          "country": "country 1",
          "countryName": "countryName 1",
          "city": "city 1",
          "cityName": "cityName 1"
        }
      ],
      "nextCursor": null
    }"#);
    let api = support::MockApi::start(vec![support::Reply::json(200, page.clone())]).await;
    let result = api.client().proxy_locations().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/proxy-locations");
    assert_eq!(calls[0].query, support::pairs(&[]));
}