可以提高应用性能,更快的响应
比如做一个页面需要调用7个不同的HSF服务拉取数据,然后渲染页面。这种情况下, 如想加快页面响应,可能会考虑并发发起7次HSF调用等到数据都返回来再进行页面渲染。但是这样的编程模型对于调用者来说响应时间会比较长且调用繁琐,如果TOP把7次的HSF调用通过内部并行的方式来完成,1次调用返回多个数据结果,那则可以大大提高每次请求响应时间,减少网络请求的次数。那对于某些复杂的API调用(单次调用RT可能会达到100MS的业务),那么就有很好的优化效果。
使用更少的流量
单次API调用会传重复的系统或者业务入参,比如:sign、method、时间戳、fileds等参数,在很多情况下这些重复参数占用了请求体里面的大部分空间。API批量调用可以将这些参数在协议层面上进行合并,实现参数的复用。对于移动端调用、服务端高并发调用中优化网络使用是很有帮助的。
请求URL:
http://gw.api.taobao.com/router/batch |
POST:
http://gw.api.taobao.com/router/batch?sign=5336DB2C525E1A3AD1013BC82C4437B2×tamp=2016-01-05+10%3A15%3A15&v=2.0&app_key=4272&sign_method=hmac&format=json &session=6102b110f51625dcd2d1921ba80c6d2b43a2a5a37e7c7c73627811065 |
text/plain;charset=UTF-8 |
PAYLOAD:
#PUBLIC#method=taobao.trade.fullinfo.get&fields=tid,type,status,payment,orders,receiver_address,receiver_mobile,receiver_name method=taobao.time.get session=6102b110f51625dcd2d1921ba80c6d2b43a2a5a37e7c7c73627811065&tid=193923500416510&fields=tid,type,status,payment,orders,receiver_address,receiver_mobile,receiver_name session=6102b110f51625dcd2d1921ba80c6d2b43a2a5a37e7c7c73627811065&tid=193923500416517 tid=193923500416518 |
Response:情况1
{"error_response":{"code":25,"msg":"Invalid signature","request_id":"16t6sm38r9709"}} |
{"time_get_response":{"time":"2016-01-05 10:53:02","request_id":"16t6sm2gxmm5q"}} {"time_get_response":{"time":"2016-01-05 10:53:02","request_id":"16t6sm2gxmm5q"}} {"trade_fullinfo_get_response":{"trade":{"orders":{"order":[{"adjust_fee":"0.00","buyer_rate":false,"cid":50069506,"consign_time":"2015-12-03 14:01:07","discount_fee":"0.00","end_time":"2015-12-03 16:18:19","invoice_no":"0000","is_oversold":false,"logistics_company":"电 子凭 证","num":1,"num_iid":2100727710059,"oid":193923500416510,"order_from":"TAOBAO","payment":"11.00","pic_path":"http:\/ \/img04.daily.taobao.net\/bao\/uploaded\/i4 \/TB1EG6SXXXXXXaBXXXXXXXXXXXX_!!0- item_pic.jpg","price":"11.00","refund_status":"NO_REFUND","seller_rate":false,"seller_type":"C","shipping_type":"virtual","snapshot_url":"d:193923500416510_1","status":"TRADE_FINISHED","title":" 测试发布收费课 2015123","total_fee":"11.00"}]},"payment":"11.00","receiver_address":"15988161275","receiver_mobile":"","receiver_name":" 不****","status":"TRADE_FINISHED","tid":193923500416510,"type":"eticket"},"request_id":"16t6sm2gxmm5q"}} {"error_response":{"code":27,"msg":"Invalid session","sub_code":"invalid-sessionkey","request_id":"16t6sm2gxmm5q"}} |
BatchTaobaoClient client = new BatchTaobaoClient (“http://gw.api.taobao.com router/batch ”, “appkey”,“secret"); |
TaobaoBatchRequest batchRequest = new TaobaoBatchRequest(); |
batchRequest.addRequest(timeGetRequest1) .addRequest(timeGetRequest2) .addRequest(timeGetRequest3) .addRequest(timeGetRequest4) .addRequest(fullinfosRequest1) .addRequest(fullinfosRequest2) .addRequest(fullinfosRequest3); |
TaobaoBatchResponse response = client.execute(batchRequest); |
if (response.isSuccess()) { for (int i = 0; i < response.getResponseList().size(); i++) } |
if (response.isSuccess()) { TimeGetResponse resp1 = response.getResponse(timeGetRequest1); if(timeGetResponse1.isSuccess()) TradeFullinfoGetResponse rep2 = response.getResponse(fullinfosRequest3); } |
注:使用sdk实现批量api调用功能 ,需要下载最新的sdk,sdk下载及使用点 这里 。
以调用接口获取订单详情为例,原来要查询某个商家多个订单详情,需要重复发起多次请求;现如果使用批量方式调用,1次请求就可以返回多个结果,可以大大提高API调用效率。以下是常用使用场景sdk调用示例(java):
BatchTaobaoClient client = new BatchTaobaoClient ("http://gw.api.taobao.com/router/batch?","test","test"); TradeFullinfoGetRequest req1 = new TradeFullinfoGetRequest(); req1.setFields("tid"); req1.setTid(6666666666661L);//订单1 TradeFullinfoGetRequest req2 = new TradeFullinfoGetRequest(); req2.setFields("tid"); req2.setTid(6666666666662L);//订单2 TaobaoBatchRequest req = new TaobaoBatchRequest(); req.addRequest(req1); req.addRequest(req2); TaobaoBatchResponse rsp = client.execute(req,"test");//test表示商家的sessionkey |
BatchTaobaoClient client = new BatchTaobaoClient ("http://gw.api.taobao.com/router/batch?","test","test"); TradeFullinfoGetRequest req1 = new TradeFullinfoGetRequest(); req1.setFields("tid"); req1.setTid(6666666666661L);//商家a的订单1 TradeFullinfoGetRequest req2 = new TradeFullinfoGetRequest(); req2.setFields("tid"); req2.setTid(6666666666662L);//商家a的订单2 TradeFullinfoGetRequest req3 = new TradeFullinfoGetRequest(); req3.setFields("tid"); req3.setTid(8888888888881L); //商家b的订单1 req3.setBatchApiSession("testtest");//商家b的sessionkey TaobaoBatchRequest req = new TaobaoBatchRequest(); req.addRequest(req1); req.addRequest(req2); req.addRequest(req3); TaobaoBatchResponse rsp = client.execute(req,"test");//商家a的sessionkey |
BatchTaobaoClient client = new BatchTaobaoClient ("http://gw.api.taobao.com/router/batch?","test","test"); TradeFullinfoGetRequest req1 = new TradeFullinfoGetRequest(); req1.setFields("tid"); req1.setTid(6666666666661L);//商家a的订单1 TradeFullinfoGetRequest req2 = new TradeFullinfoGetRequest(); req2.setFields("tid"); req2.setTid(6666666666662L);//商家a的订单2 ItemSellerGetRequest req3 = new ItemSellerGetRequest(); req3.setFields("num_iid"); req3.setNumIid(8888888881L); //商家b的商品1 req3.setBatchApiSession("testtest");//商家b的sessionkey TaobaoBatchRequest req = new TaobaoBatchRequest(); req.addRequest(req1); req.addRequest(req2); req.addRequest(req3); TaobaoBatchResponse rsp = client.execute(req,"test");//商家a的sessionkey |