1
0
mirror of https://github.com/avitex/elixir-vultr synced 2024-11-23 03:49:57 +00:00

Remove tesla dependency

This commit is contained in:
avitex 2017-10-13 21:18:43 +11:00
parent 621359349a
commit a1f08f09f0
4 changed files with 173 additions and 165 deletions

View File

@ -1,26 +1,12 @@
defmodule Vultr do defmodule Vultr do
use Tesla, docs: false use Vultr.Request, [
use Vultr.Request base_url: "https://api.vultr.com/",
]
plug Tesla.Middleware.BaseUrl, "https://api.vultr.com/v1"
plug Tesla.Middleware.FormUrlencoded
plug Tesla.Middleware.DecodeJson
adapter :ibrowse
@doc """
Create a Vultr client with an API-Key for authenticated methods
"""
def client(api_key) do
Tesla.build_client [
{Tesla.Middleware.Headers, %{ "API-Key" => api_key }}
]
end
################################################## ##################################################
# Account # Account
request :get, "account/info", [ request :get, "v1", "account/info", [
desc: """ desc: """
Retrieve information about the current account. Retrieve information about the current account.
""", """,
@ -31,7 +17,7 @@ defmodule Vultr do
################################################## ##################################################
# Application # Application
request :get, "app/list", [ request :get, "v1", "app/list", [
desc: """ desc: """
Retrieve a list of available applications. Retrieve a list of available applications.
@ -42,7 +28,7 @@ defmodule Vultr do
################################################## ##################################################
# API Key # API Key
request :get, "auth/info", [ request :get, "v1", "auth/info", [
desc: """ desc: """
Retrieve information about the current API key. Retrieve information about the current API key.
""", """,
@ -52,7 +38,7 @@ defmodule Vultr do
################################################## ##################################################
# Backup # Backup
request :get, "backup/list", [ request :get, "v1", "backup/list", [
desc: """ desc: """
List all backups on the current account. List all backups on the current account.
""", """,
@ -73,7 +59,7 @@ defmodule Vultr do
################################################## ##################################################
# Block Storage # Block Storage
request :post, "block/attach", [ request :post, "v1", "block/attach", [
desc: """ desc: """
Attach a block storage subscription to a VPS subscription. Attach a block storage subscription to a VPS subscription.
@ -97,7 +83,7 @@ defmodule Vultr do
}, },
] ]
request :post, "block/create", [ request :post, "v1", "block/create", [
desc: """ desc: """
Create a block storage subscription. Create a block storage subscription.
""", """,
@ -127,7 +113,7 @@ defmodule Vultr do
}, },
] ]
request :post, "block/delete", [ request :post, "v1", "block/delete", [
desc: """ desc: """
Delete a block storage subscription. Delete a block storage subscription.
@ -146,7 +132,7 @@ defmodule Vultr do
}, },
] ]
request :post, "block/detach", [ request :post, "v1", "block/detach", [
desc: """ desc: """
Detach a block storage subscription from the currently attached instance. Detach a block storage subscription from the currently attached instance.
""", """,
@ -162,7 +148,7 @@ defmodule Vultr do
}, },
] ]
request :post, "block/label_set", [ request :post, "v1", "block/label_set", [
desc: """ desc: """
Set the label of a block storage subscription. Set the label of a block storage subscription.
""", """,
@ -184,7 +170,7 @@ defmodule Vultr do
}, },
] ]
request :get, "block/list", [ request :get, "v1", "block/list", [
desc: """ desc: """
Retrieve a list of any active block storage subscriptions on this account. Retrieve a list of any active block storage subscriptions on this account.
""", """,
@ -202,7 +188,7 @@ defmodule Vultr do
}, },
] ]
request :post, "block/resize", [ request :post, "v1", "block/resize", [
desc: """ desc: """
Resize the block storage volume to a new size. Resize the block storage volume to a new size.
@ -229,7 +215,7 @@ defmodule Vultr do
################################################## ##################################################
# DNS # DNS
request :post, "dns/create_domain", [ request :post, "v1", "dns/create_domain", [
desc: """ desc: """
Create a domain name in DNS. Create a domain name in DNS.
""", """,
@ -251,7 +237,7 @@ defmodule Vultr do
}, },
] ]
request :post, "dns/create_record", [ request :post, "v1", "dns/create_record", [
desc: """ desc: """
Add a DNS record. Add a DNS record.
""", """,
@ -298,7 +284,7 @@ defmodule Vultr do
}, },
] ]
request :post, "dns/delete_domain", [ request :post, "v1", "dns/delete_domain", [
desc: """ desc: """
Delete a domain name and all associated records. Delete a domain name and all associated records.
""", """,
@ -314,7 +300,7 @@ defmodule Vultr do
}, },
] ]
request :post, "dns/delete_record", [ request :post, "v1", "dns/delete_record", [
desc: """ desc: """
Delete an individual DNS record. Delete an individual DNS record.
""", """,
@ -337,7 +323,7 @@ defmodule Vultr do
}, },
] ]
request :get, "dns/list", [ request :get, "v1", "dns/list", [
desc: """ desc: """
List all domains associated with the current account. List all domains associated with the current account.
""", """,
@ -345,7 +331,7 @@ defmodule Vultr do
required_access: :dns, required_access: :dns,
] ]
request :get, "dns/records", [ request :get, "v1", "dns/records", [
desc: """ desc: """
List all the records associated with a particular domain. List all the records associated with a particular domain.
""", """,
@ -361,7 +347,7 @@ defmodule Vultr do
}, },
] ]
request :post, "dns/update_record", [ request :post, "v1", "dns/update_record", [
desc: """ desc: """
Update a DNS record. Update a DNS record.
""", """,
@ -415,7 +401,7 @@ defmodule Vultr do
################################################## ##################################################
# ISO Image # ISO Image
request :get, "iso/list", [ request :get, "v1", "iso/list", [
desc: """ desc: """
List all ISOs currently available on this account. List all ISOs currently available on this account.
""", """,
@ -426,7 +412,7 @@ defmodule Vultr do
################################################## ##################################################
# Operating System # Operating System
request :get, "os/list", [ request :get, "v1", "os/list", [
desc: """ desc: """
Retrieve a list of available operating systems. Retrieve a list of available operating systems.
@ -437,7 +423,7 @@ defmodule Vultr do
################################################## ##################################################
# Plans # Plans
request :get, "plans/list", [ request :get, "v1", "plans/list", [
desc: """ desc: """
Retrieve a list of all active plans. Plans that are no longer available will not be shown. Retrieve a list of all active plans. Plans that are no longer available will not be shown.
@ -460,7 +446,7 @@ defmodule Vultr do
}, },
] ]
request :get, "plans/list_vc2", [ request :get, "v1", "plans/list_vc2", [
desc: """ desc: """
Retrieve a list of all active vc2 plans. Plans that are no longer available will not be shown. Retrieve a list of all active vc2 plans. Plans that are no longer available will not be shown.
@ -470,7 +456,7 @@ defmodule Vultr do
""", """,
] ]
request :get, "plans/list_vdc2", [ request :get, "v1", "plans/list_vdc2", [
desc: """ desc: """
Retrieve a list of all active vdc2 plans. Plans that are no longer available will not be shown. Retrieve a list of all active vdc2 plans. Plans that are no longer available will not be shown.
@ -483,7 +469,7 @@ defmodule Vultr do
################################################## ##################################################
# Regions # Regions
request :get, "regions/availability", [ request :get, "v1", "regions/availability", [
desc: """ desc: """
Retrieve a list of the `VPSPLANID`s currently available in this location. Retrieve a list of the `VPSPLANID`s currently available in this location.
@ -501,7 +487,7 @@ defmodule Vultr do
}, },
] ]
request :get, "regions/list", [ request :get, "v1", "regions/list", [
desc: """ desc: """
Retrieve a list of all active regions. Retrieve a list of all active regions.
@ -512,7 +498,7 @@ defmodule Vultr do
################################################## ##################################################
# Reserved IP # Reserved IP
request :post, "reservedip/attach", [ request :post, "v1", "reservedip/attach", [
desc: """ desc: """
Attach a reserved IP to an existing subscription. Attach a reserved IP to an existing subscription.
""", """,
@ -534,7 +520,7 @@ defmodule Vultr do
}, },
] ]
request :post, "reservedip/convert", [ request :post, "v1", "reservedip/convert", [
desc: """ desc: """
Convert an existing IP on a subscription to a reserved IP. Convert an existing IP on a subscription to a reserved IP.
@ -565,7 +551,7 @@ defmodule Vultr do
}, },
] ]
request :post, "reservedip/create", [ request :post, "v1", "reservedip/create", [
desc: """ desc: """
Create a new reserved IP. Create a new reserved IP.
@ -598,7 +584,7 @@ defmodule Vultr do
}, },
] ]
request :post, "reservedip/destroy", [ request :post, "v1", "reservedip/destroy", [
desc: """ desc: """
Remove a reserved IP from your account. Remove a reserved IP from your account.
@ -616,7 +602,7 @@ defmodule Vultr do
}, },
] ]
request :post, "reservedip/detach", [ request :post, "v1", "reservedip/detach", [
desc: """ desc: """
Detach a reserved IP from an existing subscription. Detach a reserved IP from an existing subscription.
""", """,
@ -638,7 +624,7 @@ defmodule Vultr do
}, },
] ]
request :get, "reservedip/list", [ request :get, "v1", "reservedip/list", [
desc: """ desc: """
List all the active reserved IPs on this account. List all the active reserved IPs on this account.
@ -652,7 +638,7 @@ defmodule Vultr do
################################################## ##################################################
# Server # Server
request :post, "server/app_change", [ request :post, "v1", "server/app_change", [
desc: """ desc: """
Changes the virtual machine to a different application. Changes the virtual machine to a different application.
@ -678,7 +664,7 @@ defmodule Vultr do
}, },
] ]
request :get, "server/app_change_list", [ request :get, "v1", "server/app_change_list", [
desc: """ desc: """
Retrieves a list of applications to which a virtual machine can be changed. Retrieves a list of applications to which a virtual machine can be changed.
@ -697,7 +683,7 @@ defmodule Vultr do
}, },
] ]
request :post, "server/backup_disable", [ request :post, "v1", "server/backup_disable", [
desc: """ desc: """
Disables automatic backups on a server. Disables automatic backups on a server.
@ -716,7 +702,7 @@ defmodule Vultr do
}, },
] ]
request :post, "server/backup_enable", [ request :post, "v1", "server/backup_enable", [
desc: """ desc: """
Enables automatic backups on a server. Enables automatic backups on a server.
""", """,
@ -733,7 +719,7 @@ defmodule Vultr do
}, },
] ]
request :post, "server/backup_get_schedule", [ request :post, "v1", "server/backup_get_schedule", [
desc: """ desc: """
Retrieves the backup schedule for a server. Retrieves the backup schedule for a server.
@ -752,7 +738,7 @@ defmodule Vultr do
}, },
] ]
request :post, "server/backup_set_schedule", [ request :post, "v1", "server/backup_set_schedule", [
desc: """ desc: """
Sets the backup schedule for a server. Sets the backup schedule for a server.
@ -802,7 +788,7 @@ defmodule Vultr do
}, },
] ]
request :get, "server/bandwidth", [ request :get, "v1", "server/bandwidth", [
desc: """ desc: """
Get the bandwidth used by a virtual machine. Get the bandwidth used by a virtual machine.
""", """,
@ -819,7 +805,7 @@ defmodule Vultr do
}, },
] ]
request :post, "server/create", [ request :post, "v1", "server/create", [
desc: """ desc: """
Create a new virtual machine. Create a new virtual machine.
@ -973,7 +959,7 @@ defmodule Vultr do
}, },
] ]
request :post, "server/create_ipv4", [ request :post, "v1", "server/create_ipv4", [
desc: """ desc: """
Add a new IPv4 address to a server. Add a new IPv4 address to a server.
@ -1002,7 +988,7 @@ defmodule Vultr do
}, },
] ]
request :post, "server/destroy", [ request :post, "v1", "server/destroy", [
desc: """ desc: """
Destroy (delete) a virtual machine. Destroy (delete) a virtual machine.
@ -1022,7 +1008,7 @@ defmodule Vultr do
}, },
] ]
request :post, "server/destroy_ipv4", [ request :post, "v1", "server/destroy_ipv4", [
desc: """ desc: """
Removes a secondary IPv4 address from a server. Removes a secondary IPv4 address from a server.
@ -1042,7 +1028,7 @@ defmodule Vultr do
}, },
] ]
request :get, "server/get_app_info", [ request :get, "v1", "server/get_app_info", [
desc: """ desc: """
Retrieves the application information for this subscription. Retrieves the application information for this subscription.
""", """,
@ -1059,7 +1045,7 @@ defmodule Vultr do
}, },
] ]
request :get, "server/get_user_data", [ request :get, "v1", "server/get_user_data", [
desc: """ desc: """
Retrieves the (base64 encoded) user-data for this subscription. Retrieves the (base64 encoded) user-data for this subscription.
""", """,
@ -1076,7 +1062,7 @@ defmodule Vultr do
}, },
] ]
request :post, "server/halt", [ request :post, "v1", "server/halt", [
desc: """ desc: """
Halt a virtual machine. This is a hard power off (basically, unplugging the machine). Halt a virtual machine. This is a hard power off (basically, unplugging the machine).
@ -1096,7 +1082,7 @@ defmodule Vultr do
}, },
] ]
request :post, "server/iso_attach", [ request :post, "v1", "server/iso_attach", [
desc: """ desc: """
Attach an ISO and reboot the server. Attach an ISO and reboot the server.
""", """,
@ -1120,7 +1106,7 @@ defmodule Vultr do
}, },
] ]
request :post, "server/iso_detach", [ request :post, "v1", "server/iso_detach", [
desc: """ desc: """
Detach the currently mounted ISO and reboot the server. Detach the currently mounted ISO and reboot the server.
""", """,
@ -1137,7 +1123,7 @@ defmodule Vultr do
}, },
] ]
request :get, "server/iso_status", [ request :get, "v1", "server/iso_status", [
desc: """ desc: """
Retrieve the current ISO state for a given subscription. Retrieve the current ISO state for a given subscription.
@ -1157,7 +1143,7 @@ defmodule Vultr do
}, },
] ]
request :post, "server/label_set", [ request :post, "v1", "server/label_set", [
desc: """ desc: """
Set the label of a virtual machine. Set the label of a virtual machine.
""", """,
@ -1180,7 +1166,7 @@ defmodule Vultr do
}, },
] ]
request :get, "server/list", [ request :get, "v1", "server/list", [
desc: """ desc: """
List all active or pending virtual machines on the current account. List all active or pending virtual machines on the current account.
@ -1232,7 +1218,7 @@ defmodule Vultr do
}, },
] ]
request :get, "server/list_ipv4", [ request :get, "v1", "server/list_ipv4", [
desc: """ desc: """
List the IPv4 information of a virtual machine. List the IPv4 information of a virtual machine.
@ -1242,7 +1228,7 @@ defmodule Vultr do
required_access: :subscriptions, required_access: :subscriptions,
] ]
request :get, "server/list_ipv6", [ request :get, "v1", "server/list_ipv6", [
desc: """ desc: """
List the IPv6 information of a virtual machine. List the IPv6 information of a virtual machine.
@ -1253,7 +1239,7 @@ defmodule Vultr do
required_access: :subscriptions, required_access: :subscriptions,
] ]
request :get, "server/neighbors", [ request :get, "v1", "server/neighbors", [
desc: """ desc: """
Determine what other subscriptions are hosted on the same physical host as a given subscription. Determine what other subscriptions are hosted on the same physical host as a given subscription.
""", """,
@ -1270,7 +1256,7 @@ defmodule Vultr do
}, },
] ]
request :post, "server/os_change", [ request :post, "v1", "server/os_change", [
desc: """ desc: """
Changes the virtual machine to a different operating system. Changes the virtual machine to a different operating system.
@ -1296,7 +1282,7 @@ defmodule Vultr do
}, },
] ]
request :get, "server/os_change_list", [ request :get, "v1", "server/os_change_list", [
desc: """ desc: """
Retrieves a list of operating systems to which a virtual machine can be changed. Retrieves a list of operating systems to which a virtual machine can be changed.
@ -1316,7 +1302,7 @@ defmodule Vultr do
}, },
] ]
request :post, "server/reboot", [ request :post, "v1", "server/reboot", [
desc: """ desc: """
Reboot a virtual machine. Reboot a virtual machine.
@ -1335,7 +1321,7 @@ defmodule Vultr do
}, },
] ]
request :post, "server/reinstall", [ request :post, "v1", "server/reinstall", [
desc: """ desc: """
Reinstall the operating system on a virtual machine. Reinstall the operating system on a virtual machine.
@ -1362,7 +1348,7 @@ defmodule Vultr do
}, },
] ]
request :post, "server/restore_backup", [ request :post, "v1", "server/restore_backup", [
desc: """ desc: """
Restore the specified backup to the virtual machine. Restore the specified backup to the virtual machine.
@ -1388,7 +1374,7 @@ defmodule Vultr do
}, },
] ]
request :post, "server/restore_snapshot", [ request :post, "v1", "server/restore_snapshot", [
desc: """ desc: """
Restore the specified snapshot to the virtual machine. Restore the specified snapshot to the virtual machine.
@ -1414,7 +1400,7 @@ defmodule Vultr do
}, },
] ]
request :post, "server/reverse_default_ipv4", [ request :post, "v1", "server/reverse_default_ipv4", [
desc: """ desc: """
Set a reverse DNS entry for an IPv4 address of a virtual machine to the original setting. Set a reverse DNS entry for an IPv4 address of a virtual machine to the original setting.
@ -1440,7 +1426,7 @@ defmodule Vultr do
}, },
] ]
request :post, "server/reverse_delete_ipv6", [ request :post, "v1", "server/reverse_delete_ipv6", [
desc: """ desc: """
Remove a reverse DNS entry for an IPv6 address of a virtual machine. Remove a reverse DNS entry for an IPv6 address of a virtual machine.
@ -1466,7 +1452,7 @@ defmodule Vultr do
}, },
] ]
request :get, "server/reverse_list_ipv6", [ request :get, "v1", "server/reverse_list_ipv6", [
desc: """ desc: """
List the IPv6 reverse DNS entries of a virtual machine. List the IPv6 reverse DNS entries of a virtual machine.
@ -1486,7 +1472,7 @@ defmodule Vultr do
}, },
] ]
request :post, "server/reverse_set_ipv4", [ request :post, "v1", "server/reverse_set_ipv4", [
desc: """ desc: """
Set a reverse DNS entry for an IPv4 address of a virtual machine. Set a reverse DNS entry for an IPv4 address of a virtual machine.
@ -1518,7 +1504,7 @@ defmodule Vultr do
}, },
] ]
request :post, "server/reverse_set_ipv6", [ request :post, "v1", "server/reverse_set_ipv6", [
desc: """ desc: """
Set a reverse DNS entry for an IPv6 address of a virtual machine. Set a reverse DNS entry for an IPv6 address of a virtual machine.
@ -1550,7 +1536,7 @@ defmodule Vultr do
}, },
] ]
request :post, "server/set_user_data", [ request :post, "v1", "server/set_user_data", [
desc: """ desc: """
Sets the cloud-init user-data for this subscription. Sets the cloud-init user-data for this subscription.
@ -1575,7 +1561,7 @@ defmodule Vultr do
}, },
] ]
request :post, "server/start", [ request :post, "v1", "server/start", [
desc: """ desc: """
Start a virtual machine. Start a virtual machine.
@ -1594,7 +1580,7 @@ defmodule Vultr do
}, },
] ]
request :post, "server/upgrade_plan", [ request :post, "v1", "server/upgrade_plan", [
desc: """ desc: """
Upgrade the plan of a virtual machine. Upgrade the plan of a virtual machine.
@ -1620,7 +1606,7 @@ defmodule Vultr do
}, },
] ]
request :post, "server/upgrade_plan_list", [ request :post, "v1", "server/upgrade_plan_list", [
desc: """ desc: """
Retrieve a list of the VPSPLANIDs for which a virtual machine can be upgraded. Retrieve a list of the VPSPLANIDs for which a virtual machine can be upgraded.
@ -1642,7 +1628,7 @@ defmodule Vultr do
################################################## ##################################################
# Snapshot # Snapshot
request :post, "snapshot/create", [ request :post, "v1", "snapshot/create", [
desc: """ desc: """
Create a snapshot from an existing virtual machine. Create a snapshot from an existing virtual machine.
@ -1668,7 +1654,7 @@ defmodule Vultr do
}, },
] ]
request :post, "snapshot/destroy", [ request :post, "v1", "snapshot/destroy", [
desc: """ desc: """
Destroy (delete) a snapshot. Destroy (delete) a snapshot.
@ -1687,7 +1673,7 @@ defmodule Vultr do
}, },
] ]
request :get, "snapshot/list", [ request :get, "v1", "snapshot/list", [
desc: """ desc: """
List all snapshots on the current account. List all snapshots on the current account.
""", """,
@ -1698,7 +1684,7 @@ defmodule Vultr do
################################################## ##################################################
# SSH Key # SSH Key
request :post, "sshkey/create", [ request :post, "v1", "sshkey/create", [
desc: """ desc: """
Create a new SSH Key. Create a new SSH Key.
""", """,
@ -1720,7 +1706,7 @@ defmodule Vultr do
}, },
] ]
request :post, "sshkey/destroy", [ request :post, "v1", "sshkey/destroy", [
desc: """ desc: """
Remove a SSH key. Remove a SSH key.
@ -1739,7 +1725,7 @@ defmodule Vultr do
}, },
] ]
request :get, "sshkey/list", [ request :get, "v1", "sshkey/list", [
desc: """ desc: """
List all the SSH keys on the current account. List all the SSH keys on the current account.
""", """,
@ -1747,7 +1733,7 @@ defmodule Vultr do
required_access: :subscriptions, required_access: :subscriptions,
] ]
request :post, "sshkey/update", [ request :post, "v1", "sshkey/update", [
desc: """ desc: """
Update an existing SSH Key. Update an existing SSH Key.
@ -1784,7 +1770,7 @@ defmodule Vultr do
################################################## ##################################################
# Startup Script # Startup Script
request :post, "startupscript/create", [ request :post, "v1", "startupscript/create", [
desc: """ desc: """
Create a startup script. Create a startup script.
""", """,
@ -1814,7 +1800,7 @@ defmodule Vultr do
}, },
] ]
request :post, "startupscript/destroy", [ request :post, "v1", "startupscript/destroy", [
desc: """ desc: """
Remove a startup script. Remove a startup script.
""", """,
@ -1830,7 +1816,7 @@ defmodule Vultr do
}, },
] ]
request :get, "startupscript/list", [ request :get, "v1", "startupscript/list", [
desc: """ desc: """
List all startup scripts on the current account. List all startup scripts on the current account.
@ -1841,7 +1827,7 @@ defmodule Vultr do
required_access: :subscriptions, required_access: :subscriptions,
] ]
request :post, "startupscript/update", [ request :post, "v1", "startupscript/update", [
desc: """ desc: """
Update an existing startup script. Update an existing startup script.
""", """,
@ -1875,7 +1861,7 @@ defmodule Vultr do
################################################## ##################################################
# User Management # User Management
request :post, "user/create", [ request :post, "v1", "user/create", [
desc: """ desc: """
Create a new user. Create a new user.
""", """,
@ -1916,7 +1902,7 @@ defmodule Vultr do
}, },
] ]
request :post, "user/delete", [ request :post, "v1", "user/delete", [
desc: """ desc: """
Delete a user. Delete a user.
""", """,
@ -1932,7 +1918,7 @@ defmodule Vultr do
}, },
] ]
request :get, "user/list", [ request :get, "v1", "user/list", [
desc: """ desc: """
Retrieve a list of any users associated with this account. Retrieve a list of any users associated with this account.
@ -1951,7 +1937,7 @@ defmodule Vultr do
required_access: :manage_users, required_access: :manage_users,
] ]
request :post, "user/update", [ request :post, "v1", "user/update", [
desc: """ desc: """
Update the details for a user. Update the details for a user.
""", """,

View File

@ -6,22 +6,32 @@ defmodule Vultr.Request do
:isoid, :scriptid, :snapshotid, :sshkeyid, :backupid, :userid, :isoid, :scriptid, :snapshotid, :sshkeyid, :backupid, :userid,
] ]
defmacro __using__(_) do defmacro __using__(options) do
module = __CALLER__.module
Module.register_attribute(module, :__request_opts__, persist: true)
Module.put_attribute(module, :__request_opts__, options)
quote do quote do
import Vultr.Request, only: [request: 3] import Vultr.Request, only: [request: 4]
end end
end end
defmacro request(endpoint_method, endpoint_path, endpoint_opts) do defmacro request(endpoint_method, endpoint_version, endpoint_path, endpoint_opts) do
request_opts = Module.get_attribute(__CALLER__.module, :__request_opts__)
base_url = Keyword.fetch!(request_opts, :base_url)
endpoint_params = Keyword.get(endpoint_opts, :params, {nil, nil, []}) |> normalize_params endpoint_params = Keyword.get(endpoint_opts, :params, {nil, nil, []}) |> normalize_params
endpoint_versioned_path = Path.join([endpoint_version, endpoint_path])
endpoint_url = URI.merge(base_url, endpoint_versioned_path) |> to_string
endpoint_description = Keyword.fetch!(endpoint_opts, :desc) endpoint_description = Keyword.fetch!(endpoint_opts, :desc)
endpoint_requires_api_key = Keyword.get(endpoint_opts, :api_key, nil) endpoint_requires_api_key = Keyword.get(endpoint_opts, :api_key, nil)
endpoint_required_access = Keyword.get(endpoint_opts, :required_access, nil) endpoint_required_access = Keyword.get(endpoint_opts, :required_access, nil)
endpoint_has_params = (length(endpoint_params) > 0) endpoint_has_params = (length(endpoint_params) > 0)
common_args = [__CALLER__.module, endpoint_method, endpoint_path] common_args = [endpoint_method, endpoint_url]
func = &__MODULE__.perform_request/5 func = &__MODULE__.perform_request/4
func_name = func_name =
endpoint_path endpoint_path
@ -29,7 +39,7 @@ defmodule Vultr.Request do
|> String.to_atom |> String.to_atom
func_doc = gen_doc( func_doc = gen_doc(
endpoint_method, endpoint_path, endpoint_method, endpoint_versioned_path,
endpoint_description, endpoint_params, endpoint_description, endpoint_params,
endpoint_required_access, endpoint_requires_api_key endpoint_required_access, endpoint_requires_api_key
) )
@ -38,14 +48,14 @@ defmodule Vultr.Request do
cond do cond do
endpoint_requires_api_key && endpoint_has_params -> endpoint_requires_api_key && endpoint_has_params ->
quote do quote do
def unquote(func_name)(client, params \\ []) when is_list(params) do def unquote(func_name)(api_key, params \\ []) when is_list(params) do
unquote(func).(unquote_splicing(common_args), client, params) unquote(func).(unquote_splicing(common_args), api_key, params)
end end
end end
endpoint_requires_api_key -> endpoint_requires_api_key ->
quote do quote do
def unquote(func_name)(client) do def unquote(func_name)(api_key) do
unquote(func).(unquote_splicing(common_args), client, []) unquote(func).(unquote_splicing(common_args), api_key, [])
end end
end end
endpoint_has_params -> endpoint_has_params ->
@ -68,24 +78,43 @@ defmodule Vultr.Request do
end end
end end
def perform_request(caller, method, url, client, params) do def perform_request(method, url, api_key, params) do
opts = [url: url, method: method] ++ prepare_params(method, params) headers = prepare_api_key_header(api_key)
resp = Tesla.perform_request(caller, client, opts) opts = [headers: headers] ++ prepare_params(method, params)
case resp.status do resp = HTTPotion.request(method, url, opts)
200 -> {:ok, resp.body} parsed_body = parse_body(resp)
400 -> {:error, :invalid_api_location, resp.body}
403 -> {:error, :invalid_api_key, resp.body} case resp.status_code do
405 -> {:error, :invalid_http_method, resp.body} 200 -> {:ok, parsed_body}
412 -> {:error, :bad_request, resp.body} 400 -> {:error, :invalid_api_location, parsed_body}
500 -> {:error, :server_error, resp.body} 403 -> {:error, :invalid_api_key, parsed_body}
503 -> {:error, :rate_limit, resp.body} 405 -> {:error, :invalid_http_method, parsed_body}
412 -> {:error, :bad_request, parsed_body}
500 -> {:error, :server_error, parsed_body}
503 -> {:error, :rate_limit, parsed_body}
end end
end end
defp prepare_params(:get, nil), do: [query: []] defp parse_body(%HTTPotion.Response{ body: body, headers: headers }) do
defp prepare_params(_, nil), do: [body: %{}] case HTTPotion.Headers.fetch(headers, "content-type") do
defp prepare_params(:get, params), do: [query: capitalize_special_params(params)] {:ok, "application/json"} ->
defp prepare_params(_, params), do: [body: Enum.into(capitalize_special_params(params), %{})] Poison.decode!(body)
_ ->
body
end
end
defp prepare_api_key_header(nil), do: []
defp prepare_api_key_header(api_key), do: ["API-Key": api_key]
defp prepare_params(:get, params), do: [query: prepare_query(params)]
defp prepare_params(_, params), do: [body: prepare_body(params)]
defp prepare_body(nil), do: ""
defp prepare_body(params), do: capitalize_special_params(params) |> Enum.into(%{}) |> Poison.encode!
defp prepare_query(nil), do: false
defp prepare_query(params), do: capitalize_special_params(params)
defp capitalize_special_params(params) do defp capitalize_special_params(params) do
Enum.map(params, fn {k, v} -> Enum.map(params, fn {k, v} ->
@ -122,36 +151,42 @@ defmodule Vultr.Request do
# Documentation helpers # Documentation helpers
defp gen_doc(method, path, desc, params, required_access, api_key) do defp gen_doc(method, path, desc, params, required_access, api_key) do
doc = """
#{desc}
""" """
#{desc}
doc = #{doc_params(params)}
if length(params) > 0 do
doc <> ("""
### Params
| Name | Type | Optional | Description |
| ---- | ---- | -------- | ----------- |
#{doc_params(params)}
""")
else
doc
end
doc <> """
### Backend ### Backend
- Method: `#{doc_method(method)}` - Method: `#{doc_method(method)}`
- Path: `/v1/#{path}` - Path: `#{path}`
- API Key: `#{doc_api_key(api_key)}` - API Key: `#{doc_api_key(api_key)}`
- Required Access: `#{doc_required_access(required_access)}` - Required Access: `#{doc_required_access(required_access)}`
""" """
end end
defp doc_param(param), do: """ defp doc_params([]), do: ""
| `#{String.downcase(param.name)}` | #{param.type_string} | #{doc_optional_default(param.optional, param.default)} | #{param.desc |> String.replace("\n", "<br>")} | defp doc_params(params) do
""" param_rows =
params
|> Enum.map(&doc_param/1)
|> Enum.join("")
defp doc_params(params), do: params |> Enum.map(&doc_param/1) |> Enum.join("") """
### Params
| Name | Type | Optional | Description |
| ---- | ---- | -------- | ----------- |
#{param_rows}
"""
end
defp doc_param(param) do
columns = [
"`#{String.downcase(param.name)}`",
param.type_string,
doc_optional_default(param.optional, param.default),
String.replace(param.desc, "\n", "<br>"),
]
"| #{Enum.join(columns, " | ")} |"
end
defp doc_optional_default(optional, default) do defp doc_optional_default(optional, default) do
if optional do if optional do
@ -167,13 +202,7 @@ defmodule Vultr.Request do
end end
end end
defp doc_method(method) do defp doc_method(method), do: method |> Atom.to_string |> String.upcase
if !Enum.any?([:get, :post], fn supported -> method == supported end) do
raise ArgumentError, message: "Bad method"
end
method |> Atom.to_string |> String.upcase
end
defp doc_api_key(nil), do: "No" defp doc_api_key(nil), do: "No"
defp doc_api_key(atm), do: atom_to_word(atm) defp doc_api_key(atm), do: atom_to_word(atm)

View File

@ -16,14 +16,13 @@ defmodule Vultr.Mixfile do
# Configuration for the OTP application # Configuration for the OTP application
def application, do: [ def application, do: [
applications: [:tesla, :ibrowse], applications: [:httpotion],
] ]
defp deps, do: [ defp deps, do: [
{:inch_ex, "~> 0.5", only: :docs}, {:inch_ex, "~> 0.5", only: :docs},
{:ex_doc, "~> 0.16", only: :dev, runtime: false}, {:ex_doc, "~> 0.16", only: :dev, runtime: false},
{:tesla, "~> 0.7.2"}, {:httpotion, "~> 3.0.2"},
{:ibrowse, "~> 4.2"},
{:poison, "~> 3.1"}, {:poison, "~> 3.1"},
] ]

View File

@ -1,12 +1,6 @@
%{"certifi": {:hex, :certifi, "1.0.0", "1c787a85b1855ba354f0b8920392c19aa1d06b0ee1362f9141279620a5be2039", [:rebar3], []}, %{"earmark": {:hex, :earmark, "1.2.3", "206eb2e2ac1a794aa5256f3982de7a76bf4579ff91cb28d0e17ea2c9491e46a4", [:mix], []},
"earmark": {:hex, :earmark, "1.2.3", "206eb2e2ac1a794aa5256f3982de7a76bf4579ff91cb28d0e17ea2c9491e46a4", [:mix], []},
"ex_doc": {:hex, :ex_doc, "0.16.3", "cd2a4cfe5d26e37502d3ec776702c72efa1adfa24ed9ce723bb565f4c30bd31a", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, optional: false]}]}, "ex_doc": {:hex, :ex_doc, "0.16.3", "cd2a4cfe5d26e37502d3ec776702c72efa1adfa24ed9ce723bb565f4c30bd31a", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, optional: false]}]},
"hackney": {:hex, :hackney, "1.6.6", "5564b4695d48fd87859e9df77a7fa4b4d284d24519f0cd7cc898f09e8fbdc8a3", [:rebar3], [{:certifi, "1.0.0", [hex: :certifi, optional: false]}, {:idna, "4.0.0", [hex: :idna, optional: false]}, {:metrics, "1.0.1", [hex: :metrics, optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, optional: false]}]}, "httpotion": {:hex, :httpotion, "3.0.3", "17096ea1a7c0b2df74509e9c15a82b670d66fc4d66e6ef584189f63a9759428d", [], [{:ibrowse, "~> 4.4", [hex: :ibrowse, repo: "hexpm", optional: false]}], "hexpm"},
"ibrowse": {:hex, :ibrowse, "4.4.0", "2d923325efe0d2cb09b9c6a047b2835a5eda69d8a47ed6ff8bc03628b764e991", [:rebar3], []}, "ibrowse": {:hex, :ibrowse, "4.4.0", "2d923325efe0d2cb09b9c6a047b2835a5eda69d8a47ed6ff8bc03628b764e991", [:rebar3], []},
"idna": {:hex, :idna, "4.0.0", "10aaa9f79d0b12cf0def53038547855b91144f1bfcc0ec73494f38bb7b9c4961", [:rebar3], []},
"inch_ex": {:hex, :inch_ex, "0.5.6", "418357418a553baa6d04eccd1b44171936817db61f4c0840112b420b8e378e67", [:mix], [{:poison, "~> 1.5 or ~> 2.0 or ~> 3.0", [hex: :poison, optional: false]}]}, "inch_ex": {:hex, :inch_ex, "0.5.6", "418357418a553baa6d04eccd1b44171936817db61f4c0840112b420b8e378e67", [:mix], [{:poison, "~> 1.5 or ~> 2.0 or ~> 3.0", [hex: :poison, optional: false]}]},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], []}, "poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], []}}
"mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], []},
"poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], []},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [:make, :rebar], []},
"tesla": {:hex, :tesla, "0.7.2", "29290de334f1064e98c32d0ba5e73674fdbe53c17f8af7bcc686bf5002a30bab", [:mix], [{:exjsx, ">= 0.1.0", [hex: :exjsx, optional: true]}, {:fuse, "~> 2.4", [hex: :fuse, optional: true]}, {:hackney, "~> 1.6", [hex: :hackney, optional: true]}, {:ibrowse, "~> 4.2", [hex: :ibrowse, optional: true]}, {:poison, ">= 1.0.0", [hex: :poison, optional: true]}]}}