Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(sdk): type batch cancel as the cancel response it returns

Yudhi Armyndharis58be74e34d72
diff --git a/sdk/go/batch_cancel_test.go b/sdk/go/batch_cancel_test.go
new file mode 100644
index 000000000..3aa587e2e
--- /dev/null
+++ b/sdk/go/batch_cancel_test.go
@@ -0,0 +1,24 @@
+package openwa
+
+import (
+ "context"
+ "testing"
+)
+
+// The cancel route answers the batch state without per-recipient results, so CancelBatch returns
+// its own type rather than a BatchStatusResponse whose Results would always read empty.
+func TestCancelBatchDecodesCancelResponse(t *testing.T) {
+ rt := &recordTransport{
+ status: 200,
+ body: `{"batchId":"b1","status":"cancelled","progress":{"total":2,"sent":1,"failed":0,"pending":0,"cancelled":1}}`,
+ }
+ c := newTestClient(t, rt)
+ var res *BatchCancelResponse
+ res, err := c.Messages.CancelBatch(context.Background(), "s1", "b1")
+ if err != nil {
+ t.Fatal(err)
+ }
+ if res.BatchID != "b1" || res.Status != "cancelled" || res.Progress.Cancelled != 1 {
+ t.Errorf("CancelBatch = %+v", res)
+ }
+}
diff --git a/sdk/go/messages.go b/sdk/go/messages.go
index 3751555fd..f9823239c 100644
--- a/sdk/go/messages.go
+++ b/sdk/go/messages.go
@@ -212,9 +212,10 @@ func (s *MessagesService) BatchStatus(ctx context.Context, sessionID, batchID st
return &out, nil
}
-// CancelBatch cancels a running batch. Requires an OPERATOR-level key.
-func (s *MessagesService) CancelBatch(ctx context.Context, sessionID, batchID string) (*BatchStatusResponse, error) {
- var out BatchStatusResponse
+// CancelBatch cancels a running batch. Requires an OPERATOR-level key. The reply
+// carries no per-recipient results; call BatchStatus for those.
+func (s *MessagesService) CancelBatch(ctx context.Context, sessionID, batchID string) (*BatchCancelResponse, error) {
+ var out BatchCancelResponse
path := s.base(sessionID) + "/batch/" + pathEscape(batchID) + "/cancel"
err := s.client.do(ctx, "POST", path, nil, nil, &out)
if err != nil {
diff --git a/sdk/go/types_message.go b/sdk/go/types_message.go
index 85088d206..bd2c15615 100644
--- a/sdk/go/types_message.go
+++ b/sdk/go/types_message.go
@@ -520,7 +520,7 @@ type BatchProgress struct {
Cancelled int `json:"cancelled"`
}
-// BatchStatusResponse is the response from the batch status / cancel endpoints.
+// BatchStatusResponse is the response from the batch status endpoint.
type BatchStatusResponse struct {
BatchID string `json:"batchId"`
Status BatchLifecycleStatus `json:"status"`
@@ -530,6 +530,14 @@ type BatchStatusResponse struct {
CompletedAt *string `json:"completedAt,omitempty"`
}
+// BatchCancelResponse is the response from the batch cancel endpoint: the batch
+// state without the per-recipient results.
+type BatchCancelResponse struct {
+ BatchID string `json:"batchId"`
+ Status BatchLifecycleStatus `json:"status"`
+ Progress BatchProgress `json:"progress"`
+}
+
// MessageMedia is a message's stored media: the raw bytes plus the served
// content type. Non-JSON on the wire, like StatusMedia.
type MessageMedia struct {
diff --git a/sdk/java/src/main/java/com/rmyndharis/openwa/model/BatchCancelResponse.java b/sdk/java/src/main/java/com/rmyndharis/openwa/model/BatchCancelResponse.java
new file mode 100644
index 000000000..329966b64
--- /dev/null
+++ b/sdk/java/src/main/java/com/rmyndharis/openwa/model/BatchCancelResponse.java
@@ -0,0 +1,7 @@
+package com.rmyndharis.openwa.model;
+
+/**
+ * Response from {@code POST /messages/batch/:batchId/cancel}: the batch state without the
+ * per-recipient results; call {@code batchStatus} for those.
+ */
+public record BatchCancelResponse(String batchId, BatchLifecycleStatus status, BatchProgress progress) {}
diff --git a/sdk/java/src/main/java/com/rmyndharis/openwa/model/BatchStatusResponse.java b/sdk/java/src/main/java/com/rmyndharis/openwa/model/BatchStatusResponse.java
index e50123d4c..a86a7e1a5 100644
--- a/sdk/java/src/main/java/com/rmyndharis/openwa/model/BatchStatusResponse.java
+++ b/sdk/java/src/main/java/com/rmyndharis/openwa/model/BatchStatusResponse.java
@@ -3,9 +3,9 @@ package com.rmyndharis.openwa.model;
import java.util.List;
/**
- * Response from {@code GET /messages/batch/:batchId} (batch status polling) and
- * {@code POST /messages/batch/:batchId/cancel}. Distinct from {@link BulkMessageResponse}
- * (the send-bulk acknowledgement). Optional fields are {@code null} when absent.
+ * Response from {@code GET /messages/batch/:batchId} (batch status polling). Distinct from
+ * {@link BulkMessageResponse} (the send-bulk acknowledgement) and {@link BatchCancelResponse}.
+ * Optional fields are {@code null} when absent.
*/
public record BatchStatusResponse(
String batchId,
diff --git a/sdk/java/src/main/java/com/rmyndharis/openwa/resources/MessagesResource.java b/sdk/java/src/main/java/com/rmyndharis/openwa/resources/MessagesResource.java
index 295c1d573..b00de0a63 100644
--- a/sdk/java/src/main/java/com/rmyndharis/openwa/resources/MessagesResource.java
+++ b/sdk/java/src/main/java/com/rmyndharis/openwa/resources/MessagesResource.java
@@ -5,6 +5,7 @@ import static com.rmyndharis.openwa.http.Http.encodeSegment;
import com.rmyndharis.openwa.OpenWAClient;
import com.rmyndharis.openwa.http.BinaryResponse;
import com.rmyndharis.openwa.http.HttpMethod;
+import com.rmyndharis.openwa.model.BatchCancelResponse;
import com.rmyndharis.openwa.model.BatchStatusResponse;
import com.rmyndharis.openwa.model.BulkMessageResponse;
import com.rmyndharis.openwa.model.ChatHistoryMessage;
@@ -311,19 +312,16 @@ public final class MessagesResource {
}
/**
- * Cancel a running batch. Requires an OPERATOR-level key.
- *
- * <p>The server answers only {@code batchId}, {@code status} and {@code progress}, so
- * {@code results}, {@code startedAt} and {@code completedAt} are always {@code null} here; call
- * {@link #batchStatus} for the per-item results.
+ * Cancel a running batch. Requires an OPERATOR-level key. The reply carries no per-item results;
+ * call {@link #batchStatus} for those.
*/
- public BatchStatusResponse cancelBatch(String sessionId, String batchId) {
+ public BatchCancelResponse cancelBatch(String sessionId, String batchId) {
return client.request(
HttpMethod.POST,
"/api/sessions/" + encodeSegment(sessionId) + "/messages/batch/" + encodeSegment(batchId) + "/cancel",
null,
null,
- BatchStatusResponse.class);
+ BatchCancelResponse.class);
}
// ── Internal ───────────────────────────────────────────────────────
diff --git a/sdk/java/src/test/java/com/rmyndharis/openwa/resources/MessagesResourceTest.java b/sdk/java/src/test/java/com/rmyndharis/openwa/resources/MessagesResourceTest.java
index 5cf8e378a..a0e6e2478 100644
--- a/sdk/java/src/test/java/com/rmyndharis/openwa/resources/MessagesResourceTest.java
+++ b/sdk/java/src/test/java/com/rmyndharis/openwa/resources/MessagesResourceTest.java
@@ -8,6 +8,7 @@ import com.rmyndharis.openwa.ClientConfig;
import com.rmyndharis.openwa.OpenWAClient;
import com.rmyndharis.openwa.http.BinaryResponse;
import com.rmyndharis.openwa.http.HttpMethod;
+import com.rmyndharis.openwa.model.BatchCancelResponse;
import com.rmyndharis.openwa.model.BulkMessageContent;
import com.rmyndharis.openwa.model.BulkMessageItem;
import com.rmyndharis.openwa.model.BulkMessageType;
@@ -368,8 +369,13 @@ class MessagesResourceTest {
@Test
void cancelBatchHitsCancelPath() {
- tx.respond(200, "{\"batchId\":\"b1\",\"status\":\"cancelled\"}");
- client.messages.cancelBatch("s", "b1");
+ tx.respond(
+ 200,
+ "{\"batchId\":\"b1\",\"status\":\"cancelled\","
+ + "\"progress\":{\"total\":2,\"sent\":1,\"failed\":0,\"pending\":0,\"cancelled\":1}}");
+ BatchCancelResponse res = client.messages.cancelBatch("s", "b1");
+ assertEquals("b1", res.batchId());
+ assertEquals(1, res.progress().cancelled());
assertEquals("http://h/api/sessions/s/messages/batch/b1/cancel", tx.lastRequest().url());
assertEquals(HttpMethod.POST, tx.lastRequest().method());
}
diff --git a/sdk/python/openwa/resources/messages.py b/sdk/python/openwa/resources/messages.py
index cd25579c5..0f7a2c519 100644
--- a/sdk/python/openwa/resources/messages.py
+++ b/sdk/python/openwa/resources/messages.py
@@ -15,6 +15,7 @@ from ..types import (
StarMessageRequest,
VotePollRequest,
UnpinMessageRequest,
+ BatchCancelResponse,
BatchStatusResponse,
BulkMessageResponse,
ChatHistoryMessage,
@@ -150,8 +151,8 @@ class MessagesResource:
def batch_status(self, session_id: str, batch_id: str) -> BatchStatusResponse:
return self._http.request("GET", f"/api/sessions/{quote_segment(session_id)}/messages/batch/{quote_segment(batch_id)}")
- def cancel_batch(self, session_id: str, batch_id: str) -> BatchStatusResponse:
- """Cancel a running batch. Requires an OPERATOR-level key."""
+ def cancel_batch(self, session_id: str, batch_id: str) -> BatchCancelResponse:
+ """Cancel a running batch. Requires an OPERATOR-level key. The reply carries no per-recipient results."""
return self._http.request(
"POST", f"/api/sessions/{quote_segment(session_id)}/messages/batch/{quote_segment(batch_id)}/cancel"
)
diff --git a/sdk/python/openwa/types.py b/sdk/python/openwa/types.py
index 420ea422f..0781b206c 100644
--- a/sdk/python/openwa/types.py
+++ b/sdk/python/openwa/types.py
@@ -713,9 +713,9 @@ class BatchProgress(TypedDict):
class BatchStatusResponse(TypedDict):
- """Response from ``GET /messages/batch/:batchId`` and the cancel endpoint.
+ """Response from ``GET /messages/batch/:batchId``.
- Distinct from :class:`BulkMessageResponse` (the send-bulk acknowledgement).
+ Distinct from :class:`BulkMessageResponse` (the send-bulk acknowledgement) and :class:`BatchCancelResponse`.
"""
batchId: str
@@ -726,6 +726,14 @@ class BatchStatusResponse(TypedDict):
completedAt: NotRequired[str | None]
+class BatchCancelResponse(TypedDict):
+ """Response from ``POST /messages/batch/:batchId/cancel``: the batch state without per-recipient ``results``."""
+
+ batchId: str
+ status: BatchLifecycleStatus
+ progress: BatchProgress
+
+
# ── Contact ───────────────────────────────────────────────────────
diff --git a/sdk/python/tests/test_sdk.py b/sdk/python/tests/test_sdk.py
index e55237852..fbc151e5e 100644
--- a/sdk/python/tests/test_sdk.py
+++ b/sdk/python/tests/test_sdk.py
@@ -4,7 +4,7 @@ from __future__ import annotations
import json
import time
-from typing import get_args
+from typing import get_args, get_type_hints
import httpx
import pytest
@@ -12,7 +12,14 @@ import pytest
from openwa import OpenWAClient, OpenWAApiError, OpenWANotFoundError
from openwa._http import build_url
from openwa.errors import OpenWAServiceUnavailableError
-from openwa.types import WebhookDelivery, WebhookDeliveryEvent, WebhookEvent, WebhookFilters
+from openwa.resources.messages import MessagesResource
+from openwa.types import (
+ BatchCancelResponse,
+ WebhookDelivery,
+ WebhookDeliveryEvent,
+ WebhookEvent,
+ WebhookFilters,
+)
from conftest import MockBackend, make_client
@@ -399,6 +406,11 @@ class TestMessages:
assert "/messages/batch/b/cancel" in backend.calls[-1].url
assert backend.calls[-1].method == "POST"
+ def test_cancel_batch_is_typed_as_the_cancel_response(self):
+ # The cancel route sends no results, so typing it as BatchStatusResponse (results required)
+ # lets a caller read a key that is never there.
+ assert get_type_hints(MessagesResource.cancel_batch)["return"] is BatchCancelResponse
+
# ── Sessions ───────────────────────────────────────────────────────