rubygarage / api_struct

API wrapper builder with response serialization
MIT License
234 stars 21 forks source link

Working with non-restful entities #12

Open SteveRedka opened 4 years ago

SteveRedka commented 4 years ago

Let's assume we have an API that returns a list of entities. But list is paginated; so it has several helpful fields, and entity data itself is stored in separate key, say, results. It could look like this:

{
  "next": null,
  "previous": null,
  "total": 3,
  "per_page": 100,
  "results": [
    {
      "entity_id": 1,
      "datetime_stored": "2020-01-16T14:48:46.897379Z"
    },
    {
      "entity_id": 2,
      "datetime_stored": "2020-01-16T15:02:30.430281Z"
    },
    {
      "entity_id": 3,
      "datetime_stored": "2020-01-16T15:03:15.315064Z"
    }
  ]
}

What is the proper way for writing index for this?

SteveRedka commented 4 years ago

Currently I do it like this:

  class EntityClient < ApiStruct::Client
    rest_api 'entities'

    def index
      response = get(path: 'entities/', headers: SimpleAuthenticationHeader.call)
      response.fmap { |i| i[:results] }
    end
  end

However, README.md didn't say anything that I am supposed to know anything about monads. Either I do it wrong or it should be mentioned in docs.