utils.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * Copyright (C) 2020 Tencent Cloud.
  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. 'use strict';
  17. const { sign } = require('../common');
  18. /**
  19. * 请求腾讯云VOD接口公共方法
  20. * @param {string} action - 接口请求action
  21. * @param {object} payload - 接口请求体
  22. * @return {object} API返回的有效数据
  23. */
  24. async function request(action, payload) {
  25. const [timestamp, authorization] = sign('vod', JSON.stringify(payload));
  26. const { res } = await uniCloud.httpclient.request('https://vod.tencentcloudapi.com', {
  27. method: 'POST',
  28. headers: {
  29. 'Content-Type': 'application/json',
  30. 'X-TC-Action': action,
  31. 'X-TC-Version': '2018-07-17',
  32. 'X-TC-Timestamp': timestamp,
  33. Authorization: authorization
  34. },
  35. data: payload,
  36. dataType: 'json'
  37. });
  38. const { status, statusMessage, data } = res;
  39. if (status !== 200) {
  40. throw new Error(`${action}接口调用失败[${status} - ${statusMessage}]`);
  41. }
  42. if (data.Response.Error) {
  43. throw new Error(data.Response.Error.Message);
  44. }
  45. return data.Response;
  46. }
  47. module.exports = { request };