Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(sdk-go): return nil from Catalog Info and Product on an empty body

Yudhi Armyndharis5f9248aa95d6
diff --git a/sdk/go/catalog.go b/sdk/go/catalog.go
index 14e424ab8..34209ee89 100644
--- a/sdk/go/catalog.go
+++ b/sdk/go/catalog.go
@@ -12,14 +12,11 @@ func (s *CatalogService) sessionBase(sessionID string) string {
return "/api/sessions/" + pathEscape(sessionID)
}
-// Info returns catalog metadata.
+// Info returns catalog metadata, or nil when the account has no catalog.
func (s *CatalogService) Info(ctx context.Context, sessionID string) (*CatalogInfo, error) {
- var out CatalogInfo
+ var out *CatalogInfo
err := s.client.do(ctx, "GET", s.sessionBase(sessionID)+"/catalog", nil, nil, &out)
- if err != nil {
- return nil, err
- }
- return &out, nil
+ return out, err
}
// Products returns paginated catalog products.
@@ -32,14 +29,11 @@ func (s *CatalogService) Products(ctx context.Context, sessionID string, query *
return &out, nil
}
-// Product returns a single catalog product.
+// Product returns a single catalog product, or nil when no product has that id.
func (s *CatalogService) Product(ctx context.Context, sessionID, productID string) (*CatalogProduct, error) {
- var out CatalogProduct
+ var out *CatalogProduct
err := s.client.do(ctx, "GET", s.sessionBase(sessionID)+"/catalog/products/"+pathEscape(productID), nil, nil, &out)
- if err != nil {
- return nil, err
- }
- return &out, nil
+ return out, err
}
// SendProduct sends a product message. Requires an OPERATOR-level key.
diff --git a/sdk/go/catalog_empty_body_test.go b/sdk/go/catalog_empty_body_test.go
new file mode 100644
index 000000000..4dda1fbf5
--- /dev/null
+++ b/sdk/go/catalog_empty_body_test.go
@@ -0,0 +1,31 @@
+package openwa
+
+import (
+ "context"
+ "testing"
+)
+
+// The gateway answers 200 with an empty body when the account has no catalog or no product has the
+// id. Decoding that into a value struct would hand back a record with every field empty, which
+// cannot be told apart from a real one, so both reads must come back nil.
+func TestCatalogEmptyBodyReturnsNil(t *testing.T) {
+ c := newTestClient(t, &recordTransport{status: 200, body: ""})
+ ctx := context.Background()
+
+ info, err := c.Catalog.Info(ctx, "s1")
+ if err != nil || info != nil {
+ t.Errorf("Info = %+v, %v; want nil, nil", info, err)
+ }
+ product, err := c.Catalog.Product(ctx, "s1", "missing")
+ if err != nil || product != nil {
+ t.Errorf("Product = %+v, %v; want nil, nil", product, err)
+ }
+}
+
+func TestCatalogProductDecodesBody(t *testing.T) {
+ c := newTestClient(t, &recordTransport{status: 200, body: `{"id":"p1","name":"A","url":"u","isAvailable":true}`})
+ product, err := c.Catalog.Product(context.Background(), "s1", "p1")
+ if err != nil || product == nil || product.ID != "p1" {
+ t.Errorf("Product = %+v, %v; want p1", product, err)
+ }
+}