Commit 531970e23805a98bfc4c6e4c5e18cddb80afc884

Stephen Moloney 2016-02-11T11:32:17

more queries for link operations added fpr hubic api

diff --git a/lib/hubic/hubic_api/auth.ex b/lib/hubic/hubic_api/auth.ex
index 313cddc..1d90c0e 100644
--- a/lib/hubic/hubic_api/auth.ex
+++ b/lib/hubic/hubic_api/auth.ex
@@ -22,7 +22,8 @@ defmodule ExOvh.Hubic.HubicApi.Auth do
   def prepare_request(client, {method, uri, params} = query) when method in [:get, :delete] do
     config = config(client)
     uri = uri(config, uri)
-    if params !== :nil and params !== "", do: uri = uri <> "?" <> URI.encode_query(params)
+    if params !== :nil and params !== "" and is_map(params), do: uri = uri <> "?" <> URI.encode_query(params)
+    if params !== :nil and params !== "" and is_map(params) === :false, do: uri = uri <> URI.encode_www_form(params)
     options = %{ headers: headers(method), timeout: @timeout }
     {method, uri, options}
   end
@@ -31,7 +32,6 @@ defmodule ExOvh.Hubic.HubicApi.Auth do
     config = config(client)
     uri = uri(config, uri)
     if params !== "" and params !== :nil and is_map(params), do: params = Poison.encode!(params)
-    if params !== "" and params !== :nil and is_map(params), do: params = Poison.encode!(params)
     options = %{ body: params, headers: headers(method), timeout: @timeout }
     {method, uri, options}
   end
@@ -80,7 +80,7 @@ defmodule ExOvh.Hubic.HubicApi.Auth do
 
   defp default_headers(), do: %{ "Authorization": "Bearer " <> Cache.get_token() }
   defp headers(method) when method in [:post, :put] do
-    Map.merge(default_headers(), %{ "Content-Type": "application/x-www-form-urlencoded" })
+    Map.merge(default_headers(), %{ "Content-Type": "application/json;charset=utf-8" })
   end
   defp headers(method) when method in [:get, :delete], do: default_headers()
 
diff --git a/lib/query/hubic/query.ex b/lib/query/hubic/query.ex
index a2ada36..5c4df5f 100644
--- a/lib/query/hubic/query.ex
+++ b/lib/query/hubic/query.ex
@@ -12,6 +12,7 @@ defmodule ExOvh.Query.Hubic do
       ```
   """
 
+
   #########################
   # General Hubic Requests
   #########################
@@ -36,20 +37,21 @@ defmodule ExOvh.Query.Hubic do
   @spec account_usage() :: ExOvh.Client.raw_query_t
   def account_usage(), do: {:get, "/account/usage", :nil}
 
-  ########################################
+
+
+  ########################
   # Link related Requests
-  ########################################
+  ########################
 
 
 
   @doc """
-  GET /account/links, Get all links as a list of links
+  GET /account/links, Get all links as a list of links (showing the object internal uri - not the indirectUri)
 
     ### Example:
       ```elixir
       import ExOvh.Query.Hubic
-      {:ok, resp} = ExOvh.hubic_request(get_links())
-      links = resp.body
+      ExOvh.hubic_request(get_links())
       ```
   """
   @spec get_links() :: ExOvh.Client.raw_query_t
@@ -62,15 +64,32 @@ defmodule ExOvh.Query.Hubic do
     ### Example:
       ```elixir
       import ExOvh.Query.Hubic
-      ExOvh.hubic_request(get_published_links_detailed())
+      ExOvh.hubic_request(get_links_detailed())
       ```
   """
-  @spec get_published_links_detailed() :: ExOvh.Client.raw_query_t
-  def get_published_links_detailed(), do: {:get, "/account/getAllLinks", :nil}
+  @spec get_links_detailed() :: ExOvh.Client.raw_query_t
+  def get_links_detailed(), do: {:get, "/account/getAllLinks", :nil}
+
+
+  @doc """
+  GET /account/links/{uri}, Get detailed information for an object at a given uri
+
+
+  ### Example:
+      ```elixir
+      import ExOvh.Query.Hubic
+      container = "new_container"
+      folder = "/"
+      object = "server_file.txt"
+      uri = folder <> object
+      ExOvh.hubic_request(get_link(uri))
+  """
+  @spec get_link(uri :: String.t) :: ExOvh.Client.raw_query_t
+  def get_link(uri), do: {:get, "/account/links/", uri}
 
 
   @doc ~s"""
-  POST /account/links, Create a public link to a file
+  POST /account/links, Create a public url to a file
 
   Note: links have a max ttl of 30 days on hubic currently.
   ttl can be 1,5,10,15,20,25 or 30
@@ -81,29 +100,58 @@ defmodule ExOvh.Query.Hubic do
       import ExOvh.Query.Hubic
       container = "new_container"
       object = "server_file.txt"
-      ExOvh.hubic_request(publish_object(container, object))
+      {:ok, resp} = ExOvh.hubic_request(publish_object(container, object))
+      %{ "indirectUrl" => link_indirect_uri, "expirationDate" => exp,
+         "creationDate" => created_on, "uri" => uri } = resp.body
+      object_attrs = %{
+                      link: link_indirect_uri,
+                      expiry: exp,
+                      created: created_on,
+                      object: object,
+                      folder: String.replace(uri, object, ""),
+                      object_uri: uri
+                     }
       ```
   """
-  @spec publish_object(container :: String.t, object :: String.t)
+  @spec publish_object(container :: String.t, object :: String.t, opts :: map)
                              :: ExOvh.Client.raw_query_t
-  def publish_object(container, object) do
+  def publish_object(container, object, folder \\ "/", ttl \\ "5", file \\ "file") do
     params = %{
-               "comment": "none",
-               "container": container,
-               "mode": "ro",
-               "ttl": "5",
-               "type": "file",
-               "uri": URI.encode_www_form("/" <> object),
+               "comment" => "none",
+               "container" => container,
+               "mode" => "ro",
+               "ttl" => ttl,
+               "type" => file,
+               "uri" => folder <> object
               }
     {:post, "/account/links", params}
   end
 
 
 
+  @doc ~s"""
+  DELETE /account/links/{uri}, Deletes a public url to a file
+
+
+  ### Example:
+      ```elixir
+      import ExOvh.Query.Hubic
+      container = "new_container"
+      object = "server_file.txt"
+      folder = "/"
+      uri = folder <> object
+      ExOvh.hubic_request(delete_link(uri))
+  """
+  @spec delete_link(uri :: String.t) :: ExOvh.Client.raw_query_t
+  def delete_link(uri), do: {:delete, "/account/links/", uri}
+
+
 
   ########################################
-  # HUBIC API FOLDER RELATED REQUESTS
+  # Folder related requests
   ########################################
 
 
+
+
 end
\ No newline at end of file