minidrama.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. * Copyright silenceper/wechat Author(https://silenceper.com/wechat/). All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. * You can obtain one at https://github.com/silenceper/wechat.
  17. *
  18. */
  19. package minidrama
  20. import (
  21. "context"
  22. "strconv"
  23. "github.com/silenceper/wechat/v2/util"
  24. )
  25. // SingleFileUpload 单文件上传
  26. func (s *MiniDrama) SingleFileUpload(ctx context.Context, in *SingleFileUploadRequest) (out SingleFileUploadResponse, err error) {
  27. var address string
  28. if address, err = s.requestAddress(ctx, singleFileUpload); err != nil {
  29. return
  30. }
  31. var (
  32. fields = []util.MultipartFormField{
  33. {
  34. IsFile: true,
  35. Fieldname: "media_data",
  36. Filename: string(in.MediaData),
  37. }, {
  38. IsFile: false,
  39. Fieldname: "media_name",
  40. Value: []byte(in.MediaName),
  41. }, {
  42. IsFile: false,
  43. Fieldname: "media_type",
  44. Value: []byte(in.MediaType),
  45. },
  46. }
  47. response []byte
  48. )
  49. if in.CoverType != "" && in.CoverData != nil {
  50. fields = append(fields, util.MultipartFormField{
  51. IsFile: false,
  52. Fieldname: "cover_type",
  53. Value: []byte(in.CoverType),
  54. })
  55. fields = append(fields, util.MultipartFormField{
  56. IsFile: true,
  57. Fieldname: "cover_data",
  58. Filename: string(in.CoverData),
  59. })
  60. }
  61. if in.SourceContext != "" {
  62. fields = append(fields, util.MultipartFormField{
  63. IsFile: false,
  64. Fieldname: "source_context",
  65. Value: []byte(in.SourceContext),
  66. })
  67. }
  68. if response, err = util.PostMultipartForm(fields, address); err != nil {
  69. return
  70. }
  71. // 使用通用方法返回错误
  72. err = util.DecodeWithError(response, &out, "SingleFileUpload")
  73. return
  74. }
  75. // PullUpload 拉取上传
  76. func (s *MiniDrama) PullUpload(ctx context.Context, in *PullUploadRequest) (out PullUploadResponse, err error) {
  77. var address string
  78. if address, err = s.requestAddress(ctx, pullUpload); err != nil {
  79. return
  80. }
  81. var response []byte
  82. if response, err = util.PostJSONContext(ctx, address, in); err != nil {
  83. return
  84. }
  85. // 使用通用方法返回错误
  86. err = util.DecodeWithError(response, &out, "PullUpload")
  87. return
  88. }
  89. // GetTask 查询任务状态
  90. func (s *MiniDrama) GetTask(ctx context.Context, in *GetTaskRequest) (out GetTaskResponse, err error) {
  91. var address string
  92. if address, err = s.requestAddress(ctx, getTask); err != nil {
  93. return
  94. }
  95. var response []byte
  96. if response, err = util.PostJSONContext(ctx, address, in); err != nil {
  97. return
  98. }
  99. // 使用通用方法返回错误
  100. err = util.DecodeWithError(response, &out, "GetTask")
  101. return
  102. }
  103. // ApplyUpload 申请分片上传
  104. func (s *MiniDrama) ApplyUpload(ctx context.Context, in *ApplyUploadRequest) (out ApplyUploadResponse, err error) {
  105. var address string
  106. if address, err = s.requestAddress(ctx, applyUpload); err != nil {
  107. return
  108. }
  109. var response []byte
  110. if response, err = util.PostJSONContext(ctx, address, in); err != nil {
  111. return
  112. }
  113. // 使用通用方法返回错误
  114. err = util.DecodeWithError(response, &out, "ApplyUpload")
  115. return
  116. }
  117. // UploadPart 上传分片
  118. // Content-Type 需要指定为 multipart/form-data; boundary=<delimiter>,<箭头括号>表示必须替换为有效值的变量。
  119. func (s *MiniDrama) UploadPart(ctx context.Context, in *UploadPartRequest) (out UploadPartResponse, err error) {
  120. var address string
  121. if address, err = s.requestAddress(ctx, uploadPart); err != nil {
  122. return
  123. }
  124. var (
  125. fields = []util.MultipartFormField{
  126. {
  127. IsFile: true,
  128. Fieldname: "data",
  129. Filename: string(in.Data),
  130. }, {
  131. IsFile: false,
  132. Fieldname: "upload_id",
  133. Value: []byte(in.UploadID),
  134. }, {
  135. IsFile: false,
  136. Fieldname: "part_number",
  137. Value: []byte(strconv.Itoa(in.PartNumber)),
  138. }, {
  139. IsFile: false,
  140. Fieldname: "resource_type",
  141. Value: []byte(strconv.Itoa(in.PartNumber)),
  142. },
  143. }
  144. response []byte
  145. )
  146. if response, err = util.PostMultipartForm(fields, address); err != nil {
  147. return
  148. }
  149. // 使用通用方法返回错误
  150. err = util.DecodeWithError(response, &out, "UploadPart")
  151. return
  152. }
  153. // CommitUpload 确认上传
  154. func (s *MiniDrama) CommitUpload(ctx context.Context, in *CommitUploadRequest) (out CommitUploadResponse, err error) {
  155. var address string
  156. if address, err = s.requestAddress(ctx, commitUpload); err != nil {
  157. return
  158. }
  159. var response []byte
  160. if response, err = util.PostJSONContext(ctx, address, in); err != nil {
  161. return
  162. }
  163. // 使用通用方法返回错误
  164. err = util.DecodeWithError(response, &out, "CommitUpload")
  165. return
  166. }
  167. // ListMedia 获取媒体列表
  168. func (s *MiniDrama) ListMedia(ctx context.Context, in *ListMediaRequest) (out ListMediaResponse, err error) {
  169. var address string
  170. if address, err = s.requestAddress(ctx, listMedia); err != nil {
  171. return
  172. }
  173. var response []byte
  174. if response, err = util.PostJSONContext(ctx, address, in); err != nil {
  175. return
  176. }
  177. // 使用通用方法返回错误
  178. err = util.DecodeWithError(response, &out, "ListMedia")
  179. return
  180. }
  181. // GetMedia 获取媒资详细信息
  182. func (s *MiniDrama) GetMedia(ctx context.Context, in *GetMediaRequest) (out GetMediaResponse, err error) {
  183. var address string
  184. if address, err = s.requestAddress(ctx, getMedia); err != nil {
  185. return
  186. }
  187. var response []byte
  188. if response, err = util.PostJSONContext(ctx, address, in); err != nil {
  189. return
  190. }
  191. // 使用通用方法返回错误
  192. err = util.DecodeWithError(response, &out, "GetMedia")
  193. return
  194. }
  195. // GetMediaLink 获取媒资播放链接
  196. func (s *MiniDrama) GetMediaLink(ctx context.Context, in *GetMediaLinkRequest) (out GetMediaLinkResponse, err error) {
  197. var address string
  198. if address, err = s.requestAddress(ctx, getMediaLink); err != nil {
  199. return
  200. }
  201. var response []byte
  202. if response, err = util.PostJSONContext(ctx, address, in); err != nil {
  203. return
  204. }
  205. // 使用通用方法返回错误
  206. err = util.DecodeWithError(response, &out, "GetMediaLink")
  207. return
  208. }
  209. // DeleteMedia 删除媒体
  210. func (s *MiniDrama) DeleteMedia(ctx context.Context, in *DeleteMediaRequest) (out DeleteMediaResponse, err error) {
  211. var address string
  212. if address, err = s.requestAddress(ctx, deleteMedia); err != nil {
  213. return
  214. }
  215. var response []byte
  216. if response, err = util.PostJSONContext(ctx, address, in); err != nil {
  217. return
  218. }
  219. // 使用通用方法返回错误
  220. err = util.DecodeWithError(response, &out, "DeleteMedia")
  221. return
  222. }
  223. // AuditDrama 审核剧本
  224. func (s *MiniDrama) AuditDrama(ctx context.Context, in *AuditDramaRequest) (out AuditDramaResponse, err error) {
  225. var address string
  226. if address, err = s.requestAddress(ctx, auditDrama); err != nil {
  227. return
  228. }
  229. var response []byte
  230. if response, err = util.PostJSONContext(ctx, address, in); err != nil {
  231. return
  232. }
  233. // 使用通用方法返回错误
  234. err = util.DecodeWithError(response, &out, "AuditDrama")
  235. return
  236. }
  237. // ListDramas 获取剧目列表
  238. func (s *MiniDrama) ListDramas(ctx context.Context, in *ListDramasRequest) (out ListDramasResponse, err error) {
  239. var address string
  240. if address, err = s.requestAddress(ctx, listDramas); err != nil {
  241. return
  242. }
  243. var response []byte
  244. if response, err = util.PostJSONContext(ctx, address, in); err != nil {
  245. return
  246. }
  247. // 使用通用方法返回错误
  248. err = util.DecodeWithError(response, &out, "ListDramas")
  249. return
  250. }
  251. // GetDrama 获取剧目信息
  252. func (s *MiniDrama) GetDrama(ctx context.Context, in *GetDramaRequest) (out GetDramaResponse, err error) {
  253. var address string
  254. if address, err = s.requestAddress(ctx, getDrama); err != nil {
  255. return
  256. }
  257. var response []byte
  258. if response, err = util.PostJSONContext(ctx, address, in); err != nil {
  259. return
  260. }
  261. // 使用通用方法返回错误
  262. err = util.DecodeWithError(response, &out, "GetDrama")
  263. return
  264. }
  265. // GetCdnUsageData 查询 CDN 用量数据
  266. func (s *MiniDrama) GetCdnUsageData(ctx context.Context, in *GetCdnUsageDataRequest) (out GetCdnUsageDataResponse, err error) {
  267. var address string
  268. if address, err = s.requestAddress(ctx, getCdnUsageData); err != nil {
  269. return
  270. }
  271. var response []byte
  272. if response, err = util.PostJSONContext(ctx, address, in); err != nil {
  273. return
  274. }
  275. // 使用通用方法返回错误
  276. err = util.DecodeWithError(response, &out, "GetCdnUsageData")
  277. return
  278. }
  279. // GetCdnLogs 查询 CDN 日志
  280. func (s *MiniDrama) GetCdnLogs(ctx context.Context, in *GetCdnLogsRequest) (out GetCdnLogsResponse, err error) {
  281. var address string
  282. if address, err = s.requestAddress(ctx, getCdnLogs); err != nil {
  283. return
  284. }
  285. var response []byte
  286. if response, err = util.PostJSONContext(ctx, address, in); err != nil {
  287. return
  288. }
  289. // 使用通用方法返回错误
  290. err = util.DecodeWithError(response, &out, "GetCdnLogs")
  291. return
  292. }
  293. // requestAddress 请求地址
  294. func (s *MiniDrama) requestAddress(_ context.Context, url string) (string, error) {
  295. accessToken, err := s.ctx.GetAccessToken()
  296. if err != nil {
  297. return "", err
  298. }
  299. return url + accessToken, nil
  300. }