inaka / Dayron

A repository `similar` to Ecto.Repo that maps to an underlying http client, sending requests to an external rest api instead of a database
http://inaka.net/blog/2016/05/24/introducing-dayron/
Apache License 2.0
159 stars 20 forks source link

Supporting different json list structure when retrieving data #62

Open RaphSfeir opened 7 years ago

RaphSfeir commented 7 years ago

Hey there,

Currently, it seems that Dayron only supports data retrieval of endpoints that look like this:

[
    { "id": 0, "name": "Elem 1"},
    { "id": 1, "name": "Elem 2"}
]

Unfortunately I have to work with REST APIs that output data in a different way quite often. Some APIs add some additional metadata and therefore store the main result list in a specific property. Classic example:

{
"data": [
    { "id": 0, "name": "Elem 1"},
    { "id": 1, "name": "Elem 2"}
],
"count": 2
}

I made quick changes on my fork of Dayron that support this kind of data structure, those are tested in the model_test.ex file. I have also adressed this topic a bit in the README.MD file, as I feel it would have saved me a bit of time to have this specified in the readme.

Would you be interested in a pull request?

Note that some of our services also use the JSONApi.org specification, which is even more complex. Full support of JSONApi would probably be a big work (though if you are looking to build nested data mapping, you should check out how they handle it, it's quite interesting). However, I have also written small changes that allow Dayron to extract data from a JSONApi response. It's still quite early, it feels too unsure to push this publicly, but tell me if you're interested on this topic as well, as I will probably investigate it further.

Thanks

benoror commented 6 years ago

Same situation for ActiveModel resources from a RoR server (cc @marinsalinas):

{
  "post": {
    /...
  }
}
{
  "posts": [
    /...
  ]
}
benoror commented 6 years ago

Also would be nice JSON:API support, or at least documentation for creating/overriding the adapters: