Skip to content

Commit

Permalink
add endpoint to get all tags across customers (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
miker3 authored and dbertouille committed Nov 30, 2018
1 parent 4f26131 commit 1ee02fc
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
14 changes: 14 additions & 0 deletions customer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type CustomerService interface {
Update(Customer) (*Customer, error)
Delete(int) error
ListOrders(int, interface{}) ([]Order, error)
ListTags(interface{}) ([]string, error)

// MetafieldsService used for Customer resource to communicate with Metafields resource
MetafieldsService
Expand Down Expand Up @@ -68,6 +69,11 @@ type CustomersResource struct {
Customers []Customer `json:"customers"`
}

// Represents the result from the customers/tags.json endpoint
type CustomerTagsResource struct {
Tags []string `json:"tags"`
}

// Represents the options available when searching for a customer
type CustomerSearchOptions struct {
Page int `url:"page,omitempty"`
Expand Down Expand Up @@ -174,3 +180,11 @@ func (s *CustomerServiceOp) ListOrders(customerID int, options interface{}) ([]O
err := s.client.Get(path, resource, options)
return resource.Orders, err
}

// ListTags retrieves all unique tags across all customers
func (s *CustomerServiceOp) ListTags(options interface{}) ([]string, error) {
path := fmt.Sprintf("%s/tags.json", customersBasePath)
resource := new(CustomerTagsResource)
err := s.client.Get(path, resource, options)
return resource.Tags, err
}
26 changes: 26 additions & 0 deletions customer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,3 +449,29 @@ func TestCustomerListOrders(t *testing.T) {
order := orders[0]
orderTests(t, order)
}

func TestCustomerListTags(t *testing.T) {
setup()
defer teardown()

httpmock.RegisterResponder(
"GET",
"https://fooshop.myshopify.com/admin/customers/tags.json",
httpmock.NewBytesResponder(200, loadFixture("customer_tags.json")),
)

tags, err := client.Customer.ListTags(nil)
if err != nil {
t.Errorf("Customer.ListTags returned error: %v", err)
}

// Check that tags were parsed
if len(tags) != 2 {
t.Errorf("Customer.ListTags got %v tags, expected: 2", len(tags))
}

// Check correct tag was read
if len(tags) > 0 && tags[0] != "tag1" {
t.Errorf("Customer.ListTags got %v as the first tag, expected: 'tag1'", tags[0])
}
}
6 changes: 6 additions & 0 deletions fixtures/customer_tags.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"tags": [
"tag1",
"another-tag"
]
}

0 comments on commit 1ee02fc

Please sign in to comment.