Platform Technology-Introduction to API Batch Calling


1、Why batch calls

can improve application performance and provide faster response          For example, do A page needs to call 7 different HSF services to pull data and then render the page. In this case, if you want to speed up the page response, you may consider initiating 7 HSF calls concurrently and wait until all data is returned before rendering the page. However, such a programming model will have a long response time and cumbersome calls for the caller. If TOP completes 7 HSF calls directly through internal serial mode, and one call returns multiple data results, then it can Greatly improve the response time of each request. For some complex API calls (a single RT call may reach 100MS business), then there will be a good optimization effect


Use less traffic                                        Each API call will pass repeated system or business input parameters, such as: sign, method, timestamp, fields and other parameters. In many cases, these repeated parameters occupy most of the space in the request body. API batch calls can combine these parameters at the protocol level to achieve parameter reuse. It is very helpful for optimizing network usage in mobile calls and high concurrent calls on the server.

2, Batch call API feature

  • Supports all current security and business features
  • Supports all except file upload API free combination call
  • Supports multi-session mixed calls
  • The call results do not interfere with each other
  • The API response order is exactly the same as the request order, no need to sort or query the results by number
  • Extremely fast experience, M times of transaction API and N times of commodity API calls, RT is about 1 transaction API call (assuming that transaction API RT> commodity API)
  • Servlet, HSF, HTTP mixed fully asynchronous call mode, stably supports low RT and high RT mixed scenarios, improving server throughput capability
  • Public parameters and method names can be merged to streamline request messages
  • Simple calling protocol, no need for SDK Quickly implement batch API calls

3, introduction to batch call technology

3.1 Agreement content

Request URL:

http://gw.api.taobao.com/router/batch

POST:

http://gw.api.taobao.com/router/batch?sign=5336DB2C525E1A3AD1013BC82C4437B2×tamp=2016-01-05 10:15:15&v=2.0&app_key=4272&sign_method=hmac&format=json &session=6102b110 f51625dcd2d1921ba80c6d2b43a2a5a37e7c7c73627811065
Content-Type :
##text/plain;charset=UTF-8

PAYLOAD :

    Payload carries each API in the form of from, which is split by \r\n-S-\r\n by default; the parameter value needs to be URL Encode
  • Custom separator method: httpHeader.put("top-api-separator","????")
  • The first line starts with #PUBLIC#, which can extract public parameters and API names, which is optional . If there is no need to extract public parameters, remove the #PUBLIC# line and the first separator
  • Parameter priority: API line parameters ===Override===> #PUBLIC# parameter ===Override= ==> URL parameters
  • Signature method, similar to REST signature: hmac (other similar) byte2hex (hmac(key1value1key2value2...
  • payloadsecret))
  • Account Placeholder: Suppose you have abstracted all parameters of an API into the #PUBLIC# area, please put a placeholder N to indicate that I am actually an API; as shown above

3.2 Protocol content-Response

Response: Case 1

{"error_response":{"code":25,"msg":"Invalid signature","request_id":"16t6sm38r9709"}}

Response:情况2

