diff --git a/config/config.exs b/config/config.exs index 14da6ae..e82be39 100644 --- a/config/config.exs +++ b/config/config.exs @@ -1,30 +1,33 @@ # This file is responsible for configuring your application # and its dependencies with the aid of the Mix.Config module. use Mix.Config # This configuration is loaded before any dependency and is restricted # to this project. If another project depends on this project, this # file won't be loaded nor affect the parent project. For this reason, # if you want to provide default values for your application for # 3rd-party users, it should be done in your "mix.exs" file. # You can configure for your application as: # # config :kolab_wopi, key: :value # # And access this configuration in your application as: # # Application.get_env(:kolab_wopi, :key) # # Or configure a 3rd-party app: # # config :logger, level: :info # # It is also possible to import configuration files, relative to this # directory. For example, you can emulate configuration per environment # by uncommenting the line below and defining dev.exs, test.exs and such. # Configuration from the imported file will override the ones defined # here (which is why it is important to import them last). # # import_config "#{Mix.env}.exs" + +config :kolab_wopi, + chwala_base_url: "http://localhost/chwala/api/" diff --git a/lib/kolab_wopi.ex b/lib/kolab_wopi.ex index 5aa110a..a6f19db 100644 --- a/lib/kolab_wopi.ex +++ b/lib/kolab_wopi.ex @@ -1,20 +1,19 @@ defmodule KolabWopi do use Application # See http://elixir-lang.org/docs/stable/elixir/Application.html # for more information on OTP Applications def start(_type, _args) do import Supervisor.Spec, warn: false children = [ # Define workers and child supervisors to be supervised - worker(__MODULE__.API, []), - worker(__MODULE__.Chwala, []) + worker(__MODULE__.API, []) ] # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html # for other strategies and supported options opts = [strategy: :one_for_one, name: __MODULE__.Supervisor] Supervisor.start_link(children, opts) end end diff --git a/lib/kolab_wopi/chwala.ex b/lib/kolab_wopi/chwala.ex index d3540e1..248e34b 100644 --- a/lib/kolab_wopi/chwala.ex +++ b/lib/kolab_wopi/chwala.ex @@ -1,141 +1,143 @@ defmodule KolabWopi.Chwala do - use GenServer use HTTPoison.Base require Logger - @url "http://localhost/chwala/api/" - def start_link do GenServer.start_link(__MODULE__, []) end @doc """ Fetches document metadata from Chwala Args: * `token` - Chwala access token * `file_id` - File identifier Returns List of tuples """ def document_info(token, file_id) do # Method: GET # Params: # method: "document_info" # id: file_id # token: token # # In case of an error response body will be: # [code: 403, reason: "Invalid session", status: "ERROR"] # on success it will be: # [status: "OK", result: []] # TODO: this request is not yet implemented in Chwala params = %{ token: token, id: file_id, } # TODO: error handling - response = get!(@url, [], params: params) + response = get!(base_url, [], params: params) response.body end @doc """ Fetches document content from Chwala Args: * `token` - Chwala access token * `file_id` - File identifier Returns binary """ def document_get(token, file_id) do # Method: GET # Params: # method: "document" # id: file_id # token: token # # In case of an error http code will be 500 # On success response body will contain file content params = %{ token: token, id: file_id, } - response = get!(@url, [], params: params) + response = get!(base_url, [], params: params) response.body end @doc """ Updates document content in Chwala Args: * `token` - Chwala access token * `file_id` - File identifier * `body` - File content Returns ??? """ def document_put(token, file_id, body) do # Method: PUT # Params: # method: "document" # id: file_id # token: token # # In case of an error response body will be e.g.: # [code: 403, reason: "Invalid session", status: "ERROR"] # On success it will be: # [status: "OK", result: []] params = %{ token: token, id: file_id, } # TODO: use streams to pass the file body? - response = put!(@url, body, [], params: params) + response = put!(base_url, body, [], params: params) end @doc """ Renames the document Args: * `token` - Chwala access token * `file_id` - File identifier * `name` - New file name Returns ??? """ def document_rename(token, file_id, name) do # Method: POST # Params: # method: "document_rename" # id: file_id # token: token # name: name # # In case of an error response body will be e.g.: # [code: 403, reason: "Invalid session", status: "ERROR"] # On success it will be: # [status: "OK", result: []] # TODO: not implemented in Chwala yet end # Decodes JSON response # TODO: What if the response body is expected to be not JSON # but binary data, e.g. in document_get defp process_response_body(body) do body |> Poison.decode! |> Enum.map(fn({k, v}) -> {String.to_atom(k), v} end) end + + # Returns the base URL for the chwala implementation + defp base_url() do + Application.get_env(:kolab_wopi, :chwala_base_url) + end end