Skip to content

Commit

Permalink
qcloud cdn nodejs sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
Galen-Yip committed Dec 1, 2016
1 parent ce2802f commit e03c01b
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
.git
.DS_Store
95 changes: 95 additions & 0 deletions Qcloud_CDN_API/nodejs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
## qcloud cdn openapi nodejs版本sdk

## 安装

npm install qcloud-cdn-node-sdk --save

## API

参见[API文档](https://github.com/QCloudCDN/CDN_API_SDK/blob/master/README.md)


## 错误码

参见[错误码文档](https://www.qcloud.com/document/product/228/5078)

## 使用

### 准备工作

qcloud账号的secret_id和secret_key可以从[https://console.qcloud.com/capi](https://console.qcloud.com/capi) 获取

### 初始化SDK配置

```js
const qcloudSDK = require('qcloud-cdn-node-sdk');

qcloudSDK.config({
secretId: 'qcloud账号的secretId',
secretKey: 'qcloud账号的参数表示secretKey'
})
```

### 调用具体的CDN方法

```js
/************Action对应的名字************/

//API文档见 https://github.com/QCloudCDN/CDN_API_SDK/blob/master/README.md

// DescribleCdnHosts
// GetHostInfoByHost
// GetHostInfoById
// RefreshCdnUrl
// RefreshCdnDir
// UpdateCache
// UpdateCdnProject
// UpdateCdnHost
// UpdateCdnConfig
// OfflineHost
// AddCdnHost
// OnlineHost
// DeleteCdnHost
// GenerateLogList
// GetCdnRefreshLog
// GetCdnStatTop
// GetCdnStatusCode
// DescribeCdnHostDetailedInfo
// DescribeCdnHostInfo
/************************/

// action对应的参数无需传递公共请求参数
qcloudSDK.request('Action的名字', action对应的参数对象, callback)

```

## 示例

```js
const qcloudSDK = require('qcloud-cdn-node-sdk');

qcloudSDK.config({
secretId: 'AKIDT8G5AsY1D3MChWooNq1rFSw1fyBVCX9D',
secretKey: 'pxPgRWDbCy86ZYyqBTDk7WmeRZSmPco0'
})

qcloudSDK.request('DescribeCdnHostInfo', {
'startDate': '2016-12-01',
'endDate': '2016-12-01',
'statType': 'bandwidth',
'projects.0': '123',
'hosts.0': 'www.test.com',
'hosts.1': 'www.test2.com'
}, (res) => {
// res为json格式
// do something
})
```

## 反馈

欢迎提issue

## LICENSE

MIT
90 changes: 90 additions & 0 deletions Qcloud_CDN_API/nodejs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
'use strict';

var requestLib = require('request');
var _ = require('lodash');
var utilityLib = require('utility');
var commonUtils = require('./libs/utils');

var OPENAPI_HOST = 'cdn.api.qcloud.com';
var OPENAPI_PATH = '/v2/index.php';
var OPENAPI_URL = 'https://' + OPENAPI_HOST + OPENAPI_PATH;
var METHOD = 'POST';


var QcloudSDK = function() {
this.secretKey = '';
this.secretId = '';
}

QcloudSDK.prototype.config = function(userConfig) {
checkUserConfig(userConfig)

this.secretKey = userConfig.secretKey;
this.secretId = userConfig.secretId;
}

QcloudSDK.prototype.request = function(actionName, params, callback) {
checkUserConfig({
secretKey: this.secretKey,
secretId: this.secretId
})

params = params || {};
var timestamp = Math.ceil((new Date()-0)/1000);
var nonce = _.random(1000000);
var signature = createSignature(actionName, nonce, timestamp, params, this.secretKey, this.secretId);

var requestData = _.assign({
'Action': actionName,
'Timestamp': timestamp,
'Nonce': nonce,
'SecretId': this.secretId,
'Signature': signature,
}, params)

requestData = commonUtils.serialize(requestData)

requestLib.post({
url: OPENAPI_URL,
form: requestData
}, function(err, httpRes, body) {
if(err) {
callback(err);
return;
}

callback(body)
})
}


function checkUserConfig(userConfig) {

if(!_.isPlainObject(userConfig)
|| !_.isString(userConfig['secretKey'])
|| !_.isString(userConfig['secretId'])) {
throw new Error('::config function should be called required an object param which contains secretKey[String] and secretId[String]')
}
}

function createSignature(actionName, nonce, timestamp, params, secretKey, secretId) {
var originObject = _.assign({
'Action': actionName,
'Nonce': nonce,
'SecretId': secretId,
'Timestamp': timestamp
}, params);
var sortedObject = commonUtils.sortObject(originObject);
var serializeString = commonUtils.serialize(sortedObject);
var originSignature = METHOD+OPENAPI_HOST+OPENAPI_PATH+'?'+serializeString;
var signature = encodeURIComponent(utilityLib.hmac('sha1', secretKey, originSignature));

return signature
}


var qcloudSDK = new QcloudSDK();


module.exports = qcloudSDK;

29 changes: 29 additions & 0 deletions Qcloud_CDN_API/nodejs/libs/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

var _ = require('lodash');


var sortObject = function(obj, fn) {
var keys = _.sortBy(_.keys(obj), fn);
var res = {};

_.forIn(keys, function(key) {
res[key] = obj[key];
})

return res
}

var serialize = function(obj) {
var res = '';
var mapValue = _.map(obj, function(value, key) {
return (key+'='+value)
});

res = _.join(mapValue, '&');

return res
}

exports.sortObject = sortObject;
exports.serialize = serialize;
27 changes: 27 additions & 0 deletions Qcloud_CDN_API/nodejs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "qcloud-cdn-node-sdk",
"version": "1.0.0",
"description": "腾讯云CDN OpenAPI Node.js SDK",
"main": "index.js",
"dependencies": {
"lodash": "^4.17.2",
"request": "^2.79.0",
"utility": "^1.9.0"
},
"devDependencies": {
"mocha": "^3.2.0"
},
"scripts": {
},
"repository": {
"type": "git",
"url": "https://github.com/QCloudCDN/CDN_API_SDK/tree/master/Qcloud_CDN_API/nodejs"
},
"keywords": [
"qcloud",
"nodejs",
"sdk"
],
"author": "QcloudCDN",
"license": "MIT"
}
25 changes: 18 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
##腾讯云CDN API 概览
## 消耗及统计量查询

## 腾讯云CDN OpenAPI SDK

现有版本:
* [go](https://github.com/QCloudCDN/CDN_API_SDK/tree/master/Qcloud_CDN_API/go)
* [nodejs](https://github.com/QCloudCDN/CDN_API_SDK/tree/master/Qcloud_CDN_API/nodejs)
* [php](https://github.com/QCloudCDN/CDN_API_SDK/tree/master/Qcloud_CDN_API/php)
* [python](https://github.com/QCloudCDN/CDN_API_SDK/tree/master/Qcloud_CDN_API/python)
* [java](https://github.com/QCloudCDN/CDN_API_SDK/tree/master/Qcloud_CDN_API/java/cdn_openapi_demo/src)

## API 概览

### 消耗及统计量查询

| API | 功能说明 |
| ---------------------------------------- | ---------------------------------------- |
Expand All @@ -10,7 +21,7 @@



## 域名查询
### 域名查询

| API | 功能说明 |
| ---------------------------------------- | -------------------------------- |
Expand All @@ -20,7 +31,7 @@



## 域名管理
### 域名管理

| API | 功能说明 |
| ---------------------------------------- | ------------------------ |
Expand All @@ -35,7 +46,7 @@



## 域名刷新
### 域名刷新

| API | 功能说明 |
| ---------------------------------------- | ------------------- |
Expand All @@ -45,15 +56,15 @@



## 日志查询
### 日志查询

| API | 功能说明 |
| ---------------------------------------- | -------- |
| [GenerateLogList](https://www.qcloud.com/doc/api/231/%E6%9F%A5%E8%AF%A2%E6%97%A5%E5%BF%97%E4%B8%8B%E8%BD%BD%E9%93%BE%E6%8E%A5) | 查询日志下载链接 |



## 辅助工具
### 辅助工具

| API | 功能说明 |
| ---------------------------------------- | ------------ |
Expand Down

0 comments on commit e03c01b

Please sign in to comment.