How to return random items REFUSED?

My project provides two types of resources:

  • Images
  • Tags

I would like clients to be able to request random images by their tags. For example: Give me random images labeled “New York” and “Winter.” What will the RESTful design look like in this case?

+7
rest random
Dec 30 '09 at 19:21
source share
5 answers

Summing up all the discussions in the comments and without changing my initial sentence, here is what I finally come up with:

You want to access images through tags; each tag refers to a set of images. Since this tag can be used much more than the other (for example, photos in New York are used much more than in Chicago), you should use the RESTful configuration, which allows you to cache, so you can cache photos in New York. IMHO, the solution will be:

  • Each image has a fixed URI:

    http://www.example.com/images/12345 
  • Each tag also has a URI:

     http://www.example.com/tags/New_York/random 

    This URI acts as a random image manager on the set; it returns 303 See another answer , redirecting to a random set image. By definition , this URI cannot be cached, but a fixed one should, and the browser should not understand that the redirection to the second resource is permanent, so it is optimal.

  • You can access the entire set through:

     http://www.example.com/tags/New_York 

    This access will result in a 300 Multiple Choices response; it returns the entire set (as a URI, not as an image!) to the browser, and the browser decides what to do with it.

  • You can also use the intersection of various tags:

     http://www.example.com/tags/New_York/Autumn/Manhattan/random http://www.example.com/tags/Autumn/Manhattan/New_York/random (equivalent to the previous one) http://www.example.com/tags/New_York/girls/Summer/random etc. 

So you have a fixed URI for each image, a fixed URI for each tag, and a set of photos associated with it, and a fixed URI for the random dispatcher that each tag has. You do not need to use any GET parameters as other potential solutions, therefore it is as RESTful as you can get.

+4
Dec 31 '09 at 14:11
source share

I struggled with this problem. As a result, we implemented HttpResponseRedirect, for example:

http://www.example.com/randomNewYorkImage

to a random New York image:

http://www.example.com/images/New_York/1234 .

The first resource can be conceived as a random image manager in New York. This solution will load more server since it will be requested two resources, but it is as RESTful as you can get.

Edited: In addition, if you perform caching, each image will be in the cache, and your server will go from sending the image to sending only redirects, as the cache will intercept the second request and, thus, facilitate the loading of your server.

+2
Dec 30 '09 at 19:35
source share

Multidimensional resource identification is complex.

Your resource is an image, so your URI. In addition, a particular image has a specific URI that never changes.

Your "by tag" is a non-identifying attribute of the resource. For this, a query string may appear.

Here is my first thought.

  • http://www.example.com/MyStuff/image/ id / - specific image by id
  • http://www.example.com/MyStuff/image/?tag= tagname - a random image with a given tag, implicitly, count=1 .
  • http://www.example.com/MyStuff/image/?tag= tagname &count=all - all images with the given tag in random order ( count=1 by default, which will give you an arbitrary image)
+1
Dec 30 '09 at 19:32
source share

I would do something like http://foo.com/image/tagged/sometag/random and stop sleeping over it.

0
Dec 30 '09 at 19:39
source share

I agree with the Triptych on this. In a sense, adding random code to the end of a URI makes it feel like an operation, but if it's attached to a tag, you really just clarify the context.

In my example:

/ image / tagged / sometag / random

image resource → tag area (all images with tags) → special tag (all images with X tag) → random (resource from the list of image areas with X tag)

0
Dec 30 '09 at 19:51
source share



All Articles