forked from bold-commerce/go-shopify
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add customer and order counts (bold-commerce#3)
* Singularize naming of Product service * Add customer and order count
- Loading branch information
Showing
7 changed files
with
161 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package goshopify | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
const customersBasePath = "admin/customers" | ||
|
||
// CustomerService is an interface for interfacing with the customers endpoints | ||
// of the Shopify API. | ||
// See: https://help.shopify.com/api/reference/customer | ||
type CustomerService interface { | ||
Count() (int, error) | ||
} | ||
|
||
// CustomerServiceOp handles communication with the product related methods of | ||
// the Shopify API. | ||
type CustomerServiceOp struct { | ||
client *Client | ||
} | ||
|
||
type customerCountRoot struct { | ||
Count int `json:"count"` | ||
} | ||
|
||
// Count customers | ||
func (s *CustomerServiceOp) Count() (int, error) { | ||
path := fmt.Sprintf("%s/count.json", customersBasePath) | ||
|
||
req, err := s.client.NewRequest("GET", path, nil) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
root := new(customerCountRoot) | ||
err = s.client.Do(req, root) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
return root.Count, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package goshopify | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/jarcoal/httpmock" | ||
) | ||
|
||
func TestCustomerCount(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
|
||
httpmock.RegisterResponder("GET", "https://fooshop.myshopify.com/admin/customers/count.json", | ||
httpmock.NewStringResponder(200, `{"count": 5}`)) | ||
|
||
cnt, err := client.Customer.Count() | ||
if err != nil { | ||
t.Errorf("Customer.Count returned error: %v", err) | ||
} | ||
|
||
expected := 5 | ||
if cnt != expected { | ||
t.Errorf("Customer.Count returned %d, expected %d", cnt, expected) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package goshopify | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
const ordersBasePath = "admin/orders" | ||
|
||
// OrderService is an interface for interfacing with the orders endpoints of | ||
// the Shopify API. | ||
// See: https://help.shopify.com/api/reference/order | ||
type OrderService interface { | ||
Count() (int, error) | ||
} | ||
|
||
// OrderServiceOp handles communication with the order related methods of the | ||
// Shopify API. | ||
type OrderServiceOp struct { | ||
client *Client | ||
} | ||
|
||
type orderCountRoot struct { | ||
Count int `json:"count"` | ||
} | ||
|
||
// Count orders | ||
func (s *OrderServiceOp) Count() (int, error) { | ||
path := fmt.Sprintf("%s/count.json", ordersBasePath) | ||
|
||
req, err := s.client.NewRequest("GET", path, nil) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
root := new(orderCountRoot) | ||
err = s.client.Do(req, root) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
return root.Count, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package goshopify | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/jarcoal/httpmock" | ||
) | ||
|
||
func TestOrderCount(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
|
||
httpmock.RegisterResponder("GET", "https://fooshop.myshopify.com/admin/orders/count.json", | ||
httpmock.NewStringResponder(200, `{"count": 7}`)) | ||
|
||
cnt, err := client.Order.Count() | ||
if err != nil { | ||
t.Errorf("Order.Count returned error: %v", err) | ||
} | ||
|
||
expected := 7 | ||
if cnt != expected { | ||
t.Errorf("Order.Count returned %d, expected %d", cnt, expected) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters