Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Response structure on save #23

Open
realsurfer opened this issue Nov 1, 2013 · 8 comments
Open

Response structure on save #23

realsurfer opened this issue Nov 1, 2013 · 8 comments

Comments

@realsurfer
Copy link

Hi,

First, thanks for creating this great adapter!
On fetching models and collections I have been serving the objects enclosed in an array like below:
{"meta":"","users":[{"username":"Michael","id":"1"}]}

However when creating a new model and posting to the api the adapter expects the response object without the array. Is this the expected behavior? Seems a little inconsistent.

Thanks,
Michael

@viezel
Copy link
Owner

viezel commented Nov 2, 2013

Hi there.
I'm not sure I follow. Do you want only the user object from the server? If so then use the property parentNode to specify how you want the adapter to parse. Eg make the array keys into different model attributes.

@realsurfer
Copy link
Author

Hi,
I am already using the parentNode property to access the "users" node.
My api follows the guidelines of http://jsonapi.org and as such my objects are always returned enclosed in an array.

For GET on "/users/1" I return "[{"username":"Michael","id":"1"}]".
For GET on "/users" I return "[{"username":"Michael","id":"1"}, {"username":"Tony","id":"2"}]".

So far so good but when I do a POST on "/users" I should return "[{"username":"Michael","id":"1"}]" as above but in your adapter I must return "{"username":"Michael","id":"1"}" without the array to make it work.

I hope this clears things up?
Thanks!

@viezel
Copy link
Owner

viezel commented Nov 2, 2013

@realsurfer, that is correct.
But I might be wrong here, but the Restful API specifications states the a single item is returned without an array.

Look here:
https://developers.facebook.com/tools/explorer?method=GET&path=me
Here I get my own user returned by Facebook API. It returned a single object, not an array.

So I dont believe your right.

Cheers,
Mads

@realsurfer
Copy link
Author

Well there is no official spec which is why no api is the other like.
But, when I test a GET on "/users/1" without the array your adapter doesn't handle it correctly which is why it's inconsistent. From what I can tell a GET expects a single object to be enclosed in an array and a POST does not. I could be wrong but this is the behavior I am seeing.
My suggestion is that both requests should expect the same response or add the functionality to handle both use cases with single objects which would add more flexibility to the adapter.

Thanks!
Michael

@viezel
Copy link
Owner

viezel commented Nov 2, 2013

What are you using server side?
In Napp we are using SLIM Framework which is a php rest library. It claims that it follows the standards.
We have a lot of apps where we do the entire CRUD operation, using this adapter. So this works for us.

What are you using?

@realsurfer
Copy link
Author

I am using the Laravel framework. But the framework doesn't impose any structure for the response body, that is entirely up to the developer. I can't see that the SLIM framework does anything different in this regard. There is no official restful spec just a lot of guidelines and everyone doing their own implementation so adding flexibility would make the adapter work in more use cases.

@viezel
Copy link
Owner

viezel commented Nov 3, 2013

I see your point.

I would be interested in a PR, where you make the parsing more general. Flexibility would be very great to have built in.
The parsing code is quite basic javascript. have a look here: https://github.com/viezel/napp.alloy.adapter.restapi/blob/master/restapi.js#L194-L205

@realsurfer
Copy link
Author

I'll look into it as soon as I can and see what I can contribute. It would be nice if the adapter could automatically handle both use cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants