Skip to content

Commit

Permalink
Add list collects endpoint (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
yulintan authored and dbertouille committed Nov 26, 2018
1 parent e88e862 commit 9c5a83f
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 0 deletions.
58 changes: 58 additions & 0 deletions collect.go
Original file line number Diff line number Diff line change
@@ -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)
}
80 changes: 80 additions & 0 deletions collect_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
2 changes: 2 additions & 0 deletions goshopify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 9c5a83f

Please sign in to comment.