Where to put url attributes to external services in a resource object?


#1

Hi,

because it is not clear to me how to handle urls to external services (= not JSON API resources). Where should I put it into?

I have a resource type = “user” and it should have a url for the avatar image. and also 2 additional urls for smaller sizes so our clients doesn’t have to resize it on the fly themselfs. The avatar image urls is just a simple url pointing to a .jpeg or .png on some file server. So those urls is not pointing to one of our JSON API resource objects.

We have come up with different variations, but which is one is the correct one? Or are they all wrong? :wink:

Option 1: Directly in attributes

{
  "data": [
    {
      "id": "321",
      "type": "user",
      "attributes": {
        "first_name": "Max",
        "last_name": "Mustermann",
        "avatar_big_url": "http://www.some-server.com/blub/avatars/ea734def.jpg",
        "avatar_medium_url": "http://www.some-server.com/blub/avatars/66abcdef.jpg",
        "avatar_small_url": "http://www.some-server.com/blub/avatars/eeefff736.jpg"
      }
    }
  ]
}

Option 2: Use Meta field in Relationships

{
  "data": [
    {
      "id": "321",
      "type": "user",
      "attributes": {
        "first_name": "Max",
        "last_name": "Mustermann"
      },
      "relationships": {
        "avatar_image": {
          "meta": {
            "big_url": "http://www.some-server.com/blub/avatars/ea734def.jpg",
            "medium_url": "http://www.some-server.com/blub/avatars/66abcdef.jpg",
            "small_url": "http://www.some-server.com/blub/avatars/eeefff736.jpg"
          }
        }
      }
    }
  ]
}

Option 3: Or do we need to convert it to a json resource?

{
  "data": [
    {
      "id": "321",
      "type": "user",
      "attributes": {
        "first_name": "Max",
        "last_name": "Mustermann"
      },
      "relationships": {
        "avatar_image": {
          "id": "321",
          "type": "avatar_image"
        }
      }
    }
  ],
  "included": [
    {
      "id": "321",
      "type": "avatar_image",
      "attributes": {
        "big_url": "http://www.some-server.com/blub/avatars/ea734def.jpg",
        "medium_url": "http://www.some-server.com/blub/avatars/66abcdef.jpg",
        "small_url": "http://www.some-server.com/blub/avatars/eeefff736.jpg"
      }
    }
  ]
}

#2

Option 1 is how I’ve handled this in the past (and would do again). It seems to be the simplest solution.