-
Notifications
You must be signed in to change notification settings - Fork 0
/
assets.go
128 lines (117 loc) · 2.94 KB
/
assets.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
package gadwords
import (
"encoding/base64"
"encoding/xml"
)
type AssetsService struct {
Auth
}
func NewAssetsService(auth *Auth) *AssetsService {
return &AssetsService{Auth: *auth}
}
// Media represents an audio, image or video file.
type Asset struct {
Type string `xml:"http://www.w3.org/2001/XMLSchema-instance type,attr"`
Id int64 `xml:"assetId,omitempty"`
AssetName string `xml:"assetName,omitempty"`
ImageData string `xml:"imageData,omitempty"`
AssetText string `xml:"assetText,omitempty"`
YouTubeVideoId string `xml:"youTubeVideoId,omitempty"`
FullSizeInfo *FullSizeInfo `xml:"fullSizeInfo,omitempty"`
}
type FullSizeInfo struct {
ImageHeight int64 `xml:"imageHeight,omitempty"`
ImageWidth int64 `xml:"imageWidth,omitempty"`
ImageUrl string `xml:"imageUrl,omitempty"`
}
func NewTextAsset(text string) Asset {
return Asset{
Type: "TextAsset",
AssetText: text,
}
}
func NewImageAsset(name string, data []byte) Asset {
imageData := base64.StdEncoding.EncodeToString(data)
return Asset{
Type: "ImageAsset",
AssetName: name,
ImageData: imageData,
}
}
func NewYouTubeVideoAsset(name string, youTubeVideoId string) Asset {
return Asset{
Type: "YouTubeVideoAsset",
AssetName: name,
YouTubeVideoId: youTubeVideoId,
}
}
func (s *AssetsService) Get(selector Selector) (assets []Asset, totalCount int64, err error) {
selector.XMLName = xml.Name{baseUrl, "selector"}
respBody, err := s.Auth.request(
assetServiceUrl,
"get",
struct {
XMLName xml.Name
Sel Selector
}{
XMLName: xml.Name{
Space: baseUrl,
Local: "get",
},
Sel: selector,
},
)
if err != nil {
return assets, totalCount, err
}
getResp := struct {
Size int64 `xml:"rval>totalNumEntries"`
Assets []Asset `xml:"rval>entries"`
}{}
err = xml.Unmarshal([]byte(respBody), &getResp)
if err != nil {
return assets, totalCount, err
}
return getResp.Assets, getResp.Size, err
}
type AssetOperations map[string][]Asset
func (s *AssetsService) Mutate(assetOperations AssetOperations) (assets []Asset, err error) {
type assetOperation struct {
Action string `xml:"operator"`
Assets []Asset `xml:"operand"`
}
operations := []assetOperation{}
for action, assets := range assetOperations {
for _, asset := range assets {
ad := []Asset{asset}
operations = append(operations,
assetOperation{
Action: action,
Assets: ad,
},
)
}
}
mutation := struct {
XMLName xml.Name
Ops []assetOperation `xml:"operations"`
}{
XMLName: xml.Name{
Space: baseUrl,
Local: "mutate",
},
Ops: operations,
}
respBody, err := s.Auth.request(assetServiceUrl, "mutate", mutation)
if err != nil {
return assets, err
}
mutateResp := struct {
Assets []Asset `xml:"rval>value"`
}{}
err = xml.Unmarshal([]byte(respBody), &mutateResp)
if err != nil {
return assets, err
}
return mutateResp.Assets, err
}