forked from emiddleton/gads
-
Notifications
You must be signed in to change notification settings - Fork 1
/
budget.go
118 lines (108 loc) · 3.16 KB
/
budget.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
package gads
import (
"encoding/xml"
// "fmt"
)
// A budgetService holds the connection information for the
// budget service.
type BudgetService struct {
Auth
}
// NewBudgetService creates a new budgetService
func NewBudgetService(auth *Auth) *BudgetService {
return &BudgetService{Auth: *auth}
}
// A Budget represents an allotment of money to be spent over a fixed
// period of time.
type Budget struct {
Id int64 `xml:"budgetId,omitempty"` // A unique identifier
Name string `xml:"name"` // A descriptive name
Amount int64 `xml:"amount>microAmount"` // The amount in cents
Delivery string `xml:"deliveryMethod"` // The rate at which the budget spent. valid options are STANDARD or ACCELERATED.
References int64 `xml:"referenceCount,omitempty"` // The number of campaigns using the budget
Shared bool `xml:"isExplicitlyShared,omitempty"` // If this budget was created to be shared across campaigns
Status string `xml:"status,omitempty"` // The status of the budget. can be ENABLED, REMOVED, UNKNOWN
}
// A BudgetOperations maps operations to the budgets they will be performed
// on. Budgets operations can be 'ADD', 'REMOVE' or 'SET'
type BudgetOperations map[string][]Budget
func budgetError() (err error) {
return err
}
// Get returns budgets matching a given selector and the total count of matching budgets.
func (s *BudgetService) Get(selector Selector) (budgets []Budget, totalCount int64, err error) {
selector.XMLName = xml.Name{"", "selector"}
respBody, err := s.Auth.request(
budgetServiceUrl,
"get",
struct {
XMLName xml.Name
Sel Selector
}{
XMLName: xml.Name{
Space: baseUrl,
Local: "get",
},
Sel: selector,
},
)
if err != nil {
return budgets, totalCount, err
}
getResp := struct {
Size int64 `xml:"rval>totalNumEntries"`
Budgets []Budget `xml:"rval>entries"`
}{}
err = xml.Unmarshal([]byte(respBody), &getResp)
if err != nil {
return budgets, totalCount, err
}
return getResp.Budgets, getResp.Size, err
}
// Mutate takes a budgetOperations and creates, modifies or destroys the associated budgets.
func (s *BudgetService) Mutate(budgetOperations BudgetOperations) (budgets []Budget, err error) {
type budgetOperation struct {
Action string `xml:"operator"`
Budget Budget `xml:"operand"`
}
operations := []budgetOperation{}
for action, budgets := range budgetOperations {
for _, budget := range budgets {
operations = append(operations,
budgetOperation{
Action: action,
Budget: budget,
},
)
}
}
respBody, err := s.Auth.request(
budgetServiceUrl,
"mutate",
struct {
XMLName xml.Name
Ops []budgetOperation `xml:"operations"`
}{
XMLName: xml.Name{
Space: baseUrl,
Local: "mutate",
},
Ops: operations,
},
)
if err != nil {
return budgets, err
}
mutateResp := struct {
BaseResponse
Budgets []Budget `xml:"rval>value"`
}{}
err = xml.Unmarshal([]byte(respBody), &mutateResp)
if err != nil {
return budgets, err
}
if len(mutateResp.PartialFailureErrors) > 0 {
err = mutateResp.PartialFailureErrors
}
return mutateResp.Budgets, err
}