Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(sdk-go): send the idempotency key only where it is honored
Yudhi Armyndharis3bff92ac3e0c
diff --git a/sdk/go/client.go b/sdk/go/client.goindex 2ce04489d..fce72f0ab 100644--- a/sdk/go/client.go+++ b/sdk/go/client.go@@ -241,11 +241,28 @@ type idempotencyContextKey struct{}// WithIdempotencyKey supplies a caller-owned key for one logical send. Reuse// this context for its retries and choose another key for an unrelated send.-// Only the gateway's documented idempotent send routes interpret the header.+// Only the gateway's documented idempotent send routes interpret the header,+// and the SDK sends it on those routes only.func WithIdempotencyKey(ctx context.Context, key string) context.Context {return context.WithValue(ctx, idempotencyContextKey{}, key)}+// idempotentSendSegments are the message routes that interpret Idempotency-Key. net/http sends a+// request that carries the header again after a dropped connection, which is only safe where the+// gateway deduplicates, so the header goes nowhere else.+var idempotentSendSegments = map[string]bool{+ "send-text": true, "send-image": true, "send-video": true, "send-audio": true,+ "send-document": true, "send-sticker": true, "send-location": true, "send-contact": true,+ "send-template": true, "send-poll": true, "reply": true, "forward": true,+}++// isIdempotentSendPath reports whether path is /api/sessions/{id}/messages/{segment} for one of them.+func isIdempotentSendPath(path string) bool {+ parts := strings.Split(path, "/")+ return len(parts) == 6 && parts[1] == "api" && parts[2] == "sessions" && parts[4] == "messages" &&+ idempotentSendSegments[parts[5]]+}+// doRaw performs the request and returns the response body and Content-Type.// Any non-2xx status (including an unfollowed 3xx) is an error.//@@ -286,7 +303,9 @@ func (c *Client) doRaw(ctx context.Context, method, path string, query url.Valueif len(key) < 1 || len(key) > 255 || strings.IndexFunc(key, func(r rune) bool { return r < 0x21 || r > 0x7e }) >= 0 {return nil, "", errors.New("openwa: idempotency key must contain 1-255 visible ASCII characters")}- req.Header.Set("Idempotency-Key", key)+ if req.Method == http.MethodPost && isIdempotentSendPath(path) {+ req.Header.Set("Idempotency-Key", key)+ }}if body != nil {req.Header.Set("Content-Type", "application/json")diff --git a/sdk/go/idempotency_scope_test.go b/sdk/go/idempotency_scope_test.gonew file mode 100644index 000000000..49ad96478--- /dev/null+++ b/sdk/go/idempotency_scope_test.go@@ -0,0 +1,40 @@+package openwa++import (+ "context"+ "testing"+)++// net/http sends a request that carries Idempotency-Key again after a dropped connection, so the+// key from the context reaches only the routes where the gateway deduplicates.+func TestIdempotencyKeyReachesOnlyRoutesThatHonorIt(t *testing.T) {+ rt := &recordTransport{status: 200, body: `{}`}+ c := newTestClient(t, rt)+ ctx := WithIdempotencyKey(context.Background(), "order-1")+ for _, tc := range []struct {+ name string+ do func() error+ want string+ }{+ {"send-text", func() error { _, err := c.Messages.SendText(ctx, "s", SendTextRequest{}); return err }, "order-1"},+ {"reply", func() error { _, err := c.Messages.Reply(ctx, "s", ReplyMessageRequest{}); return err }, "order-1"},+ {"forward", func() error { _, err := c.Messages.Forward(ctx, "s", ForwardMessageRequest{}); return err }, "order-1"},+ {"send-bulk", func() error { _, err := c.Messages.SendBulk(ctx, "s", SendBulkRequest{}); return err }, ""},+ {"click-button", func() error { _, err := c.Messages.ClickButton(ctx, "s", ClickButtonRequest{}); return err }, ""},+ {"status", func() error { _, err := c.Status.SendText(ctx, "s", SendTextStatusRequest{}); return err }, ""},+ {"do on a keyed route", func() error {+ return c.Do(ctx, "POST", "/api/sessions/s/messages/send-text", nil, map[string]string{}, nil)+ }, "order-1"},+ {"do on another route", func() error { return c.Do(ctx, "POST", "/api/sessions/s/groups", nil, map[string]string{}, nil) }, ""},+ } {+ t.Run(tc.name, func(t *testing.T) {+ rt.lastReq = nil+ if err := tc.do(); err != nil {+ t.Fatal(err)+ }+ if got := rt.lastReq.Header.Get("Idempotency-Key"); got != tc.want {+ t.Fatalf("Idempotency-Key = %q, want %q", got, tc.want)+ }+ })+ }+}