designing a REST api that supports multiple screensizes
Until now all my thumbnails have been 150x150. I would like to support different thumbnail sizes, in order to deal with different Android/iPhone models.
I have been considering this way of supporting multiple sizes. Always working with square thumbnails to makes things simpler.
Obta开发者_如何学JAVAining a 75x75 thumbnail
GET http://example.com/gallery?resolution=75
...
{
"id": 1001,
"thumbnail": "http://lh5.ggpht.com/EEIl3_hedJSAT4RscoceShtWy3DE4WGKXXNp4rusI-kpzWfDIbzrWx1KVywf9YqGI-elD_k4xg8bUemeRTtLgtAPOlVc0kpw=s75"
}
Obtaining a 150x150 thumbnail
GET http://example.com/gallery?resolution=150
...
{
"id": 1001,
"thumbnail": "http://lh5.ggpht.com/EEIl3_hedJSAT4RscoceShtWy3DE4WGKXXNp4rusI-kpzWfDIbzrWx1KVywf9YqGI-elD_k4xg8bUemeRTtLgtAPOlVc0kpw=s150"
}
I have seen seen a lot of api's that doesn't support custom sizes, e.g. the flickr api support small, medium, large and original size.
Can this api be improved somehow?
Well you could have fixed values like the Flickr API (Small, Medium, Large and Original). What you could do is pre-render and store them which means you can directly grab them off the disk or cache rather than rendering them on the fly. Your Request URL can then look something like:
GET http://example.com/gallery?resolution=small
However if you want to provide the most flexibility, you should continue with the way you are doing the API now and allow the API User to specify the square size. You should have a fixed maximum built in to stop abuse. An example of this would be the Gravatar API which has a fixed maximum of 512px and size can be specified with the s=
parameter in the URL.
At the end it depends on your cost/benefit ratio. Would it be substantially more difficult to maintain a flexible size API rather than a pre-rendered fixed size(s) API and does rendering on the fly need more investment in Servers/RAM/Bandwidth etc.
精彩评论