1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
defmodule ExOvh.Hubic.RequestHelpers do
import ExOvh.Query.Openstack.Swift
alias ExOvh.Hubic.OpenstackApi.Cache, as: OpenCache
@doc ~S"""
Gets a list of all openstack swift containers for the hubic app
Returns `{:ok, [<container_name>, <container_name> ...]`
or
Returns `{:error, resp}`
## Example
```elixir
alias ExOvh.Hubic.RequestHelpers
client = ExOvh # Enter your client here
RequestHelpers.containers(client)
```
"""
@spec containers(client :: atom)
:: {:ok, [String.t]} | {:error, ExOvh.Client.response_t}
def containers(client) do
account = OpenCache.get_account(client)
case ExOvh.hubic_request(account_info(account), %{ openstack: :true }) do
{:ok, resp} ->
LoggingUtils.log_return(resp)
{:ok, resp.body |> Enum.map(fn(%{"name" => container}) -> container end)}
{:error, resp} ->
{:error, resp}
end
end
@doc ~S"""
Gets a list of all objects by name in an openstack swift container for the hubic app
Allows to filter the returned list by hash or by name depending on the filter used.
Returns `{:ok, [<object_name>, <object_name> ...]`
or
Returns `{:error, resp}`
## Example
```elixir
alias ExOvh.Hubic.RequestHelpers
client = ExOvh
container = "new_container"
RequestHelpers.get_objects(client, container, :name)
RequestHelpers.get_objects(client, container, :hash)
```
"""
@spec get_objects(client :: atom, container :: String.t, filter :: atom)
:: {:ok, [String.t]} | {:error, ExOvh.Client.response_t}
def get_objects(client, container, filter)
def get_objects(client, container, :name) do
account = OpenCache.get_account(client)
case ExOvh.hubic_request(get_objects(account, container), %{ openstack: :true }) do
{:ok, resp} ->
LoggingUtils.log_return(resp)
{:ok, resp.body |> Enum.map(fn(%{"name" => object_name}) -> object_name end)}
{:error, resp} ->
{:error, resp}
end
end
def get_objects(client, container, :hash) do
account = OpenCache.get_account(client)
case ExOvh.hubic_request(get_objects(account, container), %{ openstack: :true }) do
{:ok, resp} ->
LoggingUtils.log_return(resp)
{:ok, resp.body |> Enum.map(fn(%{"hash" => object_hash}) -> object_hash end)}
{:error, resp} ->
{:error, resp}
end
end
end