taobao-openapi
Advanced tools
Comparing version 0.1.0 to 0.2.0
113
index.js
@@ -1,112 +0,1 @@ | ||
var _ = require("underscore"); | ||
var request = require("request"); | ||
var md5 = require("md5"); | ||
var moment = require("moment"); | ||
var qs = require('querystring'); | ||
module.exports = Taobao; | ||
/** | ||
* default configuration | ||
*/ | ||
var cfg = { | ||
debug: false, | ||
https: false, | ||
path: '/router/rest', | ||
httpRealHost: 'http://gw.api.taobao.com', | ||
httpSandHost: 'http://gw.api.tbsandbox.com', | ||
httpsRealHost: 'https://eco.taobao.com', | ||
httpsSandHost: 'https://gw.api.tbsandbox.com' | ||
}; | ||
/** | ||
* api client constructor | ||
* | ||
* @param {Object} config | ||
* @constructor | ||
*/ | ||
function Taobao(config) { | ||
this.config = {}; | ||
_.extend(this.config, cfg, config); | ||
} | ||
/** | ||
* reset the config | ||
* | ||
* @param {Object} config | ||
*/ | ||
Taobao.prototype.config = function (config) { | ||
_.extend(this.config, config); | ||
}; | ||
/** | ||
* get request | ||
* | ||
* @param {String} method | ||
* @param {Object} [config] | ||
* @param {Object} args | ||
* @param {Function} callback | ||
*/ | ||
Taobao.prototype.get = function () { | ||
//arguments | ||
var method = ""; | ||
var config = {}; | ||
var args = {}; | ||
var callback; | ||
if (arguments.length == 3) { | ||
method = arguments[0]; | ||
args = arguments[1]; | ||
callback = arguments[2]; | ||
} else if (arguments.length == 4) { | ||
method = arguments[0]; | ||
config = arguments[1] || {}; | ||
args = arguments[2]; | ||
callback = arguments[3]; | ||
} | ||
_.extend(args, {method: method}); | ||
var format_args = formatArgs(args, this.config); | ||
var host = this.config.debug ? | ||
(this.config.https ? this.config.httpsSandHost : this.config.httpSandPath) : | ||
(this.config.https ? this.config.httpsRealHost : this.config.httpRealHost); | ||
var uri = host + this.config.path + "?" + format_args; | ||
var option = { | ||
method: "GET", | ||
uri: uri | ||
}; | ||
request(option, function (error, response, body) { | ||
if (error) { | ||
return callback(error); | ||
} | ||
callback(null, body); | ||
}); | ||
}; | ||
function formatArgs(args, config) { | ||
args.timestamp = moment().format("YYYY-MM-DD HH:mm:ss"); | ||
args.format = "json"; | ||
args.v = "2.0"; | ||
args.sign_method = "md5"; | ||
args.app_key = config.app_key; | ||
args.sign = signArgs(args, config); | ||
return qs.stringify(args); | ||
} | ||
function signArgs(args, config) { | ||
var argsArr = []; | ||
for (var arg in args) { | ||
argsArr.push(arg + args[arg]); | ||
} | ||
return md5(config.app_secret + argsArr.sort().join('') + config.app_secret).toUpperCase(); | ||
} | ||
module.exports = require("./src"); |
{ | ||
"name": "taobao-openapi", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "package for taobao open api", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# taobao-openapi | ||
api client for taobao open platform | ||
淘宝开放平台SDK,用于node平台 | ||
# Install | ||
npm install taobao-openapi --save | ||
# Examlpe | ||
var Taobao = require("taobao-openapi"); | ||
var top = new Taobao({ | ||
app_key: "yourAppKey", | ||
app_secret: "yourAppSecret" | ||
}); | ||
top.get("taobao.shop.get", { | ||
nick: "nickname", | ||
fields: "sid,cid,nick,title,desc,shop_score" | ||
}, function(err, body){ | ||
if(err){ | ||
return console.log(err); | ||
} | ||
console.log(body); | ||
}); | ||
top.post("taobao.shop.get", { | ||
nick: "nickname", | ||
fields: "sid,cid,nick,title,desc,shop_score" | ||
}, function(err, body){ | ||
if(err){ | ||
return console.log(err); | ||
} | ||
console.log(body); | ||
}); | ||
# API | ||
get请求: | ||
top.get(method, [config], args, callback) | ||
method:api接口名称 | ||
config:可选,配置对象,只对当前请求有效 | ||
args:接口参数 | ||
callback:回调函数,包含错误参数err和响应body | ||
top.post(method, [config], args, callback) | ||
method:api接口名称 | ||
config:可选,配置对象,只对当前请求有效 | ||
args:接口参数 | ||
callback:回调函数,包含错误参数err和响应body | ||
# Features | ||
1、configure | ||
配置全局参数 | ||
top.configure({ | ||
session: "yourSession" | ||
}); | ||
### 目前有效参数: | ||
| attribute | description | default | | ||
| -------------|:-------------:| -----: | | ||
| session | session | null | | ||
| debug | 是否开启沙箱 | false | | ||
| https | https请求 | false | | ||
目前只支持json响应格式,不支持xml格式; | ||
目前只支持MD5加密方法; | ||
欢迎fork! |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
6627
5
155
76
1