uploadcare_ex v0.1.4 UploadcareEx

Elixir wrapper for Uploadcare API.

Link to this section Summary

Functions

Deletes a file by its uuid

Acquires file-specific info by its uuid

Stores a file by its uuid

Basic request method with retries

Uploads a file from provided filesystem path. Returns created file uuid

Uploads files from URLs. Returns created file status

Link to this section Types

Link to this type response()
response() :: %{status_code: number(), body: map() | binary()}

Link to this section Functions

Link to this function file_delete(uuid)
file_delete(binary()) :: :ok | {:error, any()}

Deletes a file by its uuid.

Examples:

iex> UploadcareEx.file_delete("a295f184-0328-4b30-be4d-f215d9cdbed7")
:ok

iex> UploadcareEx.file_delete("wrong")
{:error, %{body: %{"detail" => "Not found."}, status_code: 404}}
Link to this function file_info(uuid)
file_info(binary()) :: {:ok, map()} | {:error, any()}

Acquires file-specific info by its uuid.

Examples:

iex> UploadcareEx.file_info("a295f184-0328-4b30-be4d-f215d9cdbed7")
{:ok,
 %{
   "datetime_removed" => nil,
   "datetime_stored" => nil,
   ...
  }
}

iex> UploadcareEx.file_info("wrong")
{:error, %{body: %{"detail" => "Not found."}, status_code: 404}}
Link to this function file_store(uuid)
file_store(binary()) :: {:ok, map()} | {:error, any()}

Stores a file by its uuid.

Examples:

iex> UploadcareEx.file_store("a295f184-0328-4b30-be4d-f215d9cdbed7")
{:ok,
 %{
   "datetime_removed" => nil,
   "datetime_stored" => "2018-04-01T16:17:26.699680Z",
   ...
  }
 }

iex> UploadcareEx.file_store("wrong")
{:error, %{body: %{"detail" => "Not found."}, status_code: 404}}
Link to this function request(url, http_method, data \\ "", headers \\ %{})
request(binary(), atom(), any(), map()) ::
  {:ok, response()} | {:error, any()}

Basic request method with retries.

Retries are configured with retry_period and retry_expiry configuration params in your config.exs. Their default values are 1_000 and 5_000 respectively. All requests that get responses with 5xx status codes are retried. Also all HTTPoison (which is used as a default http client) errors are retried.

Examples:

iex> UploadcareEx.request("https://api.uploadcare.com/files/2e6b7f23-9143-4b71-94e7-338bbf278c01/", :get)
{:ok,
 %{
   body: %{"detail" => "Authentication credentials were not provided."},
   status_code: 401
 }}

iex> UploadcareEx.request("https://upload.uploadcare.com/from_url/status/?token=b8a3eb85-c01d-4809-9ba7-7590e4e7300f", :get)
{:ok,
 %{...}
}
Link to this function upload_file(file_path)
upload_file(binary()) :: {:ok, binary()} | {:error, any()}

Uploads a file from provided filesystem path. Returns created file uuid.

Examples:

iex> UploadcareEx.upload_file("/my/path/image.png")
{:ok, "a295f184-0328-4b30-be4d-f215d9cdbed7"}

iex> UploadcareEx.upload_file("/invalid")
{:error, %HTTPoison.Error{id: nil, reason: :enoent}}
Link to this function upload_url(url)
upload_url(binary()) :: {:ok, map()} | {:error, any()}

Uploads files from URLs. Returns created file status.

Examples:

iex> UploadcareEx.upload_url("https://avatars0.githubusercontent.com/u/6567687?s=460&v=4")
{:ok,
 %{
   "done" => 20344,
   "file_id" => "b82a3688-27fb-431d-aac8-1b50553b8779",
   "filename" => "6567687",
   ...
  }
}

iex> UploadcareEx.upload_url("https://google.com")
{:error,
 %{
   body: %{
     "error" => "Uploading of these files types is not allowed on your current plan.",
     "status" => "error"
   },
   status_code: 200
 }
}