-
Notifications
You must be signed in to change notification settings - Fork 3
/
vod_callback_service.go
205 lines (194 loc) · 5.48 KB
/
vod_callback_service.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
// Code generated by protoc-gen-byteplus-sdk
// source: VodCallbackService
// DO NOT EDIT!
package vod
import (
"encoding/json"
"net/http"
"net/url"
"strconv"
"google.golang.org/protobuf/encoding/protojson"
"github.com/pkg/errors"
"github.com/byteplus-sdk/byteplus-sdk-golang/service/vod/models/request"
"github.com/byteplus-sdk/byteplus-sdk-golang/service/vod/models/response"
)
// AddCallbackSubscription
/*
* @param *request.VodAddCallbackSubscriptionRequest
* @return *response.VodAddCallbackSubscriptionResponse, int, error
*/
func (p *Vod) AddCallbackSubscription(req *request.VodAddCallbackSubscriptionRequest) (*response.VodAddCallbackSubscriptionResponse, int, error) {
query := url.Values{}
form := url.Values{}
marshaler := protojson.MarshalOptions{
Multiline: false,
AllowPartial: false,
UseProtoNames: true,
UseEnumNumbers: false,
EmitUnpopulated: false,
}
jsonData := marshaler.Format(req)
reqMap := map[string]interface{}{}
err := json.Unmarshal([]byte(jsonData), &reqMap)
if err != nil {
return nil, 0, err
}
for k, v := range reqMap {
var sv string
switch ov := v.(type) {
case string:
sv = ov
case int:
sv = strconv.FormatInt(int64(ov), 10)
case uint:
sv = strconv.FormatUint(uint64(ov), 10)
case int8:
sv = strconv.FormatInt(int64(ov), 10)
case uint8:
sv = strconv.FormatUint(uint64(ov), 10)
case int16:
sv = strconv.FormatInt(int64(ov), 10)
case uint16:
sv = strconv.FormatUint(uint64(ov), 10)
case int32:
sv = strconv.FormatInt(int64(ov), 10)
case uint32:
sv = strconv.FormatUint(uint64(ov), 10)
case int64:
sv = strconv.FormatInt(ov, 10)
case uint64:
sv = strconv.FormatUint(ov, 10)
case bool:
sv = strconv.FormatBool(ov)
case float32:
sv = strconv.FormatFloat(float64(ov), 'f', -1, 32)
case float64:
sv = strconv.FormatFloat(ov, 'f', -1, 64)
case []byte:
sv = string(ov)
default:
v2, e2 := json.Marshal(ov)
if e2 != nil {
return nil, 0, e2
}
sv = string(v2)
}
query.Set(k, sv)
form.Add(k, sv)
}
respBody, status, err := p.Query("AddCallbackSubscription", query)
output := &response.VodAddCallbackSubscriptionResponse{}
unmarshaler := protojson.UnmarshalOptions{
DiscardUnknown: true,
}
errUnmarshal := unmarshaler.Unmarshal(respBody, output)
if err != nil || status != http.StatusOK {
// if exist http err,check whether the respBody's type is defined struct,
// if it is ,
// return struct,
// otherwise return nil body
// if httpCode is not 200,check whether the respBody's type is defined struct,
// if it is ,
// use errorCode as err and return struct,
// otherwise use respBody string as error and return
if errUnmarshal != nil || len(output.GetResponseMetadata().GetError().GetCode()) == 0 {
if err == nil {
err = errors.New(string(respBody))
}
return nil, status, err
} else {
return output, status, errors.New(output.GetResponseMetadata().GetError().GetCode())
}
}
return output, status, nil
}
// SetCallbackEvent
/*
* @param *request.VodSetCallbackEventRequest
* @return *response.VodSetCallbackEventResponse, int, error
*/
func (p *Vod) SetCallbackEvent(req *request.VodSetCallbackEventRequest) (*response.VodSetCallbackEventResponse, int, error) {
query := url.Values{}
form := url.Values{}
marshaler := protojson.MarshalOptions{
Multiline: false,
AllowPartial: false,
UseProtoNames: true,
UseEnumNumbers: false,
EmitUnpopulated: false,
}
jsonData := marshaler.Format(req)
reqMap := map[string]interface{}{}
err := json.Unmarshal([]byte(jsonData), &reqMap)
if err != nil {
return nil, 0, err
}
for k, v := range reqMap {
var sv string
switch ov := v.(type) {
case string:
sv = ov
case int:
sv = strconv.FormatInt(int64(ov), 10)
case uint:
sv = strconv.FormatUint(uint64(ov), 10)
case int8:
sv = strconv.FormatInt(int64(ov), 10)
case uint8:
sv = strconv.FormatUint(uint64(ov), 10)
case int16:
sv = strconv.FormatInt(int64(ov), 10)
case uint16:
sv = strconv.FormatUint(uint64(ov), 10)
case int32:
sv = strconv.FormatInt(int64(ov), 10)
case uint32:
sv = strconv.FormatUint(uint64(ov), 10)
case int64:
sv = strconv.FormatInt(ov, 10)
case uint64:
sv = strconv.FormatUint(ov, 10)
case bool:
sv = strconv.FormatBool(ov)
case float32:
sv = strconv.FormatFloat(float64(ov), 'f', -1, 32)
case float64:
sv = strconv.FormatFloat(ov, 'f', -1, 64)
case []byte:
sv = string(ov)
default:
v2, e2 := json.Marshal(ov)
if e2 != nil {
return nil, 0, e2
}
sv = string(v2)
}
query.Set(k, sv)
form.Add(k, sv)
}
respBody, status, err := p.Query("SetCallbackEvent", query)
output := &response.VodSetCallbackEventResponse{}
unmarshaler := protojson.UnmarshalOptions{
DiscardUnknown: true,
}
errUnmarshal := unmarshaler.Unmarshal(respBody, output)
if err != nil || status != http.StatusOK {
// if exist http err,check whether the respBody's type is defined struct,
// if it is ,
// return struct,
// otherwise return nil body
// if httpCode is not 200,check whether the respBody's type is defined struct,
// if it is ,
// use errorCode as err and return struct,
// otherwise use respBody string as error and return
if errUnmarshal != nil || len(output.GetResponseMetadata().GetError().GetCode()) == 0 {
if err == nil {
err = errors.New(string(respBody))
}
return nil, status, err
} else {
return output, status, errors.New(output.GetResponseMetadata().GetError().GetCode())
}
}
return output, status, nil
}