-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomer_products.go
126 lines (103 loc) · 3.37 KB
/
customer_products.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package rize
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/google/go-querystring/query"
)
// Handles all Customer Product operations
type customerProductService service
// CustomerProduct data type
type CustomerProduct struct {
UID string `json:"uid,omitempty"`
Status string `json:"status,omitempty"`
CustomerUID string `json:"customer_uid,omitempty"`
CustomerEmail string `json:"customer_email,omitempty"`
ProductUID string `json:"product_uid,omitempty"`
ProductName string `json:"product_name,omitempty"`
ProgramUID string `json:"program_uid,omitempty"`
}
// CustomerProductListParams builds the query parameters used in querying Customer Products
type CustomerProductListParams struct {
ProgramUID string `url:"program_uid,omitempty" json:"program_uid,omitempty"`
ProductUID string `url:"product_uid,omitempty" json:"product_uid,omitempty"`
CustomerUID string `url:"customer_uid,omitempty" json:"customer_uid,omitempty"`
}
// CustomerProductCreateParams are the body params used when creating a new Customer Product
type CustomerProductCreateParams struct {
CustomerUID string `json:"customer_uid"`
ProductUID string `json:"product_uid"`
}
// CustomerProductListResponse is an API response containing a list of Customer Products
type CustomerProductListResponse struct {
ListResponse
Data []*CustomerProduct `json:"data"`
}
// List Customers and the Products they have onboarded onto, filtered by the given parameters
func (cp *customerProductService) List(ctx context.Context, params *CustomerProductListParams) (*CustomerProductListResponse, error) {
v, err := query.Values(params)
if err != nil {
return nil, err
}
res, err := cp.client.doRequest(ctx, http.MethodGet, "customer_products", v, nil)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
response := &CustomerProductListResponse{}
if err = json.Unmarshal(body, response); err != nil {
return nil, err
}
return response, nil
}
// Create will submit a request to onboard a Customer onto a new product
func (cp *customerProductService) Create(ctx context.Context, params *CustomerProductCreateParams) (*CustomerProduct, error) {
if params.CustomerUID == "" || params.ProductUID == "" {
return nil, fmt.Errorf("CustomerUID and ProductUID are required")
}
bytesMessage, err := json.Marshal(params)
if err != nil {
return nil, err
}
res, err := cp.client.doRequest(ctx, http.MethodPost, "customer_products", nil, bytes.NewBuffer(bytesMessage))
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
response := &CustomerProduct{}
if err = json.Unmarshal(body, response); err != nil {
return nil, err
}
return response, nil
}
// Get a single Customer Product
func (cp *customerProductService) Get(ctx context.Context, uid string) (*CustomerProduct, error) {
if uid == "" {
return nil, fmt.Errorf("UID is required")
}
res, err := cp.client.doRequest(ctx, http.MethodGet, fmt.Sprintf("customer_products/%s", uid), nil, nil)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
response := &CustomerProduct{}
if err = json.Unmarshal(body, response); err != nil {
return nil, err
}
return response, nil
}