forked from emiddleton/gads
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcustomer.go
More file actions
196 lines (169 loc) · 4.44 KB
/
customer.go
File metadata and controls
196 lines (169 loc) · 4.44 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package gads
import "encoding/xml"
// CustomerService fetches or modify Customer properties
type CustomerService struct {
Auth
}
// Customer represents a customer in the Google Adwords API
type Customer struct {
ID uint64 `xml:"customerId,omitempty"`
CurrencyCode *string `xml:"currencyCode,omitempty"`
DateTimeZone *string `xml:"dateTimeZone,omitempty"`
DescriptiveName string `xml:"descriptiveName,omitempty"`
CanManageClients bool `xml:"canManageClients,omitempty"`
TestAccount bool `xml:"testAccount,omitempty"`
AutoTaggingEnabled *bool `xml:"autoTaggingEnabled"`
TrackingURLTemplate *string `xml:"trackingUrlTemplate"`
// TODO: implements this - not needed for the moment
//ConversionTrackingSettings *ConversionTrackingSettings `xml:"conversionTrackingSettings,omitempty"`
//RemarketingSettings *RemarketingSettings `xml:"remarketingSettings.omitempty"`
}
// NewCustomerService creates a CustomerService
func NewCustomerService(auth *Auth) *CustomerService {
return &CustomerService{Auth: *auth}
}
// Get fetches the managed customers of the current account
func (m *CustomerService) GetCustomers(s *Selector) (
customers []Customer,
err error,
) {
if s != nil {
s.XMLName = xml.Name{"", "serviceSelector"}
}
var respBody []byte
respBody, err = m.request(
customerServiceUrl,
"getCustomers",
struct {
XMLName xml.Name
Sel *Selector
}{
XMLName: xml.Name{
Space: managedCustomerUrl,
Local: "getCustomers",
},
Sel: s,
},
)
if err != nil {
return
}
getResp := struct {
Customers []Customer `xml:"rval"`
}{}
err = xml.Unmarshal(respBody, &getResp)
if err != nil {
return
}
return getResp.Customers, err
}
// Mutate performs modifications of one or many customer
func (m *CustomerService) Mutate(c Customer) (customer Customer, err error) {
mutation := struct {
XMLName xml.Name
Customer Customer `xml:"customer"`
}{
XMLName: xml.Name{
Space: managedCustomerUrl,
Local: "mutate",
},
Customer: c,
}
respBody, err := m.request(customerServiceUrl, "mutate", mutation)
if err != nil {
return customer, err
}
mutateResp := struct {
BaseResponse
RespCustomer Customer `xml:"rval"`
}{}
err = xml.Unmarshal([]byte(respBody), &mutateResp)
if err != nil {
return customer, err
}
if len(mutateResp.PartialFailureErrors) > 0 {
err = mutateResp.PartialFailureErrors
}
return mutateResp.RespCustomer, err
}
// ServiceLink struct for customer service link
type ServiceLink struct {
Type string `xml:"serviceType,omitempty"`
ID int64 `xml:"serviceLinkId,omitempty"`
Status string `xml:"linkStatus,omitempty"`
Name string `xml:"name,omitempty"`
}
// GetServiceLinks fetches the service links
func (m *CustomerService) GetServiceLinks(s *Selector) (serviceLinks []ServiceLink, err error) {
if s != nil {
s.XMLName = xml.Name{"", "selector"}
}
var respBody []byte
respBody, err = m.request(
customerServiceUrl,
"getServiceLinks",
struct {
XMLName xml.Name
Sel *Selector
}{
XMLName: xml.Name{
Space: managedCustomerUrl,
Local: "getServiceLinks",
},
Sel: s,
},
)
if err != nil {
return
}
getResp := struct {
ServiceLinks []ServiceLink `xml:"rval"`
}{}
err = xml.Unmarshal(respBody, &getResp)
if err != nil {
return
}
return getResp.ServiceLinks, err
}
type ServiceLinkOperations map[string][]ServiceLink
func (s *CustomerService) MutateServiceLinks(ops ServiceLinkOperations) (links []ServiceLink, err error) {
type linkOperation struct {
Action string `xml:"https://adwords.google.com/api/adwords/cm/v201809 operator"`
ServiceLink ServiceLink `xml:"operand"`
}
operations := []linkOperation{}
for action, links := range ops {
for _, link := range links {
operations = append(operations, linkOperation{Action: action, ServiceLink: link})
}
}
respBody, err := s.Auth.request(
customerServiceUrl,
"mutateServiceLinks",
struct {
XMLName xml.Name
Ops []linkOperation `xml:"operations"`
}{
XMLName: xml.Name{
Space: managedCustomerUrl,
Local: "mutateServiceLinks",
},
Ops: operations,
},
)
if err != nil {
return links, err
}
mutateResp := struct {
BaseResponse
Links []ServiceLink `xml:"rval>value"`
}{}
err = xml.Unmarshal([]byte(respBody), &mutateResp)
if err != nil {
return links, err
}
if len(mutateResp.PartialFailureErrors) > 0 {
err = mutateResp.PartialFailureErrors
}
return mutateResp.Links, err
}