forked from segmentio/gads
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.go
266 lines (232 loc) · 9.65 KB
/
base.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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package gads
import (
"encoding/xml"
"flag"
"fmt"
"io"
"io/ioutil"
"net/http"
"testing"
)
const (
version = "v201809"
rootUrl = "https://adwords.google.com/api/adwords/cm/"
baseUrl = "https://adwords.google.com/api/adwords/cm/" + version
rootMcmUrl = "https://adwords.google.com/api/adwords/mcm/"
baseMcmUrl = "https://adwords.google.com/api/adwords/mcm/" + version
rootRemarketingUrl = "https://adwords.google.com/api/adwords/rm/"
baseRemarketingUrl = "https://adwords.google.com/api/adwords/rm/" + version
rootReportDownloadUrl = "https://adwords.google.com/api/adwords/reportdownload/"
baseReportDownloadUrl = "https://adwords.google.com/api/adwords/reportdownload/" + version
rootTrafficUrl = "https://adwords.google.com/api/adwords/o/"
baseTrafficUrl = "https://adwords.google.com/api/adwords/o/" + version
)
type ServiceUrl struct {
Url string
Name string
}
// exceptions
var (
ERROR_NOT_YET_IMPLEMENTED = fmt.Errorf("Not yet implemented")
)
var (
configJson = flag.String("config_json", "./config.json", "API credentials")
// service urls
adGroupAdServiceUrl = ServiceUrl{baseUrl, "AdGroupAdService"}
adGroupBidModifierServiceUrl = ServiceUrl{baseUrl, "AdGroupBidModifierService"}
adGroupCriterionServiceUrl = ServiceUrl{baseUrl, "AdGroupCriterionService"}
adGroupExtensionSettingServiceUrl = ServiceUrl{baseUrl, "AdGroupExtensionSettingService"}
adGroupFeedServiceUrl = ServiceUrl{baseUrl, "AdGroupFeedService"}
adGroupServiceUrl = ServiceUrl{baseUrl, "AdGroupService"}
adParamServiceUrl = ServiceUrl{baseUrl, "AdParamService"}
adwordsUserListServiceUrl = ServiceUrl{baseRemarketingUrl, "AdwordsUserListService"}
batchJobServiceUrl = ServiceUrl{baseUrl, "BatchJobService"}
biddingStrategyServiceUrl = ServiceUrl{baseUrl, "BiddingStrategyService"}
budgetOrderServiceUrl = ServiceUrl{baseUrl, "BudgetOrderService"}
budgetServiceUrl = ServiceUrl{baseUrl, "BudgetService"}
campaignExtensionSettingUrl = ServiceUrl{baseUrl, "CampaignExtensionSettingService"}
campaignCriterionServiceUrl = ServiceUrl{baseUrl, "CampaignCriterionService"}
campaignFeedServiceUrl = ServiceUrl{baseUrl, "CampaignFeedService"}
campaignServiceUrl = ServiceUrl{baseUrl, "CampaignService"}
campaignSharedSetServiceUrl = ServiceUrl{baseUrl, "CampaignSharedSetService"}
constantDataServiceUrl = ServiceUrl{baseUrl, "ConstantDataService"}
conversionTrackerServiceUrl = ServiceUrl{baseUrl, "ConversionTrackerService"}
customerFeedServiceUrl = ServiceUrl{baseUrl, "CustomerFeedService"}
customerServiceUrl = ServiceUrl{baseMcmUrl, "CustomerService"}
customerSyncServiceUrl = ServiceUrl{baseUrl, "CustomerSyncService"}
dataServiceUrl = ServiceUrl{baseUrl, "DataService"}
experimentServiceUrl = ServiceUrl{baseUrl, "ExperimentService"}
feedItemServiceUrl = ServiceUrl{baseUrl, "FeedItemService"}
feedMappingServiceUrl = ServiceUrl{baseUrl, "FeedMappingService"}
feedServiceUrl = ServiceUrl{baseUrl, "FeedService"}
geoLocationServiceUrl = ServiceUrl{baseUrl, "GeoLocationService"}
labelServiceUrl = ServiceUrl{baseUrl, "LabelService"}
locationCriterionServiceUrl = ServiceUrl{baseUrl, "LocationCriterionService"}
managedCustomerServiceUrl = ServiceUrl{baseMcmUrl, "ManagedCustomerService"}
mediaServiceUrl = ServiceUrl{baseUrl, "MediaService"}
mutateJobServiceUrl = ServiceUrl{baseUrl, "MutateJobService"}
offlineConversionFeedServiceUrl = ServiceUrl{baseUrl, "OfflineConversionFeedService"}
reportDefinitionServiceUrl = ServiceUrl{baseUrl, "ReportDefinitionService"}
reportDownloadServiceUrl = ServiceUrl{baseReportDownloadUrl, ""}
sharedCriterionServiceUrl = ServiceUrl{baseUrl, "SharedCriterionService"}
sharedSetServiceUrl = ServiceUrl{baseUrl, "SharedSetService"}
targetingIdeaServiceUrl = ServiceUrl{baseTrafficUrl, "TargetingIdeaService"}
trafficEstimatorServiceUrl = ServiceUrl{baseTrafficUrl, "TrafficEstimatorService"}
)
func (s ServiceUrl) String() string {
if s.Name != "" {
return s.Url + "/" + s.Name
}
return s.Url
}
type Auth struct {
CustomerId string
DeveloperToken string
UserAgent string
PartialFailure bool
Testing *testing.T `json:"-"`
Client *http.Client `json:"-"`
}
//
// Selector structs
//
type DateRange struct {
Min string `xml:"min"`
Max string `xml:"max"`
}
type Predicate struct {
Field string `xml:"field"`
Operator string `xml:"operator"`
Values []string `xml:"values"`
}
type OrderBy struct {
Field string `xml:"field"`
SortOrder string `xml:"sortOrder"`
}
type Paging struct {
Offset int64 `xml:"https://adwords.google.com/api/adwords/cm/v201809 startIndex"`
Limit int64 `xml:"https://adwords.google.com/api/adwords/cm/v201809 numberResults"`
}
type Selector struct {
XMLName xml.Name
Fields []string `xml:"fields"`
Predicates []Predicate `xml:"predicates"`
DateRange *DateRange `xml:"dateRange,omitempty"`
Ordering []OrderBy `xml:"ordering"`
Paging *Paging `xml:"paging,omitempty"`
}
type AWQLQuery struct {
XMLName xml.Name
Query string `xml:"query"`
}
type Options struct {
CustomerID string
}
// https://developers.google.com/adwords/api/docs/reference/v201809/AdGroupExtensionSettingService.DayOfWeek
// Days of the week.
// MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
type DayOfWeek string
// https://developers.google.com/adwords/api/docs/reference/v201809/AdGroupExtensionSettingService.MinuteOfHour
// Minutes in an hour. Currently only 0, 15, 30, and 45 are supported
// ZERO, FIFTEEN, THIRTY, FORTY_FIVE
type MinuteOfHour string
// https://developers.google.com/adwords/api/docs/reference/v201809/AdGroupExtensionSettingService.GeoRestriction
// A restriction used to determine if the request context's geo should be matched.
// UNKNOWN, LOCATION_OF_PRESENCE
type GeoRestriction string
// https://developers.google.com/adwords/api/docs/reference/v201809/AdGroupExtensionSettingService.PolicyData
// Approval and policy information attached to an entity.
type PolicyData struct {
DisapprovalReasons []DisapprovalReason `xml:"https://adwords.google.com/api/adwords/cm/v201809 disapprovalReasons,omitempty"`
PolicyDataType string `xml:"https://adwords.google.com/api/adwords/cm/v201809 PolicyData.Type,omitempty"`
}
// https://developers.google.com/adwords/api/docs/reference/v201809/AdGroupExtensionSettingService.DisapprovalReason
// Container for information about why an AdWords entity was disapproved.
type DisapprovalReason struct {
ShortName string `xml:"https://adwords.google.com/api/adwords/cm/v201809 shortName,omitempty"`
}
// error parsers
func selectorError() (err error) {
return err
}
func (a *Auth) request(serviceUrl ServiceUrl, action string, body interface{}, opts *Options) (respBody []byte, err error) {
type devToken struct {
XMLName xml.Name
}
type soapReqHeader struct {
XMLName xml.Name
UserAgent string `xml:"userAgent"`
DeveloperToken string `xml:"developerToken"`
ClientCustomerId string `xml:"clientCustomerId,omitempty"`
PartialFailure bool `xml:"partialFailure,omitempty"`
}
type soapReqBody struct {
Body interface{}
}
type soapReqEnvelope struct {
XMLName xml.Name
Header soapReqHeader `xml:"Header>RequestHeader"`
Body soapReqBody `xml:"http://schemas.xmlsoap.org/soap/envelope/ Body"`
}
header := soapReqHeader{
XMLName: xml.Name{serviceUrl.Url, "RequestHeader"},
UserAgent: a.UserAgent,
DeveloperToken: a.DeveloperToken,
}
if opts != nil && opts.CustomerID != "" {
header.ClientCustomerId = opts.CustomerID
} else if a.CustomerId != "" {
header.ClientCustomerId = a.CustomerId
}
r, w := io.Pipe()
defer r.Close()
go func() {
w.CloseWithError(xml.NewEncoder(w).Encode(soapReqEnvelope{
XMLName: xml.Name{"http://schemas.xmlsoap.org/soap/envelope/", "Envelope"},
Header: header,
Body: soapReqBody{body},
}))
}()
req, err := http.NewRequest("POST", serviceUrl.String(), r)
req.Header.Add("Accept", "text/xml")
req.Header.Add("Accept", "multipart/*")
req.Header.Add("Content-Type", "text/xml;charset=UTF-8")
req.Header.Add("SOAPAction", action)
resp, err := a.Client.Do(req)
if err != nil {
return []byte{}, err
}
defer resp.Body.Close()
respBody, err = ioutil.ReadAll(resp.Body)
if a.Testing != nil {
a.Testing.Logf("respBody ->\n%s\n%s\n", string(respBody), resp.Status)
}
type soapRespHeader struct {
RequestId string `xml:"requestId"`
ServiceName string `xml:"serviceName"`
MethodName string `xml:"methodName"`
Operations int64 `xml:"operations"`
ResponseTime int64 `xml:"responseTime"`
}
type soapRespBody struct {
Response []byte `xml:",innerxml"`
}
soapResp := struct {
XMLName xml.Name `xml:"http://schemas.xmlsoap.org/soap/envelope/ Envelope"`
Header soapRespHeader `xml:"Header>RequestHeader"`
Body soapRespBody `xml:"http://schemas.xmlsoap.org/soap/envelope/ Body"`
}{}
err = xml.Unmarshal([]byte(respBody), &soapResp)
if err != nil {
return respBody, err
}
if resp.StatusCode == 400 || resp.StatusCode == 401 || resp.StatusCode == 403 || resp.StatusCode == 405 || resp.StatusCode == 500 {
fault := Fault{}
err = xml.Unmarshal(soapResp.Body.Response, &fault)
if err != nil {
return respBody, err
}
return soapResp.Body.Response, &fault.Errors
}
return soapResp.Body.Response, err
}