Current section
Files
Jump to
Current section
Files
lib/cloudflare_api/managed_transforms.ex
defmodule CloudflareApi.ManagedTransforms do
@moduledoc ~S"""
Manage zone-level Managed Transforms (`/zones/:zone_id/managed_headers`).
"""
use CloudflareApi.Typespecs
@doc ~S"""
List managed transforms.
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.ManagedTransforms.list(client, "zone_id")
{:ok, [%{"id" => "example"}]}
"""
def list(client, zone_id) do
request(client, :get, base(zone_id))
end
@doc ~S"""
Update managed transforms.
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.ManagedTransforms.update(client, "zone_id", %{})
{:ok, %{"id" => "example"}}
"""
def update(client, zone_id, params) when is_map(params) do
request(client, :patch, base(zone_id), params)
end
@doc ~S"""
Delete managed transforms.
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.ManagedTransforms.delete(client, "zone_id")
{:ok, %{"id" => "example"}}
"""
def delete(client, zone_id) do
request(client, :delete, base(zone_id))
end
defp base(zone_id), do: "/zones/#{zone_id}/managed_headers"
defp request(client, method, url, body \\ nil) do
client = c(client)
result =
case {method, body} do
{:get, _} -> Tesla.get(client, url)
{:patch, %{} = params} -> Tesla.patch(client, url, params)
{:delete, _} -> Tesla.delete(client, url)
end
handle_response(result)
end
defp handle_response({:ok, %Tesla.Env{status: 204}}), do: {:ok, :no_content}
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