-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnegative_keyword.go
117 lines (112 loc) · 5.45 KB
/
negative_keyword.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
package badsoap
import (
"encoding/xml"
)
func (a *Auth) GetNegativeKeywordsByCampaignIds(accountId int64, campaignIds []int64) (campaignIdsNegativeKeywords map[int64][]string, err error) {
getNegativeKeywordsByCampaignIdsRequest := struct {
XMLName xml.Name `xml:"https://adcenter.microsoft.com/v8 GetNegativeKeywordsByCampaignIdsRequest"`
AccountId int64 `xml:"https://adcenter.microsoft.com/v8 AccountId"`
CampaignIds []int64 `xml:"http://schemas.microsoft.com/2003/10/Serialization/Arrays CampaignIds>long"`
}{XMLNS, accountId, campaignIds}
reqBody, err := xml.MarshalIndent(SoapRequestEnvelope{ENVNS, a.authHeader("GetNegativeKeywordsByCampaignIds"), SoapRequestBody{getNegativeKeywordsByCampaignIdsRequest}}, " ", " ")
if err != nil {
return campaignIdsNegativeKeywords, err
}
soapRespBody, err := a.Request("GetNegativeKeywordsByCampaignIds", reqBody)
if err != nil {
return campaignIdsNegativeKeywords, err
}
type unmarshalCampaignNegativeKeywords struct {
CampaignId int64 `xml:"CampaignId"`
NegativeKeywords []string `xml:"NegativeKeywords>string"`
}
opResponse := struct {
CampaignNegativeKeywords []unmarshalCampaignNegativeKeywords `xml:"CampaignNegativeKeywords>CampaignNegativeKeywords"`
}{}
err = xml.Unmarshal(soapRespBody, &opResponse)
if err != nil {
return campaignIdsNegativeKeywords, err
}
campaignIdsNegativeKeywords = map[int64][]string{}
for _, ans := range opResponse.CampaignNegativeKeywords {
campaignIdsNegativeKeywords[ans.CampaignId] = ans.NegativeKeywords
}
return campaignIdsNegativeKeywords, err
}
func (a *Auth) SetNegativeKeywordsToCampaigns(accountId int64, campaignIdsNegativeKeywords map[int64][]string) error {
type negativeKeyword struct {
XMLName xml.Name `xml:"CampaignNegativeKeywords"`
CampaignId int64
NegativeKeywords []string `xml:"http://schemas.microsoft.com/2003/10/Serialization/Arrays NegativeKeywords>string"`
}
allNegativeKeywords := []negativeKeyword{}
for campaignId, negativeKeywords := range campaignIdsNegativeKeywords {
allNegativeKeywords = append(allNegativeKeywords, negativeKeyword{XMLNS, campaignId, negativeKeywords})
}
setNegativeKeywordsToCampaignsRequest := struct {
XMLName xml.Name `xml:"https://adcenter.microsoft.com/v8 SetNegativeKeywordsToCampaignsRequest"`
AccountId int64 `xml:"AccountId"`
Campaigns []negativeKeyword `xml:"CampaignNegativeKeywords>CampaignNegativeKeywords"`
}{XMLNS, accountId, allNegativeKeywords}
requestXml := SoapRequestEnvelope{ENVNS, a.authHeader("SetNegativeKeywordsToCampaigns"), SoapRequestBody{setNegativeKeywordsToCampaignsRequest}}
reqBody, err := xml.MarshalIndent(requestXml, " ", " ")
if err != nil {
return err
}
_, err = a.Request("SetNegativeKeywordsToCampaigns", reqBody)
return err
}
func (a *Auth) GetNegativeKeywordsByAdGroupIds(campaignId int64, adGroupIds []int64) (adGroupIdsNegativeKeywords map[int64][]string, err error) {
getNegativeKeywordsByAdGroupIdsRequest := struct {
XMLName xml.Name `xml:"https://adcenter.microsoft.com/v8 GetNegativeKeywordsByAdGroupIdsRequest"`
CampaignId int64 `xml:"https://adcenter.microsoft.com/v8 CampaignId"`
AdGroupIds []int64 `xml:"http://schemas.microsoft.com/2003/10/Serialization/Arrays AdGroupIds>long"`
}{XMLNS, campaignId, adGroupIds}
reqBody, err := xml.MarshalIndent(SoapRequestEnvelope{ENVNS, a.authHeader("GetNegativeKeywordsByAdGroupIds"), SoapRequestBody{getNegativeKeywordsByAdGroupIdsRequest}}, " ", " ")
if err != nil {
return adGroupIdsNegativeKeywords, err
}
soapRespBody, err := a.Request("GetNegativeKeywordsByAdGroupIds", reqBody)
if err != nil {
return adGroupIdsNegativeKeywords, err
}
type unmarshalAdGroupNegativeKeywords struct {
AdGroupId int64 `xml:"AdGroupId"`
NegativeKeywords []string `xml:"NegativeKeywords>string"`
}
opResponse := struct {
AdGroupNegativeKeywords []unmarshalAdGroupNegativeKeywords `xml:"AdGroupNegativeKeywords>AdGroupNegativeKeywords"`
}{}
err = xml.Unmarshal(soapRespBody, &opResponse)
if err != nil {
return adGroupIdsNegativeKeywords, err
}
adGroupIdsNegativeKeywords = map[int64][]string{}
for _, ans := range opResponse.AdGroupNegativeKeywords {
adGroupIdsNegativeKeywords[ans.AdGroupId] = ans.NegativeKeywords
}
return adGroupIdsNegativeKeywords, err
}
func (a *Auth) SetNegativeKeywordsToAdGroups(campaignId int64, adGroupIdsNegativeKeywords map[int64][]string) error {
type negativeKeyword struct {
XMLName xml.Name `xml:"AdGroupNegativeKeywords"`
AdGroupId int64
NegativeKeywords []string `xml:"http://schemas.microsoft.com/2003/10/Serialization/Arrays NegativeKeywords>string"`
}
allNegativeKeywords := []negativeKeyword{}
for adGroupId, negativeKeywords := range adGroupIdsNegativeKeywords {
allNegativeKeywords = append(allNegativeKeywords, negativeKeyword{XMLNS, adGroupId, negativeKeywords})
}
setNegativeKeywordsToAdGroupsRequest := struct {
XMLName xml.Name `xml:"https://adcenter.microsoft.com/v8 SetNegativeKeywordsToAdGroupsRequest"`
CampaignId int64 `xml:"CampaignId"`
AdGroups []negativeKeyword `xml:"AdGroupNegativeKeywords>AdGroupNegativeKeywords"`
}{XMLNS, campaignId, allNegativeKeywords}
requestXml := SoapRequestEnvelope{ENVNS, a.authHeader("SetNegativeKeywordsToAdGroups"), SoapRequestBody{setNegativeKeywordsToAdGroupsRequest}}
reqBody, err := xml.MarshalIndent(requestXml, " ", " ")
if err != nil {
return err
}
_, err = a.Request("SetNegativeKeywordsToAdGroups", reqBody)
return err
}