DayPI: Batch API Calls together

By vineet

You can now group multiple DayPI requests into one request using the Batch API. Batch APIs help improve the performance of your application as your application makes only HTTP request to the DayPIs.

You can also download a PHP script to call the Batch API.

Read more about how to call the Batch API using PHP.

 

  1. What is the Batch API? 

    The Batch API accepts multiple DayPI requests, executes each of them in parallel and returns a combined response for all DayPI requests. You can make upto 20 individual DayPI requests in one batch request.
     

  2. When should you use the Batch API?

    If you are building an application or website that makes multiple DayPI requests to render one module or web-page, you can group the DayPI requests together into one batch request.

    For e.g., if you are building a topic page which calls topic_getRelatedStories, topic_getRelatedTopics and topic_getRelatedQuotes, you can make one batch request with all the 3 DayPI requests together.

     
  3.  What methods are available for the Batch API?

    The Batch API currently exposes one method - parallel. All DayPI requests provided as an input to this method, are executed in parallel by the batch API and the response to each DayPI request is combined and returned as one response.

     
  4.  How do you make a request to the Batch API?

    The methodology to make Batch API calls is very similar to that of making DayPI calls:

    • .     All requests to the Batch API are HTTP GET or POST requests.

    • .     All input parameters are accepted as name-value pairs in the query string of the HTTP GET request or the body of the HTTP POST request.

    • .     Batch APIs can return data as XML, JSON or PHP serialized formats.

    •      Values for all input parmeters must be url-encoded.
    There is only input parameter for the method parallel: request. The request parameter accepts the part of a valid DayPI request that appears beyond the format specifier in the DayPI request.

    For e.g., if your individual DayPI request is

    http: //freeapi.daylife.com/xmlrest/publicapi/4.2/search_getRelatedArticles?
    query=george%20bush&accesskey=youraccesskey&signature=thesignature
    


    then the value of the requestparam to the parallel method would be the urlencoded value of

    /publicapi/4.2/search_getRelatedArticles?
    query=george%20bush&accesskey=youraccesskey&signature=thesignature
    


    You can provide multiple name-value pairs for the request parameter as an input, one for each request . A batch request with 2 individual request looks like this:

    http: //freeapi.daylife.com/xmlrest/batch/1.0/parallel?accesskey=youraccesskey&
    request=urlencode(/publicapi/4.2/search_getRelatedArticles?query=george%20bush&
    		accesskey=youraccesskey&signature=thesignature&request_id=1001)
    &request=urlencode(/publicapi/4.2/search_getRelatedTopics?query=george%20bush&
    		accesskey=youraccesskey&signature=thesignature&request_id=1002)
    
     
  5. How do you match an individual DayPI request in the batch request to an individual DayPI response in the batch response?
  6. Notice the request_id param in the values of the request parameter above. Every single DayPI request provided as input to the batch.parallel method needs to have a unique alphanumeric request_id. When the batch API combines the response of each individual DayPI request, it adds the request_id from the indvidual request to its corresponding response. Your Batch API client can then match up every single request sent to batch.parallel to the individual responses in the combined response using the request_id.

    Download Sample Batch Response Here.

     

  7. Is there a way to call the batch API in a way so that you do not have to repeatedly provide common parameters that share the same value in each individual DayPI request?
  8. Yes, any parameter to the batch.parallel API call that starts with "batch_" is appended to each individual request without the prefix "batch_". For e.,g,, your provide batch_accesskey=XYZ as an input to the batch.parallel call, then accesskey=XYZ gets added as a parameter to each individual request.

     

  9. Are there any timeouts by the Batch API?
  10. Yes. The Batch API attempts to make individual requests in parallel, waits a maximum upto 25 seconds for each request to come back and then returns the combined responses of the requests that got completed.

     

  11. Is there any authentication for the Batch API?
  12. No. The Batch API has no authentication of its own. Each individual DayPI request provided as an input must contain a valid accesskey and signature. However, you can use the feature mentioned in topic 6 above to provide batch_accesskey and batch_signature as an input to the batch.parallel call rather than providing accesskey and signature in each individual request.

     

  13. What error codes are returned by the Batch API?

 3001: Succes. All individual requests were completed without a timeout. However, this does not guarantee that each individual request was successful. 

-3001: One or more individual requests did not complete within the 25 second timeframe.

-3002: Duplicate request_ids provided in the individual requests

-3003: One or more of the individual DayPI request were not formatted properly. See above how to make a valid request for the Batch API.

-3004: More than 20 requests provided as input to the batch API.

 

AttachmentSize
response-parallel.zip8.65 KB