Current section

Files

Jump to
cloudflare_api lib cloudflare_api custom_hostname_fallback_origin.ex
Raw

lib/cloudflare_api/custom_hostname_fallback_origin.ex

defmodule CloudflareApi.CustomHostnameFallbackOrigin do
@moduledoc ~S"""
Manage fallback origin settings for custom hostnames on a zone.
"""
use CloudflareApi.Typespecs
@doc ~S"""
Get custom hostname fallback origin.
Calls the Cloudflare API endpoint described in the moduledoc and
returns `{:ok, result}` on success or `{:error, reason}` when the request fails.
## Examples
iex> client = CloudflareApi.client("api-token")
iex> CloudflareApi.CustomHostnameFallbackOrigin.get(client, "zone_id")
{:ok, %{"id" => "example"}}
"""
def get(client, zone_id) do
c(client)
|> Tesla.get(path(zone_id))
|> handle_response()
end
@doc ~S"""
Update custom hostname fallback origin.
Calls the Cloudflare API endpoint described in the moduledoc and
returns `{:ok, result}` on success or `{:error, reason}` when the request fails.
## Examples
iex> client = CloudflareApi.client("api-token")
iex> CloudflareApi.CustomHostnameFallbackOrigin.update(client, "zone_id", %{})
{:ok, %{"id" => "example"}}
"""
def update(client, zone_id, params) when is_map(params) do
c(client)
|> Tesla.put(path(zone_id), params)
|> handle_response()
end
@doc ~S"""
Delete custom hostname fallback origin.
Calls the Cloudflare API endpoint described in the moduledoc and
returns `{:ok, result}` on success or `{:error, reason}` when the request fails.
## Examples
iex> client = CloudflareApi.client("api-token")
iex> CloudflareApi.CustomHostnameFallbackOrigin.delete(client, "zone_id")
{:ok, %{"id" => "example"}}
"""
def delete(client, zone_id) do
c(client)
|> Tesla.delete(path(zone_id), body: %{})
|> handle_response()
end
defp path(zone_id), do: "/zones/#{zone_id}/custom_hostnames/fallback_origin"
defp handle_response({:ok, %Tesla.Env{status: status, body: %{"result" => result}}})
when status in 200..299,
do: {:ok, result}
defp handle_response({:ok, %Tesla.Env{status: status, body: body}}) when status in 200..299,
do: {:ok, body}
defp handle_response({:ok, %Tesla.Env{body: %{"errors" => errors}}}), do: {:error, errors}
defp handle_response(other), do: {:error, other}
defp c(%Tesla.Client{} = client), do: client
defp c(fun) when is_function(fun, 0), do: fun.()
end