View Source Doex.Api (doex v0.14.1)
Make almost direct calls to the Digital Ocean API.
Assuming your configs are properly setup, then you only need to provide
the endpoint after the /v2
in the DO API, so to access your account
you would run
Doex.Api.get("/account")
OR, you encode the method, and make the same call like
Doex.Api.call(:get, %{source: "/account"})
If your configurations are message up (or other errors occur), it will look similar to
{:error,
"Expected a 200, received 401",
%{"id" => "unauthorized", "message" => "Unable to authenticate you."}}
If things are working as expected, a success message looks like
{:ok,
%{"account" => %{"droplet_limit" => 99, "email" => "me@example.com",
"email_verified" => true, "floating_ip_limit" => 5, "status" => "active",
"status_message" => "",
"uuid" => "abcdefghabcdefghabcdefghabcdefghabcdefgh"}}}
To send a POST command, for example creating a new droplet, you can run
Doex.Api.post(
"/droplets",
%{name: "dplet001",
region: "tor1",
size: "s-1vcpu-1gb",
image: "ubuntu-18-04-x64",
ssh_keys: [12345],
backups: false,
ipv6: true,
user_data: nil,
private_networking: nil,
volumes: nil,
tags: ["dplet001"]})
OR, you encode the method, and make the same call like
Doex.Api.call(
:post,
%{source: "/droplets",
body: %{name: "dplet001",
region: "tor1",
size: "s-1vcpu-1gb",
image: "ubuntu-18-04-x64",
ssh_keys: [12345],
backups: false,
ipv6: true,
user_data: nil,
private_networking: nil,
volumes: nil,
tags: ["dplet001"]}})
You could also provide additional headers within the call, but the defaults of
%{bearer: <your configured token>,
content_type: "application/json"}
Should be sufficient. But if you needed to make the calls directly without any loaded config, then you could. For example,
Doex.Api.get(
"/account",
%{bearer: "ABC123",
content_type: "application/json"})
OR,
Doex.Api.call(
:get,
%{source: "/account",
header: %{bearer: "ABC123",
content_type: "application/json"}})
A similar approach can be used for sending the headers via a POST call.
Link to this section Summary
Functions
Retrieve data from the API using any method (:get, :post, :put, :delete, etc) available
Make an API call using PUT. Optionally provide any required data and headers
Encode the provided hash map for the URL.
Build the headers for your API
Make an API call using GET. Optionally provide any required headers
Make an API call using POST. Optionally provide any required data and headers
Make an API call using PUT. Optionally provide any required data and headers
Provided the relative path of the Digital Ocean API, resolve the full URL
Link to this section Functions
Retrieve data from the API using any method (:get, :post, :put, :delete, etc) available
Make an API call using PUT. Optionally provide any required data and headers
Encode the provided hash map for the URL.
examples
Examples
iex> Doex.Api.encode_body(%{a: "one", b: "two"})
"{\"a\":\"one\",\"b\":\"two\"}"
iex> Doex.Api.encode_body(%{a: "o ne"})
"{\"a\":\"o ne\"}"
iex> Doex.Api.encode_body(nil, %{a: "o ne"})
"{\"a\":\"o ne\"}"
iex> Doex.Api.encode_body("application/x-www-form-urlencoded", %{a: "o ne"})
"a=o+ne"
iex> Doex.Api.encode_body("application/json", %{a: "b"})
"{\"a\":\"b\"}"
Build the headers for your API
examples
Examples
iex> Doex.Api.encode_headers(%{content_type: "application/json", bearer: "abc123"})
[{"Authorization", "Bearer abc123"}, {"Content-Type", "application/json"}]
iex> Doex.Api.encode_headers(%{bearer: "abc123"})
[{"Authorization", "Bearer abc123"}, {"Content-Type", "application/json"}]
iex> Doex.Api.encode_headers(%{})
[{"Authorization", "Bearer FILL_ME_IN"}, {"Content-Type", "application/json"}]
iex> Doex.Api.encode_headers()
[{"Authorization", "Bearer FILL_ME_IN"}, {"Content-Type", "application/json"}]
iex> Doex.Api.encode_headers(nil)
[{"Authorization", "Bearer FILL_ME_IN"}, {"Content-Type", "application/json"}]
Make an API call using GET. Optionally provide any required headers
Make an API call using POST. Optionally provide any required data and headers
Make an API call using PUT. Optionally provide any required data and headers
Provided the relative path of the Digital Ocean API, resolve the full URL
examples
Examples
iex> Doex.Api.resolve_url("/accounts")
"https://api.digitalocean.com/v2/accounts"