-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathad.go
217 lines (202 loc) · 7.86 KB
/
ad.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
package badsoap
import (
"encoding/xml"
)
// AdType: "MobileAd","TextAd","ProductAd"
// EditorialStatus: "Active","Disapproved","Inactive"
// Status: "Active","Deleted","Inactive","Paused"
// Type: "Image","Mobile","Product","RichSearch","Text"
type Ad struct {
XMLName xml.Name `xml:"https://adcenter.microsoft.com/v8 Ad"`
AdType string `xml:"http://www.w3.org/2001/XMLSchema-instance type,attr"`
EditorialStatus string `xml:"https://adcenter.microsoft.com/v8 EditorialStatus,omitempty"`
Id int64 `xml:"https://adcenter.microsoft.com/v8 Id,omitempty"`
Status string `xml:"https://adcenter.microsoft.com/v8 Status,omitempty"`
Type string `xml:"https://adcenter.microsoft.com/v8 Type"`
BusinessName string `xml:"https://adcenter.microsoft.com/v8 BusinessName,omitempty"`
DestinationUrl string `xml:"https://adcenter.microsoft.com/v8 DestinationUrl,omitempty"`
DisplayUrl string `xml:"https://adcenter.microsoft.com/v8 DisplayUrl,omitempty"`
PhoneNumber string `xml:"https://adcenter.microsoft.com/v8 PhoneNumber,omitempty"`
Text string `xml:"https://adcenter.microsoft.com/v8 Text,omitempty"`
Title string `xml:"https://adcenter.microsoft.com/v8 Title,omitempty"`
PromotionalText string `xml:"https://adcenter.microsoft.com/v8 PromotionalText,omitempty"`
}
func NewTextAd(title, text, destinationUrl, displayUrl, status string) Ad {
return Ad{
AdType: "TextAd",
Type: "Text",
Title: title,
Text: text,
DestinationUrl: destinationUrl,
DisplayUrl: displayUrl,
Status: status,
}
}
func NewMobileAd(title, text, businessName, destinationUrl, displayUrl, phoneNumber, status string) Ad {
return Ad{
AdType: "MobileAd",
Type: "Mobile",
Title: title,
Text: text,
BusinessName: businessName,
DestinationUrl: destinationUrl,
DisplayUrl: displayUrl,
PhoneNumber: phoneNumber,
Status: status,
}
}
func NewProductAd(promotionalText, status string) Ad {
return Ad{
AdType: "ProductAd",
Type: "Product",
PromotionalText: promotionalText,
Status: status,
}
}
func unmarshalAds(xmlBytes []byte) (ads []Ad, err error) {
opResponse := struct {
Ads []Ad `xml:"Ads>Ad"`
}{}
err = xml.Unmarshal(xmlBytes, &opResponse)
if err != nil {
return ads, err
}
return opResponse.Ads, err
}
func unmarshalAdIds(xmlBytes []byte) (adIds []int64, err error) {
opResponse := struct {
AdIds []int64 `xml:"AdIds>long"`
}{}
err = xml.Unmarshal(xmlBytes, &opResponse)
if err != nil {
return adIds, err
}
return opResponse.AdIds, err
}
func (a *Auth) AddAds(adGroupId int64, ads []Ad) (adIds []int64, err error) {
as := []Ad{}
for _, a := range ads {
a.EditorialStatus = ""
a.Id = 0
as = append(as, a)
}
addAdsRequest := struct {
XMLName xml.Name `xml:"https://adcenter.microsoft.com/v8 AddAdsRequest"`
AdGroupId int64 `xml:"https://adcenter.microsoft.com/v8 AdGroupId"`
Ads []Ad `xml:"https://adcenter.microsoft.com/v8 Ads>Ad"`
}{XMLNS, adGroupId, as}
reqBody, err := xml.MarshalIndent(SoapRequestEnvelope{ENVNS, a.authHeader("AddAds"), SoapRequestBody{addAdsRequest}}, " ", " ")
if err != nil {
return adIds, err
}
soapRespBody, err := a.Request("AddAds", reqBody)
if err != nil {
return adIds, err
}
return unmarshalAdIds(soapRespBody)
}
func (a *Auth) DeleteAds(adGroupId int64, adIds []int64) error {
deleteAdsRequest := struct {
XMLName xml.Name `xml:"https://adcenter.microsoft.com/v8 DeleteAdsRequest"`
AdGroupId int64 `xml:"https://adcenter.microsoft.com/v8 AdGroupId"`
AdIds []int64 `xml:"http://schemas.microsoft.com/2003/10/Serialization/Arrays AdIds>long"`
}{XMLNS, adGroupId, adIds}
reqBody, err := xml.MarshalIndent(SoapRequestEnvelope{ENVNS, a.authHeader("DeleteAds"), SoapRequestBody{deleteAdsRequest}}, " ", " ")
if err != nil {
return err
}
_, err = a.Request("DeleteAds", reqBody)
return err
}
func (a *Auth) GetAdsByAdGroupId(adGroupId int64) (ads []Ad, err error) {
getAdsByAdGroupIdRequest := struct {
XMLName xml.Name `xml:"https://adcenter.microsoft.com/v8 GetAdsByAdGroupIdRequest"`
AdGroupId int64 `xml:"https://adcenter.microsoft.com/v8 AdGroupId"`
}{XMLNS, adGroupId}
reqBody, err := xml.MarshalIndent(SoapRequestEnvelope{ENVNS, a.authHeader("GetAdsByAdGroupId"), SoapRequestBody{getAdsByAdGroupIdRequest}}, " ", " ")
if err != nil {
return ads, err
}
soapRespBody, err := a.Request("GetAdsByAdGroupId", reqBody)
if err != nil {
return ads, err
}
return unmarshalAds(soapRespBody)
}
func (a *Auth) GetAdsByEditorialStatus(adGroupId int64, editorialStatus string) (ads []Ad, err error) {
getAdsByEditorialStatusRequest := struct {
XMLName xml.Name `xml:"https://adcenter.microsoft.com/v8 GetAdsByEditorialStatusRequest"`
AdGroupId int64 `xml:"https://adcenter.microsoft.com/v8 AdGroupId"`
EditorialStatus string `xml:"https://adcenter.microsoft.com/v8 EditorialStatus"`
}{XMLNS, adGroupId, editorialStatus}
reqBody, err := xml.MarshalIndent(SoapRequestEnvelope{ENVNS, a.authHeader("GetAdsByEditorialStatus"), SoapRequestBody{getAdsByEditorialStatusRequest}}, " ", " ")
if err != nil {
return ads, err
}
soapRespBody, err := a.Request("GetAdsByEditorialStatus", reqBody)
if err != nil {
return ads, err
}
return unmarshalAds(soapRespBody)
}
func (a *Auth) GetAdsByIds(adGroupId int64, adIds []int64) (ads []Ad, err error) {
getAdsByIdsRequest := struct {
XMLName xml.Name `xml:"https://adcenter.microsoft.com/v8 GetAdsByIdsRequest"`
AdGroupId int64 `xml:"https://adcenter.microsoft.com/v8 AdGroupId"`
AdIds []int64 `xml:"http://schemas.microsoft.com/2003/10/Serialization/Arrays AdIds>long"`
}{XMLNS, adGroupId, adIds}
reqBody, err := xml.MarshalIndent(SoapRequestEnvelope{ENVNS, a.authHeader("GetAdsByIds"), SoapRequestBody{getAdsByIdsRequest}}, " ", " ")
if err != nil {
return ads, err
}
soapRespBody, err := a.Request("GetAdsByIds", reqBody)
if err != nil {
return ads, err
}
return unmarshalAds(soapRespBody)
}
func (a *Auth) PauseAds(adGroupId int64, adIds []int64) error {
pauseAdsRequest := struct {
XMLName xml.Name `xml:"https://adcenter.microsoft.com/v8 PauseAdsRequest"`
AdGroupId int64 `xml:"https://adcenter.microsoft.com/v8 AdGroupId"`
AdIds []int64 `xml:"http://schemas.microsoft.com/2003/10/Serialization/Arrays AdIds>long"`
}{XMLNS, adGroupId, adIds}
reqBody, err := xml.MarshalIndent(SoapRequestEnvelope{ENVNS, a.authHeader("PauseAds"), SoapRequestBody{pauseAdsRequest}}, " ", " ")
if err != nil {
return err
}
_, err = a.Request("PauseAds", reqBody)
return err
}
func (a *Auth) ResumeAds(adGroupId int64, adIds []int64) error {
resumeAdsRequest := struct {
XMLName xml.Name `xml:"https://adcenter.microsoft.com/v8 ResumeAdsRequest"`
AdGroupId int64 `xml:"https://adcenter.microsoft.com/v8 AdGroupId"`
AdIds []int64 `xml:"http://schemas.microsoft.com/2003/10/Serialization/Arrays AdIds>long"`
}{XMLNS, adGroupId, adIds}
reqBody, err := xml.MarshalIndent(SoapRequestEnvelope{ENVNS, a.authHeader("ResumeAds"), SoapRequestBody{resumeAdsRequest}}, " ", " ")
if err != nil {
return err
}
_, err = a.Request("ResumeAds", reqBody)
return err
}
func (a *Auth) UpdateAds(adGroupId int64, ads []Ad) error {
as := []Ad{}
for _, a := range ads {
a.EditorialStatus = ""
a.Status = ""
as = append(as, a)
}
updateAdsRequest := struct {
XMLName xml.Name `xml:"https://adcenter.microsoft.com/v8 UpdateAdsRequest"`
AdGroupId int64 `xml:"https://adcenter.microsoft.com/v8 AdGroupId"`
Ads []Ad `xml:"https://adcenter.microsoft.com/v8 Ads>Ad"`
}{XMLNS, adGroupId, as}
reqBody, err := xml.MarshalIndent(SoapRequestEnvelope{ENVNS, a.authHeader("UpdateAds"), SoapRequestBody{updateAdsRequest}}, " ", " ")
if err != nil {
return err
}
_, err = a.Request("UpdateAds", reqBody)
return err
}