Changeset View
Changeset View
Standalone View
Standalone View
lib/kolab_wopi/chwala.ex
defmodule KolabWopi.Chwala do | defmodule KolabWopi.Chwala do | ||||
use GenServer | |||||
use HTTPoison.Base | use HTTPoison.Base | ||||
require Logger | require Logger | ||||
@url "http://localhost/chwala/api/" | |||||
def start_link do | def start_link do | ||||
GenServer.start_link(__MODULE__, []) | GenServer.start_link(__MODULE__, []) | ||||
end | end | ||||
@doc """ | @doc """ | ||||
Fetches document metadata from Chwala | Fetches document metadata from Chwala | ||||
Args: | Args: | ||||
Show All 18 Lines | def document_info(token, file_id) do | ||||
params = %{ | params = %{ | ||||
token: token, | token: token, | ||||
id: file_id, | id: file_id, | ||||
} | } | ||||
# TODO: error handling | # TODO: error handling | ||||
response = get!(@url, [], params: params) | response = get!(base_url, [], params: params) | ||||
response.body | response.body | ||||
end | end | ||||
@doc """ | @doc """ | ||||
Fetches document content from Chwala | Fetches document content from Chwala | ||||
Args: | Args: | ||||
* `token` - Chwala access token | * `token` - Chwala access token | ||||
Show All 11 Lines | def document_get(token, file_id) do | ||||
# In case of an error http code will be 500 | # In case of an error http code will be 500 | ||||
# On success response body will contain file content | # On success response body will contain file content | ||||
params = %{ | params = %{ | ||||
token: token, | token: token, | ||||
id: file_id, | id: file_id, | ||||
} | } | ||||
response = get!(@url, [], params: params) | response = get!(base_url, [], params: params) | ||||
response.body | response.body | ||||
end | end | ||||
@doc """ | @doc """ | ||||
Updates document content in Chwala | Updates document content in Chwala | ||||
Args: | Args: | ||||
* `token` - Chwala access token | * `token` - Chwala access token | ||||
Show All 16 Lines | def document_put(token, file_id, body) do | ||||
params = %{ | params = %{ | ||||
token: token, | token: token, | ||||
id: file_id, | id: file_id, | ||||
} | } | ||||
# TODO: use streams to pass the file body? | # TODO: use streams to pass the file body? | ||||
response = put!(@url, body, [], params: params) | response = put!(base_url, body, [], params: params) | ||||
end | end | ||||
@doc """ | @doc """ | ||||
Renames the document | Renames the document | ||||
Args: | Args: | ||||
* `token` - Chwala access token | * `token` - Chwala access token | ||||
* `file_id` - File identifier | * `file_id` - File identifier | ||||
Show All 20 Lines | defmodule KolabWopi.Chwala do | ||||
# Decodes JSON response | # Decodes JSON response | ||||
# TODO: What if the response body is expected to be not JSON | # TODO: What if the response body is expected to be not JSON | ||||
# but binary data, e.g. in document_get | # but binary data, e.g. in document_get | ||||
defp process_response_body(body) do | defp process_response_body(body) do | ||||
body | body | ||||
|> Poison.decode! | |> Poison.decode! | ||||
|> Enum.map(fn({k, v}) -> {String.to_atom(k), v} end) | |> Enum.map(fn({k, v}) -> {String.to_atom(k), v} end) | ||||
end | end | ||||
# Returns the base URL for the chwala implementation | |||||
defp base_url() do | |||||
Application.get_env(:kolab_wopi, :chwala_base_url) | |||||
end | |||||
end | end |