smoope / SwiftyTraverson

Other
7 stars 3 forks source link

HAL embedding #1

Open maxsz opened 8 years ago

maxsz commented 8 years ago

Hi, great work!

HAL supports embedding resources (https://github.com/mikekelly/hal_specification/blob/master/hal_specification.md) using the special _embedded keyword. This has the benefit of saving some network requests. Are you planning to support these?

vicmosin commented 8 years ago

Hi, thanks! What exactly do you mean? All the response is deserialized either to JSON or Dictionary. From there you can retrieve it. Or your idea is to have some shortcut to _embedded collection?

maxsz commented 8 years ago

My first thought was, that a .follow(...).get { } combination could omit the actual network request, when the _embedded collection already contains the data from the link.

vicmosin commented 8 years ago

Ok, now I get it. But I wouldn't do. My believe is that traverson's duty is to do very simple thing - follow the links provided by server. Adding logics such as checking whether the embedded collection contains needed resource somewhere between traversing the links will create more issues. How will we identify such resource? What if embedded resource differs from actual one? How client will know whether the resource was retrieved from embedded or from real call? All this will complicate the traversing process as well as produce possible side effects. If we are talking about performance, nobody said you can't use caching policies, Etag for example, so this unnecessary last request will be cached anyway.

vicmosin commented 8 years ago

But I'd like to hear some ideas of how it might be implemented... Even in pseudo code)) Thanks for your response

systemfreund commented 8 years ago

If i might chime in: We actually are using _embedded resources, see here https://developers.smoope.com/docs/rest-api#_example_response_4

However, our API responses will never contain a relation X in both sections (_links, _embedded) at the same time. Therefore there's no need for the traverson client to be smart about it.

Example, which you won't happen in our API:

{
   "_links": {
       "user" : {
           "href" : "https://api.smoope.com/users/a8c5438f-41ed-4bf8-8fc8-b8e187d2280b"
       },
   },

   "_embedded": {
       "user": {
           "_links": {
               "self" : {
                   "href" : "https://api.smoope.com/users/a8c5438f-41ed-4bf8-8fc8-b8e187d2280b"
               },
           },

           "name": "Gregor Gysi",
           [...]
       }
   }
}

But, I agree of course that the traverson client should consider the _embedded resources in the .follow() call. Example:

{
   "_embedded": {
       "user": {
           "_links": {
               "self" : {
                   "href" : "https://api.smoope.com/users/a8c5438f-41ed-4bf8-8fc8-b8e187d2280b"
               },
           },

           "name": "Gregor Gysi",
           [...]
       }
   }
}

Here resource.follow("user") should indeed directly return the user resource which is contained in the parent resource without doing any network calls.