From 9c5a83f3ef0a596cd0597a7f4a5eefd9f4d7448a Mon Sep 17 00:00:00 2001 From: yulintan <35462237+yulintan@users.noreply.github.com> Date: Mon, 26 Nov 2018 08:11:21 -0600 Subject: [PATCH] Add list collects endpoint (#38) --- collect.go | 58 +++++++++++++++++++++++++++++++++++ collect_test.go | 80 +++++++++++++++++++++++++++++++++++++++++++++++++ goshopify.go | 2 ++ 3 files changed, 140 insertions(+) create mode 100644 collect.go create mode 100644 collect_test.go diff --git a/collect.go b/collect.go new file mode 100644 index 00000000..1370ae46 --- /dev/null +++ b/collect.go @@ -0,0 +1,58 @@ +package goshopify + +import ( + "fmt" + "time" +) + +const collectsBasePath = "admin/collects" + +// CollectService is an interface for interfacing with the collect endpoints +// of the Shopify API. +// See: https://help.shopify.com/api/reference/products/collect +type CollectService interface { + List(interface{}) ([]Collect, error) + Count(interface{}) (int, error) +} + +// CollectServiceOp handles communication with the collect related methods of +// the Shopify API. +type CollectServiceOp struct { + client *Client +} + +// Collect represents a Shopify collect +type Collect struct { + ID int `json:"id,omitempty"` + CollectionID int `json:"collection_id,omitempty"` + ProductID int `json:"product_id,omitempty"` + Featured bool `json:"featured,omitempty"` + CreatedAt *time.Time `json:"created_at,omitempty"` + UpdatedAt *time.Time `json:"updated_at,omitempty"` + Position int `json:"position,omitempty"` + SortValue string `json:"sort_value,omitempty"` +} + +// Represents the result from the collects/X.json endpoint +type CollectResource struct { + Collect *Collect `json:"collect"` +} + +// Represents the result from the collects.json endpoint +type CollectsResource struct { + Collects []Collect `json:"collects"` +} + +// List collects +func (s *CollectServiceOp) List(options interface{}) ([]Collect, error) { + path := fmt.Sprintf("%s.json", collectsBasePath) + resource := new(CollectsResource) + err := s.client.Get(path, resource, options) + return resource.Collects, err +} + +// Count collects +func (s *CollectServiceOp) Count(options interface{}) (int, error) { + path := fmt.Sprintf("%s/count.json", collectsBasePath) + return s.client.Count(path, options) +} diff --git a/collect_test.go b/collect_test.go new file mode 100644 index 00000000..a336f38b --- /dev/null +++ b/collect_test.go @@ -0,0 +1,80 @@ +package goshopify + +import ( + "reflect" + "testing" + + httpmock "gopkg.in/jarcoal/httpmock.v1" +) + +func collectTests(t *testing.T, collect Collect) { + + // Test a few fields + cases := []struct { + field string + expected interface{} + actual interface{} + }{ + {"ID", 18091352323, collect.ID}, + {"CollectionID", 241600835, collect.CollectionID}, + {"ProductID", 6654094787, collect.ProductID}, + {"Featured", false, collect.Featured}, + {"SortValue", "0000000001", collect.SortValue}, + } + + for _, c := range cases { + if c.expected != c.actual { + t.Errorf("Collect.%v returned %v, expected %v", c.field, c.actual, c.expected) + } + } +} + +func TestCollectList(t *testing.T) { + setup() + defer teardown() + + httpmock.RegisterResponder("GET", "https://fooshop.myshopify.com/admin/collects.json", + httpmock.NewStringResponder(200, `{"collects": [{"id":1},{"id":2}]}`)) + + collects, err := client.Collect.List(nil) + if err != nil { + t.Errorf("Collect.List returned error: %v", err) + } + + expected := []Collect{{ID: 1}, {ID: 2}} + if !reflect.DeepEqual(collects, expected) { + t.Errorf("Collect.List returned %+v, expected %+v", collects, expected) + } +} + +func TestCollectCount(t *testing.T) { + setup() + defer teardown() + + httpmock.RegisterResponder("GET", "https://fooshop.myshopify.com/admin/collects/count.json", + httpmock.NewStringResponder(200, `{"count": 5}`)) + + params := map[string]string{"since_id": "123"} + httpmock.RegisterResponderWithQuery("GET", "https://fooshop.myshopify.com/admin/collects/count.json", params, + httpmock.NewStringResponder(200, `{"count": 2}`)) + + cnt, err := client.Collect.Count(nil) + if err != nil { + t.Errorf("Collect.Count returned error: %v", err) + } + + expected := 5 + if cnt != expected { + t.Errorf("Collect.Count returned %d, expected %d", cnt, expected) + } + + cnt, err = client.Collect.Count(ListOptions{SinceID: 123}) + if err != nil { + t.Errorf("Collect.Count returned error: %v", err) + } + + expected = 2 + if cnt != expected { + t.Errorf("Collect.Count returned %d, expected %d", cnt, expected) + } +} diff --git a/goshopify.go b/goshopify.go index bf8687f0..22589166 100644 --- a/goshopify.go +++ b/goshopify.go @@ -70,6 +70,7 @@ type Client struct { Redirect RedirectService Page PageService StorefrontAccessToken StorefrontAccessTokenService + Collect CollectService } // A general response error that follows a similar layout to Shopify's response @@ -207,6 +208,7 @@ func NewClient(app App, shopName, token string) *Client { c.Page = &PageServiceOp{client: c} c.StorefrontAccessToken = &StorefrontAccessTokenServiceOp{client: c} c.UsageCharge = &UsageChargeServiceOp{client: c} + c.Collect = &CollectServiceOp{client: c} return c }