Update HTTP client to comply with new API contract

master
Milton Mazzarri 2017-04-10 21:51:20 -05:00
parent e686683b56
commit 1fd9f5c8a7
No known key found for this signature in database
GPG Key ID: CF3DE6E356E17F1E
1 changed files with 153 additions and 51 deletions

View File

@ -6,118 +6,128 @@ defmodule Hunter.Api.HTTPClient do
@behaviour Hunter.Api
def verify_credentials(%Hunter.Client{base_url: base_url} = conn) do
{:ok, %HTTPoison.Response{body: body, status_code: 200}} = HTTPoison.get(base_url <> "/api/v1/accounts/verify_credentials", get_headers(conn))
%HTTPoison.Response{body: body, status_code: 200} = HTTPoison.get!(base_url <> "/api/v1/accounts/verify_credentials", get_headers(conn))
Poison.decode!(body, as: %Hunter.Account{})
end
def account(%Hunter.Client{base_url: base_url} = conn, id) do
{:ok, %HTTPoison.Response{body: body, status_code: 200}} = HTTPoison.get(base_url <> "/api/v1/accounts/#{id}", get_headers(conn))
%HTTPoison.Response{body: body, status_code: 200} = HTTPoison.get!(base_url <> "/api/v1/accounts/#{id}", get_headers(conn))
Poison.decode!(body, as: %Hunter.Account{})
end
def followers(%Hunter.Client{base_url: base_url} = conn, id) do
{:ok, %HTTPoison.Response{body: body, status_code: 200}} = HTTPoison.get(base_url <> "/api/v1/accounts/#{id}/followers", get_headers(conn))
%HTTPoison.Response{body: body, status_code: 200} = HTTPoison.get!(base_url <> "/api/v1/accounts/#{id}/followers", get_headers(conn))
Poison.decode!(body, as: [%Hunter.Account{}])
end
def following(%Hunter.Client{base_url: base_url} = conn, id) do
{:ok, %HTTPoison.Response{body: body, status_code: 200}} = HTTPoison.get(base_url <> "/api/v1/accounts/#{id}/following", get_headers(conn))
%HTTPoison.Response{body: body, status_code: 200} = HTTPoison.get!(base_url <> "/api/v1/accounts/#{id}/following", get_headers(conn))
Poison.decode!(body, as: [%Hunter.Account{}])
end
def follow_by_uri(%Hunter.Client{base_url: base_url} = conn, uri) do
payload = Poison.encode!(%{uri: uri})
{:ok, %HTTPoison.Response{body: body, status_code: 200}} = HTTPoison.post(base_url <> "/api/v1/follows", payload, [{"Content-Type", "application/json"} | get_headers(conn)])
%HTTPoison.Response{body: body, status_code: 200} = HTTPoison.post!(base_url <> "/api/v1/follows", payload, [{"Content-Type", "application/json"} | get_headers(conn)])
Poison.decode!(body, as: %Hunter.Account{})
end
def search_account(%Hunter.Client{base_url: base_url} = conn, options) do
%HTTPoison.Response{body: body, status_code: 200} = HTTPoison.get!(base_url <> "/api/v1/accounts/search", [{"Content-Type", "application/json"} | get_headers(conn)], options)
Poison.decode!(body, as: [%Hunter.Account{}])
end
def blocks(%Hunter.Client{base_url: base_url} = conn) do
%HTTPoison.Response{body: body, status_code: 200} = HTTPoison.get!(base_url <> "/api/v1/blocks" , [{"Content-Type", "application/json"} | get_headers(conn)])
Poison.decode!(body, as: [%Hunter.Account{}])
end
def follow_requests(%Hunter.Client{base_url: base_url} = conn) do
%HTTPoison.Response{body: body, status_code: 200} = HTTPoison.get!(base_url <> "/api/v1/follow_requests" , [{"Content-Type", "application/json"} | get_headers(conn)])
Poison.decode!(body, as: [%Hunter.Account{}])
end
def mutes(%Hunter.Client{base_url: base_url} = conn) do
%HTTPoison.Response{body: body, status_code: 200} = HTTPoison.get!(base_url <> "/api/v1/mutes" , [{"Content-Type", "application/json"} | get_headers(conn)])
Poison.decode!(body, as: [%Hunter.Account{}])
end
def create_app(%Hunter.Client{base_url: base_url} = conn, name, redirect_uri, scopes, website) do
payload = Poison.encode!(%{client_name: name, redirect_uris: redirect_uri, scopes: scopes, website: website})
{:ok, %HTTPoison.Response{body: body, status_code: 200}} = HTTPoison.post(base_url <> "/api/v1/apps", payload, [{"Content-Type", "application/json"} | get_headers(conn)])
%HTTPoison.Response{body: body, status_code: 200} = HTTPoison.post!(base_url <> "/api/v1/apps", payload, [{"Content-Type", "application/json"} | get_headers(conn)])
Poison.decode!(body, as: %Hunter.Application{})
end
def upload_media(%Hunter.Client{base_url: base_url} = conn, file) do
{:ok, %HTTPoison.Response{body: body, status_code: 200}} = HTTPoison.post(base_url <> "/api/v1/media", {:file, file}, get_headers(conn))
%HTTPoison.Response{body: body, status_code: 200} = HTTPoison.post!(base_url <> "/api/v1/media", {:file, file}, get_headers(conn))
Poison.decode!(body, as: %Hunter.Attachment{})
end
def relationships(_ids) do
# :: [Hunter.Relationship.t]
# @return [Hunter::Collection<Hunter::Relationship>]
# perform_request_with_collection(:get, '', array_param(:id, ids), Hunter::Relationship)
HTTPoison.get("/api/v1/accounts/relationships")
def relationships(%Hunter.Client{base_url: base_url} = conn, ids) do
%HTTPoison.Response{body: body, status_code: 200} = HTTPoison.get!(base_url <> "/api/v1/accounts/relationships", [{"Content-Type", "application/json"} | get_headers(conn)], [id: ids])
# Poison.decode!(body, as: [%Hunter.Relationship{}])
Poison.decode!(body, as: [%Hunter.Relationship{}])
end
def follow(%Hunter.Client{base_url: base_url} = conn, id) do
payload = Poison.encode!(%{})
{:ok, %HTTPoison.Response{body: body, status_code: 200}} = HTTPoison.post(base_url <> "/api/v1/accounts/#{id}/follow", payload, [{"Content-Type", "application/json"} | get_headers(conn)])
%HTTPoison.Response{body: body, status_code: 200} = HTTPoison.post!(base_url <> "/api/v1/accounts/#{id}/follow", payload, [{"Content-Type", "application/json"} | get_headers(conn)])
Poison.decode!(body, as: %Hunter.Relationship{})
end
def unfollow(%Hunter.Client{base_url: base_url} = conn, id) do
payload = Poison.encode!(%{})
{:ok, %HTTPoison.Response{body: body, status_code: 200}} = HTTPoison.post(base_url <> "/api/v1/accounts/#{id}/unfollow", payload, [{"Content-Type", "application/json"} | get_headers(conn)])
%HTTPoison.Response{body: body, status_code: 200} = HTTPoison.post!(base_url <> "/api/v1/accounts/#{id}/unfollow", payload, [{"Content-Type", "application/json"} | get_headers(conn)])
Poison.decode!(body, as: %Hunter.Relationship{})
end
def block(%Hunter.Client{base_url: base_url} = conn, id) do
payload = Poison.encode!(%{})
{:ok, %HTTPoison.Response{body: body, status_code: 200}} = HTTPoison.post(base_url <> "/api/v1/accounts/#{id}/block", payload, [{"Content-Type", "application/json"} | get_headers(conn)])
%HTTPoison.Response{body: body, status_code: 200} = HTTPoison.post!(base_url <> "/api/v1/accounts/#{id}/block", payload, [{"Content-Type", "application/json"} | get_headers(conn)])
Poison.decode!(body, as: %Hunter.Relationship{})
end
def unblock(%Hunter.Client{base_url: base_url} = conn, id) do
payload = Poison.encode!(%{})
{:ok, %HTTPoison.Response{body: body, status_code: 200}} = HTTPoison.post(base_url <> "/api/v1/accounts/#{id}/unblock", payload, [{"Content-Type", "application/json"} | get_headers(conn)])
%HTTPoison.Response{body: body, status_code: 200} = HTTPoison.post!(base_url <> "/api/v1/accounts/#{id}/unblock", payload, [{"Content-Type", "application/json"} | get_headers(conn)])
Poison.decode!(body, as: %Hunter.Relationship{})
end
def mute(%Hunter.Client{base_url: base_url} = conn, id) do
payload = Poison.encode!(%{})
{:ok, %HTTPoison.Response{body: body, status_code: 200}} = HTTPoison.post(base_url <> "/api/v1/accounts/#{id}/mute", payload, [{"Content-Type", "application/json"} | get_headers(conn)])
%HTTPoison.Response{body: body, status_code: 200} = HTTPoison.post!(base_url <> "/api/v1/accounts/#{id}/mute", payload, [{"Content-Type", "application/json"} | get_headers(conn)])
Poison.decode!(body, as: %Hunter.Relationship{})
end
def unmute(%Hunter.Client{base_url: base_url} = conn, id) do
payload = Poison.encode!(%{})
{:ok, %HTTPoison.Response{body: body, status_code: 200}} = HTTPoison.post(base_url <> "/api/v1/accounts/#{id}/unmute", payload, [{"Content-Type", "application/json"} | get_headers(conn)])
%HTTPoison.Response{body: body, status_code: 200} = HTTPoison.post!(base_url <> "/api/v1/accounts/#{id}/unmute", payload, [{"Content-Type", "application/json"} | get_headers(conn)])
Poison.decode!(body, as: %Hunter.Relationship{})
end
def search(_conn, _query, _options) do
# :: Hunter.Result.t
def search(%Hunter.Client{base_url: base_url} = conn, query, options) do
options = Keyword.merge(options, [q: query])
# @return [Hunter::Results] If q is a URL, Hunter will
# attempt to fetch the provided account or status. Otherwise, it
# will do a local account and hashtag search.
# opts = {
# q: query,
# }.merge(options)
# perform_request_with_object(:get, '/api/v1/search', opts, Hunter::Results)
%HTTPoison.Response{body: body, status_code: 200} = HTTPoison.get!(base_url <> "/api/v1/search", get_headers(conn), options)
Poison.decode!(body, as: %Hunter.Result{accounts: [%Hunter.Account{}], statuses: [%Hunter.Status{}]})
end
def create_status(%Hunter.Client{base_url: base_url} = conn, text, in_reply_to_id, _media_ids) do
payload = Poison.encode!(%{status: text, in_reply_to_id: in_reply_to_id})
{:ok, %HTTPoison.Response{body: body, status_code: 200}} = HTTPoison.post(base_url <> "/api/v1/statuses", payload, [{"Content-Type", "application/json"} | get_headers(conn)])
Poison.decode!(body, as: %Hunter.Status{})
%HTTPoison.Response{body: body, status_code: 200} = HTTPoison.post!(base_url <> "/api/v1/statuses", payload, [{"Content-Type", "application/json"} | get_headers(conn)])
to_status(body)
end
def status(%Hunter.Client{base_url: base_url} = conn, id) do
{:ok, %HTTPoison.Response{body: body, status_code: 200}} = HTTPoison.get(base_url <> "/api/v1/statuses/#{id}", get_headers(conn))
%HTTPoison.Response{body: body, status_code: 200} = HTTPoison.get!(base_url <> "/api/v1/statuses/#{id}", get_headers(conn))
Poison.decode(body, as: %Hunter.Status{})
end
@ -133,58 +143,150 @@ defmodule Hunter.Api.HTTPClient do
def reblog(%Hunter.Client{base_url: base_url} = conn, id) do
payload = Poison.encode!(%{})
{:ok, %HTTPoison.Response{body: body, status_code: 200}} = HTTPoison.post(base_url <> "/api/v1/statuses/#{id}/reblog", payload, [{"Content-Type", "application/json"} | get_headers(conn)])
Poison.decode!(body, as: %Hunter.Status{})
%HTTPoison.Response{body: body, status_code: 200} = HTTPoison.post!(base_url <> "/api/v1/statuses/#{id}/reblog", payload, [{"Content-Type", "application/json"} | get_headers(conn)])
to_status(body)
end
def unreblog(%Hunter.Client{base_url: base_url} = conn, id) do
payload = Poison.encode!(%{})
{:ok, %HTTPoison.Response{body: body, status_code: 200}} = HTTPoison.post(base_url <> "/api/v1/statuses/#{id}/unreblog", payload, [{"Content-Type", "application/json"} | get_headers(conn)])
Poison.decode!(body, as: %Hunter.Status{})
%HTTPoison.Response{body: body, status_code: 200} = HTTPoison.post!(base_url <> "/api/v1/statuses/#{id}/unreblog", payload, [{"Content-Type", "application/json"} | get_headers(conn)])
to_status(body)
end
def favourite(%Hunter.Client{base_url: base_url} = conn, id) do
payload = Poison.encode!(%{})
{:ok, %HTTPoison.Response{body: body, status_code: 200}} = HTTPoison.post(base_url <> "/api/v1/statuses/#{id}/favourite", payload, [{"Content-Type", "application/json"} | get_headers(conn)])
Poison.decode!(body, as: %Hunter.Status{})
%HTTPoison.Response{body: body, status_code: 200} = HTTPoison.post!(base_url <> "/api/v1/statuses/#{id}/favourite", payload, [{"Content-Type", "application/json"} | get_headers(conn)])
to_status(body)
end
def unfavourite(%Hunter.Client{base_url: base_url} = conn, id) do
payload = Poison.encode!(%{})
{:ok, %HTTPoison.Response{body: body, status_code: 200}} = HTTPoison.post(base_url <> "/api/v1/statuses/#{id}/unfavourite", payload, [{"Content-Type", "application/json"} | get_headers(conn)])
Poison.decode!(body, as: %Hunter.Status{})
%HTTPoison.Response{body: body, status_code: 200} = HTTPoison.post!(base_url <> "/api/v1/statuses/#{id}/unfavourite", payload, [{"Content-Type", "application/json"} | get_headers(conn)])
to_status(body)
end
def favourites(%Hunter.Client{base_url: base_url} = conn) do
{:ok, %HTTPoison.Response{body: body, status_code: 200}} = HTTPoison.get(base_url <> "/api/v1/favourites", get_headers(conn))
Poison.decode!(body, as: [%Hunter.Status{}])
%HTTPoison.Response{body: body, status_code: 200} = HTTPoison.get!(base_url <> "/api/v1/favourites", get_headers(conn))
to_statuses(body)
end
@doc """
Get an account's statuses
## Options
* `only_media` - (optional): only return statuses that have media attachments
* `exclude_replies` - (optional): skip statuses that reply to other statuses
"""
def statuses(%Hunter.Client{base_url: base_url} = conn, account_id, options) do
{:ok, %HTTPoison.Response{body: body, status_code: 200}} = HTTPoison.get(base_url <> "/api/v1/accounts/#{account_id}/statuses", get_headers(conn), options)
Poison.decode!(body, as: [%Hunter.Status{}])
%HTTPoison.Response{body: body, status_code: 200} = HTTPoison.get!(base_url <> "/api/v1/accounts/#{account_id}/statuses", get_headers(conn), options)
to_statuses(body)
end
def home_timeline(%Hunter.Client{base_url: base_url} = conn, options) do
{:ok, %HTTPoison.Response{body: body, status_code: 200}} = HTTPoison.get(base_url <> "/api/v1/timelines/home", get_headers(conn), options)
Poison.decode!(body, as: [%Hunter.Status{}])
%HTTPoison.Response{body: body, status_code: 200} = HTTPoison.get!(base_url <> "/api/v1/timelines/home", get_headers(conn), options)
to_statuses(body)
end
def public_timeline(%Hunter.Client{base_url: base_url} = conn, options) do
{:ok, %HTTPoison.Response{body: body, status_code: 200}} = HTTPoison.get(base_url <> "/api/v1/timelines/public", get_headers(conn), options)
Poison.decode!(body, as: [%Hunter.Status{}])
%HTTPoison.Response{body: body, status_code: 200} = HTTPoison.get!(base_url <> "/api/v1/timelines/public", get_headers(conn), options)
to_statuses(body)
end
def hashtag_timeline(%Hunter.Client{base_url: base_url} = conn, hashtag, options) do
{:ok, %HTTPoison.Response{body: body, status_code: 200}} = HTTPoison.get(base_url <> "/api/v1/timelines/tag/#{hashtag}", get_headers(conn), options)
Poison.decode!(body, as: [%Hunter.Status{}])
%HTTPoison.Response{body: body, status_code: 200} = HTTPoison.get!(base_url <> "/api/v1/timelines/tag/#{hashtag}", get_headers(conn), options)
to_statuses(body)
end
@spec instance_info(Hunter.Client.t) :: Hunter.Instance.t
def instance_info(%Hunter.Client{base_url: base_url} = conn) do
%HTTPoison.Response{body: body, status_code: 200} = HTTPoison.get!(base_url <> "/api/v1/instance", get_headers(conn))
Poison.decode!(body, as: %Hunter.Instance{})
end
def notifications(%Hunter.Client{base_url: base_url} = conn) do
%HTTPoison.Response{body: body, status_code: 200} = HTTPoison.get!(base_url <> "/api/v1/notifications", get_headers(conn))
to_notifications(body)
end
def notification(%Hunter.Client{base_url: base_url} = conn, id) do
%HTTPoison.Response{body: body, status_code: 200} = HTTPoison.get!(base_url <> "/api/v1/notifications/#{id}", get_headers(conn))
to_notification(body)
end
def clear_notifications(%Hunter.Client{base_url: base_url} = conn) do
payload = Poison.encode!(%{})
%HTTPoison.Response{body: body, status_code: 200} = HTTPoison.post!(base_url <> "/api/v1/notifications/clear", payload, [{"Content-Type", "application/json"} | get_headers(conn)])
body
end
def reports(%Hunter.Client{base_url: base_url} = conn) do
%HTTPoison.Response{body: body, status_code: 200} = HTTPoison.get!(base_url <> "/api/v1/reports", get_headers(conn))
Poison.decode!(body, as: [%Hunter.Report{}])
end
def report(%Hunter.Client{base_url: base_url} = conn, account_id, status_ids, comment) do
payload = Poison.encode!(%{
account_id: account_id,
status_ids: status_ids,
comment: comment
})
%HTTPoison.Response{body: body, status_code: 200} = HTTPoison.post!(base_url <> "/api/v1/reports", payload, [{"Content-Type", "application/json"} | get_headers(conn)])
Poison.decode!(body, as: %Hunter.Report{})
end
def status_context(%Hunter.Client{base_url: base_url} = conn, id) do
%HTTPoison.Response{body: body, status_code: 200} = HTTPoison.get!(base_url <> "/api/v1/statuses/#{id}/context", get_headers(conn))
Poison.decode!(body, as: %Hunter.Context{ancestors: [%Hunter.Status{}], descendants: [%Hunter.Status{}]})
end
def card_by_status(%Hunter.Client{base_url: base_url} = conn, id) do
%HTTPoison.Response{body: body, status_code: 200} = HTTPoison.get!(base_url <> "/api/v1/statuses/#{id}/card", get_headers(conn))
Poison.decode!(body, as: %Hunter.Card{})
end
## Helpers
defp get_headers(%Hunter.Client{bearer_token: token}) do
[{"Authorization", "Bearer #{token}"}]
end
defp to_status(body) do
Poison.decode!(body, as: status_nested_struct())
end
defp to_statuses(body) do
Poison.decode!(body, as: [status_nested_struct()])
end
defp status_nested_struct do
%Hunter.Status{
account: %Hunter.Account{},
reblog: %Hunter.Status{},
media_attachments: [%Hunter.Attachment{}],
mentions: [%Hunter.Mention{}],
tags: [%Hunter.Tag{}],
application: %Hunter.Application{}
}
end
defp to_notification(body) do
Poison.decode!(body, as: notification_nested_struct())
end
defp to_notifications(body) do
Poison.decode!(body, as: [notification_nested_struct()])
end
defp notification_nested_struct do
%Hunter.Notification{
account: %Hunter.Account{},
status: %Hunter.Status{}
}
end
end