open8200 aretmis header sign SDK for node.js
aretmis-header-sign 由官方提供的方法,用于请求过程中在header添加签名的请求信息
npm install artemis-header-sign;
// commonjs
var client = require('artemis-header-sign');
// es6
import client from 'artemis-header-sign';
/**
* @param {String} appKey
* @param {String} secret
*/
var signHeader = client(appKey, secret).signHeader;
/**
* @param {String} url 请求url
* @param {JSON} urlParams 请求url中的参数
* @param {String} method 请求方式 POST GET DELETE PUT
* @param {JSON} headers 请求头,比如 { 'content-type': 'application/x-www-form-urlencoded;charset=UTF-8' }
* @param {JSON} params form表单的请求参数,比如get请求的query和post请求的body { 'a': 1, 'b': 2 }
* @param {JSON} data 非form请求数据的参数
*
* @return {JSON} header 应用于请求时的header
*/
var header = signHeader(url, urlParams, method, headers, params, data);
使用 npm 安装 axios 包
npm install axios
var axios = require('axios');
var client = require('artemis-header-sign');
// 注: 当前appKey仅供调试接口,尽可以调通 /api/artemis/v1/plus 和 /api/artemis/v1/minus 接口
var signHeader = client('23967750', 'BZcz3VlqL1DVhWeF1boE').signHeader;
/**
* post请求,form传参调用方法
* signHeader(...args)
* @param method: 'post'
* @param params { a: 1, b: 2}
*/
axios.request({
url: 'https://open8200.hikvision.com/artemis/api/artemis/v1/plus',
params: {
a: 1,
b: 2
},
method: 'post',
headers: signHeader('https://open8200.hikvision.com/artemis/api/artemis/v1/plus', null, 'post', null, { a: 1, b: 2 }, null)
}) ...
axios.post('https://open8200.hikvision.com/artemis/api/artemis/v1/plus', null, {
params: {
a: 1,
b: 2
},
headers: signHeader('https://open8200.hikvision.com/artemis/api/artemis/v1/plus', null, 'post', null, { a: 1, b: 2 }, null)
}) ...
/**
* get请求,form传参调用方法
* signHeader(...args)
* @param method: 'get'
* @param params { a: 1, b: 2}
*/
axios.get('https://open8200.hikvision.com/artemis/api/artemis/v1/minus', {
params: {
a: 1,
b: 1
},
headers: signHeader('https://open8200.hikvision.com/artemis/api/artemis/v1/minus', null, 'get', null, { a: 1, b: 1 }, null)
}) ...
/**
* post请求中,参数为非form的请求,
* @param method 'post'
* @param params 如果存在form参数,在此处传
* @param data 非form参数,在此处传参
* @param header 需要添加自定义header时 { 'Content-Type': 'application/json;charset=UTF-8' }
*/
axios.request({
url: 'https://open8200.hikvision.com/artemis/api/vms/v1/setStatus',
data: [{ apiId: 71, status: "ONLINE" }],
method: 'post',
headers: signHeader('https://open8200.hikvision.com/artemis/api/vms/v1/setStatus', null, 'post',
{
'Content-Type': 'application/json;charset=UTF-8'
},
null, [{ apiId: 71, status: "ONLINE" }])
}) ...
axios.post('https://open8200.hikvision.com/artemis/api/vms/v1/setStatus', [{ apiId: 71, status: "ONLINE" }], {
headers: signHeader('https://open8200.hikvision.com/artemis/api/vms/v1/setStatus', null, 'post',
{
'Content-Type': 'application/json;charset=UTF-8'
},
null, [{ apiId: 71, status: "ONLINE" }])
}) ...
/**
* url中存在参数时,在请求地址中,需要将appKey替换成请求的具体参数,在signHeader中,传入第二个参数urlParams,且key对应url中的名称
* @param urlParams { appKey: '22501409' }
*/
axios.get('https://open8200.hikvision.com/artemis/api/artemis/v1/agreementService/securityParam/appKey/22501409' /* appkey 被替换 */, {
headers: signHeader('https://open8200.hikvision.com/artemis/api/artemis/v1/agreementService/securityParam/appKey/{appKey}',
{
appKey: '22501409'
},
'get', null, null, null)
}) ...