Add support to upload media
parent
b4e38f2b3f
commit
6b3fb0c076
|
@ -105,11 +105,11 @@ defmodule Hunter do
|
|||
## Parameters
|
||||
|
||||
* `conn` - connection credentials
|
||||
* `file`
|
||||
* `file` - media to be uploaded
|
||||
|
||||
"""
|
||||
@spec upload_media(Hunter.Client.t, Path.t) :: Hunter.Media.t
|
||||
def upload_media(conn, file), do: Hunter.Media.upload_media(conn, file)
|
||||
@spec upload_media(Hunter.Client.t, Path.t) :: Hunter.Attachment.t
|
||||
def upload_media(conn, file), do: Hunter.Attachment.upload_media(conn, file)
|
||||
|
||||
@doc """
|
||||
Get the relationships of authenticated user towards given other users
|
||||
|
|
|
@ -1,4 +1,8 @@
|
|||
defmodule Hunter.Api do
|
||||
@moduledoc """
|
||||
Hunter API contract
|
||||
"""
|
||||
|
||||
## Account
|
||||
|
||||
@doc """
|
||||
|
@ -77,11 +81,10 @@ defmodule Hunter.Api do
|
|||
## Parameters
|
||||
|
||||
* `conn` - connection credentials
|
||||
* `file` -
|
||||
* `file` - media to be uploaded
|
||||
|
||||
"""
|
||||
@callback upload_media(conn :: Hunter.Client.t, file :: Path.t) :: Hunter.Media.t
|
||||
|
||||
@callback upload_media(conn :: Hunter.Client.t, file :: Path.t) :: Hunter.Attachment.t
|
||||
|
||||
## Relationship
|
||||
|
||||
|
|
|
@ -1,4 +1,8 @@
|
|||
defmodule Hunter.Api.HTTPClient do
|
||||
@moduledoc """
|
||||
HTTP Client for Hunter
|
||||
"""
|
||||
|
||||
@behaviour Hunter.Api
|
||||
|
||||
def verify_credentials(%Hunter.Client{base_url: base_url} = conn) do
|
||||
|
@ -36,10 +40,8 @@ defmodule Hunter.Api.HTTPClient do
|
|||
end
|
||||
|
||||
def upload_media(%Hunter.Client{base_url: base_url} = conn, file) do
|
||||
payload = Poison.encode!(%{file: file})
|
||||
|
||||
{:ok, %HTTPoison.Response{body: body, status_code: 200}} = HTTPoison.post(base_url <> "/api/v1/media", payload, [{"Content-Type", "application/json"} | get_headers(conn)])
|
||||
Poison.decode!(body, as: %Hunter.Media{})
|
||||
{:ok, %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
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
defmodule Hunter.Attribute do
|
||||
defmodule Hunter.Attachment do
|
||||
@moduledoc """
|
||||
Attribute entity
|
||||
Attachment entity
|
||||
|
||||
This module defines a `Hunter.Attribute` struct and the main functions
|
||||
for working with Attributes.
|
||||
This module defines a `Hunter.Attachment` struct and the main functions
|
||||
for working with Attachments.
|
||||
|
||||
## Fields
|
||||
|
||||
|
@ -15,6 +15,8 @@ defmodule Hunter.Attribute do
|
|||
* `text_url` - Shorter URL for the image, for insertion into text (only present on local images)
|
||||
|
||||
"""
|
||||
@hunter_api Application.get_env(:hunter, :hunter_api)
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
id: non_neg_integer,
|
||||
type: String.t,
|
||||
|
@ -26,4 +28,18 @@ defmodule Hunter.Attribute do
|
|||
|
||||
@derive [Poison.Encoder]
|
||||
defstruct [:id, :type, :url, :remote_url, :preview_url, :text_url]
|
||||
|
||||
@doc """
|
||||
Upload a media attachment
|
||||
|
||||
## Parameters
|
||||
|
||||
* `conn` - connection credentials
|
||||
* `file` - media to be uploaded
|
||||
|
||||
"""
|
||||
@spec upload_media(Hunter.Client.t, Path.t) :: Hunter.Attachment.t
|
||||
def upload_media(conn, file) do
|
||||
@hunter_api.upload_media(conn, file)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,28 +0,0 @@
|
|||
defmodule Hunter.Media do
|
||||
|
||||
@hunter_api Application.get_env(:hunter, :hunter_api)
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
id: non_neg_integer,
|
||||
url: URI.t,
|
||||
preview_url: URI.t,
|
||||
type: String.t
|
||||
}
|
||||
|
||||
@derive [Poison.Encoder]
|
||||
defstruct [:id, :url, :preview_url, :type]
|
||||
|
||||
@doc """
|
||||
Upload a media file
|
||||
|
||||
## Parameters
|
||||
|
||||
* `conn` - Connection credentials
|
||||
* `file` - [HTTP::FormData::File]
|
||||
|
||||
"""
|
||||
@spec upload_media(Hunter.Client.t, Path.t) :: Hunter.Media.t
|
||||
def upload_media(conn, file) do
|
||||
@hunter_api.upload_media(conn, file)
|
||||
end
|
||||
end
|
|
@ -26,7 +26,7 @@ defmodule Hunter.Api.InMemory do
|
|||
end
|
||||
|
||||
def upload_media(_, _) do
|
||||
%Hunter.Media{}
|
||||
%Hunter.Attachment{}
|
||||
end
|
||||
|
||||
def relationships(_) do
|
||||
|
|
Loading…
Reference in New Issue