{"time_get_response":{"time":"2016-01-05 10:53:02","request_id":"16t6sm2gxmm5q"}}
-S-
{"time_get_response":{"time":"2016-01-05 10:53:02","request_id":"16t6sm2gxmm5q"}}
-S-
{"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"}}
-S-
{"error_response":{"code":27,"msg":"Invalid session","sub_code":"invalid-sessionkey","request_id":"16t6sm2gxmm5q"}}
  • Response, case 1: The response message has only one result; such as signature, appkey does not exist; these are common characteristics, for example, we only sign once, then An API signature error means that all APIs will be signed incorrectly.
  • Response, case 2: The number of APIs in the response message and the request message are the same, and the response order will be consistent with the request order. For example, if you submit 15 requests, there may be 10 successes, 3 flow control, and 2 HSF service provider exceptions; but our response body will still have 15 request responses.
  • Custom separator mode: The default separator is \r\n-S-\r\n; when submitting a request in custom mode, use httpHeader.put("top-api-separator","?? ??”); The request and response body delimiters must be consistent

4 , SDK usage

4.1 Create Client

BatchTaobaoClient client = new BatchTaobaoClient ("http://gw.api.taobao.com router/batch
", "appkey","secret");} / /Note: To use this method, you need to download the latest sdk. The old sdk does not yet support the batch call function

4.2 Create BatchRequest

TaobaoBatchRequest batchRequest = new TaobaoBatchRequest();

4.3 添加请求API

batchRequest.addRequest(timeGetRequest1)
                                     .addRequest(timeGetRequest2)
                                     .addRequest(timeGetRequest3)
                                     .addRequest(timeGetRequest4)
                                     .addRequest(fullinfosRequest1)
                                     .addRequest(fullinfosRequest2)
                                     .addRequest(fullinfosRequest3);

4.4 提交请求

TaobaoBatchResponse       response = client.execute(batchRequest);
 

4.5 遍历结果

if (response.isSuccess()) {
                   for (int i = 0; i < response.getResponseList().size(); i ) 
                         println("body:"   response.getResponseList().get(i).getBody());
             }

4.6 Random query results

#if (response.isSuccess()) {
                                                                                                                         if(timeGetResponse1 .isSuccess())
TradeFullinfoGetResponse rep2 = response se.getResponse(fullinfosRequest3);
}


Note: To use sdk to implement batch api calling function, you need to download the latest sdk. The sdk download and usage point is here.

5. Examples of typical usage scenarios

Take calling the interface to obtain order details as an example. Originally, to query multiple order details of a merchant, multiple requests need to be made repeatedly; now if the batch method is used Call, multiple results can be returned with one request, which can greatly improve the efficiency of API calls. The following is an example of sdk calling in common usage scenarios (java):

5.1. Query the details of multiple orders from a single merchant

##BatchTaobaoClient client = new BatchTaobaoClient ( "http://gw.api.taobao.com/router/batch? ","test","test");                                                                                                      
req1.setTid(666666666661L); //Order 1
## TradeFullinfogetRequest Req2 = New TradeFullInfogetrequest ();
Req2.SetFields ("TID"); Single 2
## taobaobatchrequest REQ = new TaobaoBatchRequest();
req.addRequest(req1); req.addRequest(req2); q, "test"); //test represents the merchant sessionkey



5.2. Query multiple order details from multiple merchants

BatchTaobaoClient client = new BatchTaobaoClient ( "http://gw.api.taobao.com/router/batch ?","test","test");                                                      
req1.setTid(6666666666661L); // Order 1 from Merchant a
TradeFullinfoGetRequest req2 = new TradeFullinfoGetRequest();
req2.setFields( "tid");
req2.setTid(6666666666662L); //Order 2 of merchant a
                                                                                                                                                              ’     ’             ’ ’             remote ’ s t ’s ’ t ‐ ‐‐ ‐ ​ ​ ​ ​ ​ ​ ​ ​ ​888888888881L); //Order 1 from merchant b
req3.setBatchApiSession( "testtest "); //sessionkey of merchant b
TaobaoBatchRequest req = new TaobaoBatchRequest();
req.addRequest(req1);
       req.addRequest(req2);       
       req.addRequest(req3);       
          
     TaobaoBatchResponse rsp = client.execute(req,       "test");       //商家a的sessionkey

5.3. Query multiple merchant orders and product details

BatchTaobaoClient client = new BatchTaobaoClient ( "http://gw.api.taobao.com/router/batch ?","test","test");                                                      
req1.setTid(6666666666661L); // Order 1 from Merchant a
TradeFullinfoGetRequest req2 = new TradeFullinfoGetRequest();
req2.setFields( "tid");
req2.setTid(6666666666662L); //Order 2 of merchant a
ItemSellerGetRequest req3 = new ItemSellerGetRequest();
req3.setFields("num_iid");
req3.setNumIid (8888888881L); // Merchant b’s product 1
req3.setBatchApiSession( "testtest "); //sessionkey of merchant b
TaobaoBatchRequest req = new TaobaoBatchRequest();
req.addRequest(req1);
       req.addRequest(req2);       
       req.addRequest(req3);       
          
     TaobaoBatchResponse rsp = client.execute(req,       "test");       //商家a的sessionkey

 

FAQ

测试环境下与正式环境下的API调用量有限制吗?