-
Notifications
You must be signed in to change notification settings - Fork 1
/
dmp.go
333 lines (273 loc) · 9.52 KB
/
dmp.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package OceanEngineApi
import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"github.com/MrNullPoint/OceanEngineApi/pb"
"github.com/golang/protobuf/proto"
"os"
"path/filepath"
"strconv"
)
const (
ApiDmpDataSourceFileUpload = ApiUrlPrefix + ApiVersion + "/dmp/data_source/file/upload/"
ApiDmpDataSourceCreate = ApiUrlPrefix + ApiVersion + "/dmp/data_source/create/"
ApiDmpDataSourceUpdate = ApiUrlPrefix + ApiVersion + "/dmp/data_source/update/"
ApiDmpDataSourceDetail = ApiUrlPrefix + ApiVersion + "/dmp/data_source/read/"
ApiDmpAudiencePublish = ApiUrlPrefix + ApiVersion + "/dmp/custom_audience/publish/"
ApiDmpAudiencePush = ApiUrlPrefix + ApiVersion + "/dmp/custom_audience/push_v2/"
)
// @function: 构建 DMP 所需要的上传的 zip 文件
// @params<path>: zip 文件生成的路径
// @params<name>: 可以指定 zip 文件名
func (api *OceanEngineApi) DataSourceFileCompose(data []*pb.DmpData, path string) (string, error) {
name := api.randomString(32)
binPath := filepath.Join(path, name)
zipPath := filepath.Join(path, name+".zip")
file, err := os.OpenFile(binPath, os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
return "", err
}
defer file.Close()
for _, d := range data {
b, err := proto.Marshal(d)
if err != nil {
return "", err
}
s := base64.StdEncoding.EncodeToString(b)
if _, err := file.WriteString(s + "\n"); err != nil {
return "", err
}
}
if err := api.zip(binPath, zipPath); err != nil {
return "", err
}
if err := os.Remove(binPath); err != nil {
return "", err
}
return zipPath, nil
}
type DataSourceFileUploadResp struct {
OceanEngineResp
Data struct {
FilePath string `json:"file_path"`
} `json:"data"`
}
// @function: 数据源文件上传
// @reference: https://ad.oceanengine.com/openapi/doc/index.html?id=501
func (api *OceanEngineApi) DataSourceFileUpload(file string, advertiserId int) (*DataSourceFileUploadResp, error) {
params := make(map[string]string)
params["advertiser_id"] = strconv.Itoa(advertiserId)
files := make(map[string]string)
files["file"] = file
body, contentType, err := api.formCompose(params, files)
if err != nil {
return nil, err
}
req, err := api.NewRequest("POST", ApiDmpDataSourceFileUpload, contentType, body)
if err != nil {
return nil, err
}
resp := new(DataSourceFileUploadResp)
if err := resp.doRequest(api, req, resp); err != nil {
return nil, err
}
return resp, nil
}
type DataSourceCreateResp struct {
OceanEngineResp
Data struct {
DataSourceId string `json:"data_source_id"`
} `json:"data"`
}
// @function: 数据源创建
// @reference: https://ad.oceanengine.com/openapi/doc/index.html?id=502
func (api *OceanEngineApi) DataSourceCreate(advertiserId int, dataSourceName string, dataSourceType string,
desc string, format int, storageType int, paths []string) (*DataSourceCreateResp, error) {
// 默认投放数据源类型为 UID
if dataSourceType == "" {
dataSourceType = "UID"
}
if dataSourceName == "" || len(dataSourceName) >= 30 || len(desc) >= 256 ||
len(paths) >= 1000 || len(paths) == 0 || (dataSourceType != "UID" && dataSourceType != "DID") {
return nil, errors.New("data source create params check failed")
}
params := make(map[string]interface{})
params["advertiser_id"] = advertiserId
params["data_source_name"] = dataSourceName
params["description"] = desc
params["file_paths"] = paths
params["data_format"] = 0
params["file_storage_type"] = 0
if format != 0 {
params["data_format"] = format
}
if storageType != 0 {
params["file_storage_type"] = storageType
}
body, err := json.Marshal(params)
if err != nil {
return nil, err
}
req, err := api.NewRequest("POST", ApiDmpDataSourceCreate, ContentTypeJson, bytes.NewBuffer(body))
if err != nil {
return nil, err
}
resp := new(DataSourceCreateResp)
if err := resp.doRequest(api, req, resp); err != nil {
return nil, err
}
return resp, nil
}
type DataSourceUpdateResp struct {
OceanEngineResp
Data struct{} `json:"data"`
}
// @function: 数据源更新
// @reference: https://ad.oceanengine.com/openapi/doc/index.html?id=504
func (api *OceanEngineApi) DataSourceUpdate(advertiserId int, dataSourceId string, operationType int,
format int, storageType int, paths []string) (*DataSourceUpdateResp, error) {
if dataSourceId == "" || (operationType != 1 && operationType != 2 && operationType != 3) ||
len(paths) >= 200 || len(paths) == 0 {
return nil, errors.New("data source update params check failed")
}
params := make(map[string]interface{})
params["advertiser_id"] = advertiserId
params["data_source_id"] = dataSourceId
params["operation_type"] = operationType
params["file_paths"] = paths
params["data_format"] = 0
params["file_storage_type"] = 0
if format != 0 {
params["data_format"] = format
}
if storageType != 0 {
params["file_storage_type"] = storageType
}
body, err := json.Marshal(params)
if err != nil {
return nil, err
}
req, err := api.NewRequest("POST", ApiDmpDataSourceUpdate, ContentTypeJson, bytes.NewBuffer(body))
if err != nil {
return nil, err
}
resp := new(DataSourceUpdateResp)
if err := resp.doRequest(api, req, resp); err != nil {
return nil, err
}
return resp, nil
}
type DataSourceDetailResp struct {
OceanEngineResp
Data struct {
DataList []struct {
Name string `json:"name"`
DataSourceId string `json:"data_source_id"`
Description string `json:"description"`
Status int `json:"status"`
CoverNum int64 `json:"cover_num"`
UploadNum int64 `json:"upload_num"`
CreateTime string `json:"create_time"`
ModifyTime string `json:"modify_time"`
LatestPublishedChangeLogId int `json:"latest_published_change_log_id"`
LatestPublishedTime string `json:"latest_published_time"`
DataSourceType string `json:"data_source_type"`
ChangeLogs []map[string]interface{} `json:"change_logs"`
DefaultAudience struct {
AdvertiserId int `json:"advertiser_id"`
CustomAudienceId int `json:"custom_audience_id"`
Name string `json:"name"`
CustomType int `json:"custom_type"`
Source int `json:"source"`
Status int `json:"status"`
PushStatus int `json:"push_status"`
UploadNum int64 `json:"upload_num"`
CoverNum int64 `json:"cover_num"`
ExpiryDate string `json:"expiry_date"`
CreateTime string `json:"create_time"`
ModifyTime string `json:"modify_time"`
Isdel int `json:"isdel"`
CalculateSubType int `json:"calculate_sub_type"`
CalculateType int `json:"calculate_type"`
DataSourceId string `json:"data_source_id"`
Tag string `json:"tag"`
ThirdPartyInfo string `json:"third_party_info"`
DeliveryStatus string `json:"delivery_status"`
} `json:"default_audience"`
} `json:"data_list"`
} `json:"data"`
}
// @function: 数据源详细信息
// @reference: https://ad.oceanengine.com/openapi/doc/index.html?id=505
func (api *OceanEngineApi) DataSourceDetail(advertiserId int, dataSourceIds []string) (*DataSourceDetailResp, error) {
if len(dataSourceIds) == 0 || len(dataSourceIds) >= 400 {
return nil, errors.New("data source detail params check failed")
}
params := make(map[string]interface{})
params["advertiser_id"] = advertiserId
params["data_source_id_list"] = dataSourceIds
body, err := json.Marshal(params)
if err != nil {
return nil, err
}
req, err := api.NewRequest("GET", ApiDmpDataSourceDetail, ContentTypeJson, bytes.NewBuffer(body))
if err != nil {
return nil, err
}
resp := new(DataSourceDetailResp)
if err := resp.doRequest(api, req, resp); err != nil {
return nil, err
}
return resp, nil
}
type AudiencePublishResp struct {
OceanEngineResp
Data struct{} `json:"data"`
}
// @function: 发布人群包
// @reference: https://ad.oceanengine.com/openapi/doc/index.html?id=508
func (api *OceanEngineApi) AudiencePublish(advertiserId int, audienceId int) (*AudiencePublishResp, error) {
params := make(map[string]interface{})
params["advertiser_id"] = advertiserId
params["custom_audience_id"] = audienceId
body, err := json.Marshal(params)
if err != nil {
return nil, err
}
req, err := api.NewRequest("POST", ApiDmpAudiencePublish, ContentTypeJson, bytes.NewBuffer(body))
if err != nil {
return nil, err
}
resp := new(AudiencePublishResp)
if err := resp.doRequest(api, req, resp); err != nil {
return nil, err
}
return resp, nil
}
type AudiencePushResp struct {
OceanEngineResp
Data struct{} `json:"data"`
}
// @function: 推送人群包
// @reference: https://ad.oceanengine.com/openapi/doc/index.html?id=509
func (api *OceanEngineApi) AudiencePush(advertiserId int, audienceId int, targetAdvertiserIds []int) (*AudiencePushResp, error) {
params := make(map[string]interface{})
params["advertiser_id"] = advertiserId
params["custom_audience_id"] = audienceId
params["target_advertiser_ids"] = targetAdvertiserIds
body, err := json.Marshal(params)
if err != nil {
return nil, err
}
req, err := api.NewRequest("POST", ApiDmpAudiencePush, ContentTypeJson, bytes.NewBuffer(body))
if err != nil {
return nil, err
}
resp := new(AudiencePushResp)
if err := resp.doRequest(api, req, resp); err != nil {
return nil, err
}
return resp, nil
}