Skip to content

Commit e03c01b

Browse files
committed
qcloud cdn nodejs sdk
1 parent ce2802f commit e03c01b

File tree

6 files changed

+262
-7
lines changed

6 files changed

+262
-7
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
.git
3+
.DS_Store

Qcloud_CDN_API/nodejs/README.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
## qcloud cdn openapi nodejs版本sdk
2+
3+
## 安装
4+
5+
npm install qcloud-cdn-node-sdk --save
6+
7+
## API
8+
9+
参见[API文档](https://github.com/QCloudCDN/CDN_API_SDK/blob/master/README.md)
10+
11+
12+
## 错误码
13+
14+
参见[错误码文档](https://www.qcloud.com/document/product/228/5078)
15+
16+
## 使用
17+
18+
### 准备工作
19+
20+
qcloud账号的secret_id和secret_key可以从[https://console.qcloud.com/capi](https://console.qcloud.com/capi) 获取
21+
22+
### 初始化SDK配置
23+
24+
```js
25+
const qcloudSDK = require('qcloud-cdn-node-sdk');
26+
27+
qcloudSDK.config({
28+
secretId: 'qcloud账号的secretId',
29+
secretKey: 'qcloud账号的参数表示secretKey'
30+
})
31+
```
32+
33+
### 调用具体的CDN方法
34+
35+
```js
36+
/************Action对应的名字************/
37+
38+
//API文档见 https://github.com/QCloudCDN/CDN_API_SDK/blob/master/README.md
39+
40+
// DescribleCdnHosts
41+
// GetHostInfoByHost
42+
// GetHostInfoById
43+
// RefreshCdnUrl
44+
// RefreshCdnDir
45+
// UpdateCache
46+
// UpdateCdnProject
47+
// UpdateCdnHost
48+
// UpdateCdnConfig
49+
// OfflineHost
50+
// AddCdnHost
51+
// OnlineHost
52+
// DeleteCdnHost
53+
// GenerateLogList
54+
// GetCdnRefreshLog
55+
// GetCdnStatTop
56+
// GetCdnStatusCode
57+
// DescribeCdnHostDetailedInfo
58+
// DescribeCdnHostInfo
59+
/************************/
60+
61+
// action对应的参数无需传递公共请求参数
62+
qcloudSDK.request('Action的名字', action对应的参数对象, callback)
63+
64+
```
65+
66+
## 示例
67+
68+
```js
69+
const qcloudSDK = require('qcloud-cdn-node-sdk');
70+
71+
qcloudSDK.config({
72+
secretId: 'AKIDT8G5AsY1D3MChWooNq1rFSw1fyBVCX9D',
73+
secretKey: 'pxPgRWDbCy86ZYyqBTDk7WmeRZSmPco0'
74+
})
75+
76+
qcloudSDK.request('DescribeCdnHostInfo', {
77+
'startDate': '2016-12-01',
78+
'endDate': '2016-12-01',
79+
'statType': 'bandwidth',
80+
'projects.0': '123',
81+
'hosts.0': 'www.test.com',
82+
'hosts.1': 'www.test2.com'
83+
}, (res) => {
84+
// res为json格式
85+
// do something
86+
})
87+
```
88+
89+
## 反馈
90+
91+
欢迎提issue
92+
93+
## LICENSE
94+
95+
MIT

Qcloud_CDN_API/nodejs/index.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
'use strict';
2+
3+
var requestLib = require('request');
4+
var _ = require('lodash');
5+
var utilityLib = require('utility');
6+
var commonUtils = require('./libs/utils');
7+
8+
var OPENAPI_HOST = 'cdn.api.qcloud.com';
9+
var OPENAPI_PATH = '/v2/index.php';
10+
var OPENAPI_URL = 'https://' + OPENAPI_HOST + OPENAPI_PATH;
11+
var METHOD = 'POST';
12+
13+
14+
var QcloudSDK = function() {
15+
this.secretKey = '';
16+
this.secretId = '';
17+
}
18+
19+
QcloudSDK.prototype.config = function(userConfig) {
20+
checkUserConfig(userConfig)
21+
22+
this.secretKey = userConfig.secretKey;
23+
this.secretId = userConfig.secretId;
24+
}
25+
26+
QcloudSDK.prototype.request = function(actionName, params, callback) {
27+
checkUserConfig({
28+
secretKey: this.secretKey,
29+
secretId: this.secretId
30+
})
31+
32+
params = params || {};
33+
var timestamp = Math.ceil((new Date()-0)/1000);
34+
var nonce = _.random(1000000);
35+
var signature = createSignature(actionName, nonce, timestamp, params, this.secretKey, this.secretId);
36+
37+
var requestData = _.assign({
38+
'Action': actionName,
39+
'Timestamp': timestamp,
40+
'Nonce': nonce,
41+
'SecretId': this.secretId,
42+
'Signature': signature,
43+
}, params)
44+
45+
requestData = commonUtils.serialize(requestData)
46+
47+
requestLib.post({
48+
url: OPENAPI_URL,
49+
form: requestData
50+
}, function(err, httpRes, body) {
51+
if(err) {
52+
callback(err);
53+
return;
54+
}
55+
56+
callback(body)
57+
})
58+
}
59+
60+
61+
function checkUserConfig(userConfig) {
62+
63+
if(!_.isPlainObject(userConfig)
64+
|| !_.isString(userConfig['secretKey'])
65+
|| !_.isString(userConfig['secretId'])) {
66+
throw new Error('::config function should be called required an object param which contains secretKey[String] and secretId[String]')
67+
}
68+
}
69+
70+
function createSignature(actionName, nonce, timestamp, params, secretKey, secretId) {
71+
var originObject = _.assign({
72+
'Action': actionName,
73+
'Nonce': nonce,
74+
'SecretId': secretId,
75+
'Timestamp': timestamp
76+
}, params);
77+
var sortedObject = commonUtils.sortObject(originObject);
78+
var serializeString = commonUtils.serialize(sortedObject);
79+
var originSignature = METHOD+OPENAPI_HOST+OPENAPI_PATH+'?'+serializeString;
80+
var signature = encodeURIComponent(utilityLib.hmac('sha1', secretKey, originSignature));
81+
82+
return signature
83+
}
84+
85+
86+
var qcloudSDK = new QcloudSDK();
87+
88+
89+
module.exports = qcloudSDK;
90+

Qcloud_CDN_API/nodejs/libs/utils.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
3+
var _ = require('lodash');
4+
5+
6+
var sortObject = function(obj, fn) {
7+
var keys = _.sortBy(_.keys(obj), fn);
8+
var res = {};
9+
10+
_.forIn(keys, function(key) {
11+
res[key] = obj[key];
12+
})
13+
14+
return res
15+
}
16+
17+
var serialize = function(obj) {
18+
var res = '';
19+
var mapValue = _.map(obj, function(value, key) {
20+
return (key+'='+value)
21+
});
22+
23+
res = _.join(mapValue, '&');
24+
25+
return res
26+
}
27+
28+
exports.sortObject = sortObject;
29+
exports.serialize = serialize;

Qcloud_CDN_API/nodejs/package.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "qcloud-cdn-node-sdk",
3+
"version": "1.0.0",
4+
"description": "腾讯云CDN OpenAPI Node.js SDK",
5+
"main": "index.js",
6+
"dependencies": {
7+
"lodash": "^4.17.2",
8+
"request": "^2.79.0",
9+
"utility": "^1.9.0"
10+
},
11+
"devDependencies": {
12+
"mocha": "^3.2.0"
13+
},
14+
"scripts": {
15+
},
16+
"repository": {
17+
"type": "git",
18+
"url": "https://github.com/QCloudCDN/CDN_API_SDK/tree/master/Qcloud_CDN_API/nodejs"
19+
},
20+
"keywords": [
21+
"qcloud",
22+
"nodejs",
23+
"sdk"
24+
],
25+
"author": "QcloudCDN",
26+
"license": "MIT"
27+
}

README.md

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
1-
##腾讯云CDN API 概览
2-
## 消耗及统计量查询
1+
2+
## 腾讯云CDN OpenAPI SDK
3+
4+
现有版本:
5+
* [go](https://github.com/QCloudCDN/CDN_API_SDK/tree/master/Qcloud_CDN_API/go)
6+
* [nodejs](https://github.com/QCloudCDN/CDN_API_SDK/tree/master/Qcloud_CDN_API/nodejs)
7+
* [php](https://github.com/QCloudCDN/CDN_API_SDK/tree/master/Qcloud_CDN_API/php)
8+
* [python](https://github.com/QCloudCDN/CDN_API_SDK/tree/master/Qcloud_CDN_API/python)
9+
* [java](https://github.com/QCloudCDN/CDN_API_SDK/tree/master/Qcloud_CDN_API/java/cdn_openapi_demo/src)
10+
11+
## API 概览
12+
13+
### 消耗及统计量查询
314

415
| API | 功能说明 |
516
| ---------------------------------------- | ---------------------------------------- |
@@ -10,7 +21,7 @@
1021

1122

1223

13-
## 域名查询
24+
### 域名查询
1425

1526
| API | 功能说明 |
1627
| ---------------------------------------- | -------------------------------- |
@@ -20,7 +31,7 @@
2031

2132

2233

23-
## 域名管理
34+
### 域名管理
2435

2536
| API | 功能说明 |
2637
| ---------------------------------------- | ------------------------ |
@@ -35,7 +46,7 @@
3546

3647

3748

38-
## 域名刷新
49+
### 域名刷新
3950

4051
| API | 功能说明 |
4152
| ---------------------------------------- | ------------------- |
@@ -45,15 +56,15 @@
4556

4657

4758

48-
## 日志查询
59+
### 日志查询
4960

5061
| API | 功能说明 |
5162
| ---------------------------------------- | -------- |
5263
| [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) | 查询日志下载链接 |
5364

5465

5566

56-
## 辅助工具
67+
### 辅助工具
5768

5869
| API | 功能说明 |
5970
| ---------------------------------------- | ------------ |

0 commit comments

Comments
 (0)