views:

215

answers:

3

Hello everybody,

I'm currently (I try to) designing a RESTful API for a social network. But I'm not sure if my current approach does still accord to the RESTful principles. I'd be glad if some brighter heads could give me some tips.

Suppose the following URI represents the name field of a user account:

people/{UserID}/profile/fields/name

But there are almost hundred possible fields. So I want the client to create its own field views or use predefined ones. Let's suppose that the following URI represents a predefined field view that includes the fields "name", "age", "gender":

utils/views/field-views/myFieldView

And because field views are kind of higher logic I don't want to mix support for field views into the "people/{UserID}/profile/fields" resource. Instead I want to do the following:

utils/views/field-views/myFieldView/{UserID}


Another example


Suppose we want to perform some quantity operations (hope that this is the right name for it in English). We have the following URIs whereas each of them points to a list of persons -- the friends of them:

GET people/exampleUID-1/relationships/friends
GET people/exampleUID-2/relationships/friends

And now we want to find out which of their friends are also friends of mine. So we do this:

GET people/myUID/relationships/intersections/{Value-1};{Value-2}

Whereas "{Value-1/2}" are the url encoded values of "people/exampleUID-1/friends" and "people/exampleUID-2/friends". And then we get back a representation of all people which are friends of all three persons.

Though Leonard Richardson & Sam Ruby state in their book "RESTful Web Services" that a RESTful design is somehow like an "extreme object oriented" approach, I think that my approach is object oriented and therefore accords to RESTful principles. Or am I wrong?

When not: Are such "object oriented" approaches generally encouraged when used with care and in order to avoid query-based REST-RPC hybrids?

Thanks for your feedback in advance,

peta

+2  A: 

I've never worked with REST, but I'd have assumed that GETting a profile resource at '''/people/{UserId}/profile''' would yield a document, in XML or JSON or something, that includes all the fields. Client-side I'd then ignore the fields I'm not interested in. Isn't that much nicer than having to (a) configure a personalised view on the server or (b) make lots of requests to fetch each field?

Richard Turner
Agree. Consider making user profile the unit of granularity rather than individual fields like name.
C. Dragon 76
Also, consider storing a version or timestamp field with each resource so you can detect and handle concurrent update attempts.
C. Dragon 76
Richard, C. Dragon: Thanks for your feedbacks, but when I make user profile the unit of granularity, I'll have a huge overhead everytime I want to consume/update specific fields. And there are approx. 50 possible field types. Iterating through a list of 700people in average and having to discard this overhead everytime isn't very efficient i think. That's why I want to introduce customized views -- to avoid unnecessary overload.
kishkash
@Richard, C.Dragon: I agree - "/people/{UserID}/profile" should definitely provide the client with all fields. However having an additional mechanism might be useful when working with larger resources - technically, it's a similar case as usage of pagination for constraining the view to only a subset of the resource. @C. Dragon 76: even though timestamps complicate the design, I have to agree. However I would store them in order to enable the API and its clients proper usage of caching (HEAD method).
MicE
+2  A: 
MicE
A: 

Hi MicE,

thanks for your clarifying answers. They are exactly what I was asking for. Unfortunately I hadn't the time to read "RESTful Web Services" from cover to cover; but I will catch it up as soon as possible. :-)

Regarding the first part of my post:

You're right. I incline to your first example, and without fields. I think that the I don't need it at all. (At the moment) Why do you suggest the use of OR (,) instead of AND (;)? Intuitively I'd use the AND operator because I want all three of them and not just the first one existing. (Like on page 121 the colorpairs example)

Regarding the second part:

With {Value-1/2} I meant only the url-encoded value of the URIs -- not their response data. :) Here I incline with you second example. Here it should be obvious that under the hood an algorithm is involed when calculating intersecting friends. And beside that I'm probably going to add some further operations to it.

peta

kishkash
**(#1)** The reason why I considered it an OR was that you want to get all three - name, or age, or gender. Using an AND would return none of them, as there is no intersection between these three fields (while in the second part we *are* looking for the intersection. **(#2)** That is one way how to approach it, but please note that if you add more logic to it, you should still aim to have the `intersection` keyword work consistently across your whole API (i.e. as the `field` keyword does).
MicE