Coder Social home page Coder Social logo

qcloudapi-sdk's People

Contributors

alsotang avatar cyrilluce avatar justan avatar oncecrazy avatar puterjam avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

qcloudapi-sdk's Issues

code: 6100, message: '本版本内不支持的接口或者接口已经被废弃,请参考该接口的文档

`var Capi = require('qcloudapi-sdk')

//通过构造函数传入的参数将作为默认配置
var capi = new Capi({
// baseHost: 'wenzhi.api.qcloud.com',
SecretId: 'AKID4r2cxZhSLK2Mrtlnl5k3PAACygctvfpu',
SecretKey: '*****************************************',
serviceType: 'account'
})
// 传入配置以覆盖默认项
capi.request({
Region: 'bj',
Action: 'TextSentiment',
content: '我爱洗澡',
type: 1
}, function(error, data) {
if (error) {
console.log(error);
}
console.log(data)
})`
code: 6100, message: '本版本内不支持的接口或者接口已经被废弃,请参考该接口的文档。'

In order to use async, await or then, can you add a dev branch, I have already written the code and tested in tencentcloud vod service

1、new Buffer() is deprecated
2、Use ES6 class
3、The main change is in QcloudApi.prototype.request
4、Use axios instead of request module ( or request-promise-native)
5、The original method "QcloudApi.prototype.request = function(data, opts, callback, extra) " has a problem. If don't pass data, for example , request (opts, callback, extra),the opts parameter will be treated as data.

code :

const axios = require('axios')
const qs = require('querystring')
const dotQs = require('dot-qs')
const crypto = require('crypto')
const packageJSON = require('./package.json')
const baseHost = 'api.qcloud.com'

class QcloudApi {
  constructor(defaults) {
    this.defaults = Object.assign({
      path: '/v2/index.php',
      method: 'POST',
      protocol: 'https',
      baseHost: baseHost
    }, defaults)
  }

  generateUrl(opts) {
    opts = opts || {}
    const host = this._getHost(opts)
    const path = opts.path === undefined ? this.defaults.path : opts.path
    return (opts.protocol || this.defaults.protocol) + '://' + host + path
  }

  generateQueryString(data, opts) {
    opts = opts || this.defaults
    const defaults = this.defaults
    let param = Object.assign({
      Region: this.defaults.Region,
      SecretId: opts.SecretId || this.defaults.SecretId,
      Timestamp: Math.round(Date.now() / 1000),
      Nonce: Math.round(Math.random() * 65535),
      RequestClient: 'SDK_NODEJS_' + packageJSON.version 
    }, data)
    param = dotQs.flatten(param)
    let keys = Object.keys(param)
    let qstr = '', signStr
    const host = this._getHost(opts)
    const method = (opts.method || defaults.method).toUpperCase()
    const path = opts.path === undefined ? defaults.path : opts.path
    keys.sort()
    keys.forEach(function(key) {
      const val = param[key]
      if(method === 'POST' && val && val[0] === '@'){
        return
      }
      if(key === '') {
        return
      }
      if(val === undefined || val === null || (typeof val === 'number' && isNaN(val))) {
        val = ''
      }
      qstr += '&' + (key.indexOf('_') ? key.replace(/_/g, '.') : key )+ '=' + val
    })
    qstr = qstr.slice(1)
    signStr = this.sign(method + host + path + '?' + qstr, opts.SecretKey || defaults.SecretKey)
    param.Signature = signStr
    return qs.stringify(param)
  }

  /**
   * request({data: {x: 'x'}, opts: {y: 'y'}, extra: {z:'z'}})
   */
  request(options) {
    {data ={}, opts = this.defaults, extra} = options
    const url = this.generateUrl(opts)
    const method = (opts.method || this.defaults.method).toUpperCase()
    const dataStr = this.generateQueryString(data, opts)
    let option = {url, method}
    if(method === 'POST') {
        option.data = dataStr
    }else{
        option.url += '?' + dataStr
    }
    if(extra) Object.assign(option, extra)
    // use .catch or try catch to catch error in outside
    return axios(option)
  }

  /**
   * Buffer.from(string[, encoding]), encoding <string> The encoding of string. Default: 'utf8'.
   */
  sign(str, secretKey) {
    const hmac = crypto.createHmac('sha1', secretKey || '')
    return hmac.update(Buffer.from(str)).digest('base64')
  }

  _getHost(opts) {
    let host = opts.host
    if(!host) {
        host = (opts.serviceType || this.defaults.serviceType) + '.' + (opts.baseHost || this.defaults.baseHost)
    }
    return host
  }
}

module.exports = QcloudApi

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.