forked from getconversio/go-shopify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomer.go
78 lines (67 loc) · 2.57 KB
/
customer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package goshopify
import (
"fmt"
"time"
"github.com/shopspring/decimal"
)
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 {
List(interface{}) ([]Customer, error)
Count(interface{}) (int, error)
Get(int, interface{}) (*Customer, error)
}
// CustomerServiceOp handles communication with the product related methods of
// the Shopify API.
type CustomerServiceOp struct {
client *Client
}
// Customer represents a Shopify customer
type Customer struct {
ID int `json:"id"`
Email string `json:"email"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
State string `json:"state"`
Note string `json:"note"`
VerifiedEmail bool `json:"verified_email"`
MultipassIdentifier string `json:"multipass_identifier"`
OrdersCount int `json:"orders_count"`
TaxExempt bool `json:"tax_exempt"`
TotalSpent *decimal.Decimal `json:"total_spent"`
Phone string `json:"phone"`
Tags string `json:"tags"`
LastOrderId int `json:"last_order_id"`
AcceptsMarketing bool `json:"accepts_marketing"`
CreatedAt *time.Time `json:"created_at"`
UpdatedAt *time.Time `json:"updated_at"`
}
// Represents the result from the customers/X.json endpoint
type CustomerResource struct {
Customer *Customer `json:"customer"`
}
// Represents the result from the customers.json endpoint
type CustomersResource struct {
Customers []Customer `json:"customers"`
}
// List customers
func (s *CustomerServiceOp) List(options interface{}) ([]Customer, error) {
path := fmt.Sprintf("%s.json", customersBasePath)
resource := new(CustomersResource)
err := s.client.Get(path, resource, options)
return resource.Customers, err
}
// Count customers
func (s *CustomerServiceOp) Count(options interface{}) (int, error) {
path := fmt.Sprintf("%s/count.json", customersBasePath)
return s.client.Count(path, options)
}
// Get customer
func (s *CustomerServiceOp) Get(customerID int, options interface{}) (*Customer, error) {
path := fmt.Sprintf("%s/%v.json", customersBasePath, customerID)
resource := new(CustomerResource)
err := s.client.Get(path, resource, options)
return resource.Customer, err
}