Last active
November 15, 2016 19:51
-
-
Save danmcclain/d91685810565f1d2bd75f69fa7f4f3cb to your computer and use it in GitHub Desktop.
JSON API client API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule SomeAPI.Client do | |
use JSONAPIClient | |
host "https://example.com" | |
namespace "api/v1" | |
register_type :users, SomeAPI.User | |
register_type :orgs, SomeAPI.Org | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule SomeAPI.Org do | |
use JSONAPIClient.Resource | |
attribute :name | |
relationship :users | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Linked relationship | |
users = SomeAPI.Client.get(:users, page: 1) | |
user = Enum.at users, 0 | |
user.orgs | |
# => %JSONAPI.RelationshipNotLoaded{} | |
user = SomeAPI.Client.load_relationship(user[0], :org) | |
user.orgs | |
# => [%SomeAPI.Org{}] | |
user.name | |
# => Jane Doe | |
# Sideloaded relationship | |
users = SomeAPI.Client.get(:users, page: 1) | |
user = Enum.at users, 0 | |
user.orgs | |
# => [%SomeAPI.Org{}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule SomeAPI.User do | |
use JSONAPIClient.Resource | |
attribute :name | |
relationship :orgs | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment