One step further Restful api
Nowadays, Restful API is THE language used between client and backend server.
We are creating an iOS app. The backend server provides several endpoints: /api/hot_item_A, /api/hot_item_B, etc. In one view, it needs data from both endpoints, that means either the client make two requests or server provides a new api which returns A + B. I don’t like neither of them. The idea of “Saving http requests” sounds quite general, too general that it deserves a solution. Followed the trace, I tried to google “bundle rest api” but with no luck. Then I decide to make it out by myself.
Here is what I got:
/api/wrapper/?urls=/api/hot_item_A,/api/hot_item_B
I created a new endpoint wrapper, which accepts a query param urls. It contains a comma separated list. The backend logic is quite simple, get the result of each and return the combined results as following:
One step further
Use the first requests result to build the second one.
/api/wrapper/?urls=/api/product/20/,/api/company/{0:company_id}/
This will return the product 20 and its company information. {0:company_id}, 0 is the index of url, company_id is the data field name of json data.
A little wilder
/api/wrapper/?urls=/api/hot_products/,/api/product/?company_id={in:0:company_id}
The hot_products return a list of products, and the {in:0:company_id} query gets all products from those company which has at least one product in hot product list.
Even better
/api/wrapper/?urls=/api/hot_products/,{embed:0:company:/api/company/{0:company_id}/}
It should return the hot_products which embed the company information in each of the product.
More powerful, more ugly the query string is. ☺