-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
262 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
.git | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters