ExTwilio.Api
Provides a basic HTTP interface to allow easy communication with the Twilio API, by wrapping HTTPotion
.
Examples
Requests are made to the Twilio API by passing in a resource module into one of this Api
module's functions. The correct URL to the resource is inferred from the module name.
ExTwilio.Api.find(Resource, "sid")
%Resource{ sid: "sid", ... }
Items are returned as instances of the given module's struct. For more details, see the documentation for each function.
Summary
Create a new resource in the Twilio API with a POST request
Destroy an existing resource in the Twilio Api
Find a given resource in the Twilio API by its SID
Adds the Account SID and Auth Token to every request through HTTP basic auth
If the request body is a list, then convert the list to a query string. Otherwise, pass it through unmodified
Automatically add the Content-Type application/x-www-form-urlencoded. This allows POST request data to be processed properly. It seems to have no negative effect on GET calls, so it is added to all requests
Sends an HTTP request. Args: * method - HTTP method, atom (:get, :head, :post, :put, :delete, etc.) * url - URL, binary string or char list * options - orddict of options Options: * body - request body, binary string or char list * headers - HTTP headers, orddict (eg. ["Accept": "application/json"]) * timeout - timeout in ms, integer * basic_auth - basic auth credentials (eg. {"user", "password"}) * stream_to - if you want to make an async request, the pid of the process * direct - if you want to use ibrowse's direct feature, the pid of the worker spawned by spawn_worker_process or spawn_link_worker_process Returns HTTPotion.Response or HTTPotion.AsyncResponse if successful. Raises HTTPotion.HTTPError if failed
Deprecated form of request; body and headers are now options, see request/3
Deprecated form of request with the direct option; body and headers are now options, see request/3
Update an existing resource in the Twilio Api
Functions
Specs
create(atom, list, list) ::
ExTwilio.Parser.success |
ExTwilio.Parser.error
Create a new resource in the Twilio API with a POST request.
Examples
ExTwilio.Api.create(ExTwilio.Call, [to: "1112223333", from: "4445556666"])
{:ok, %Call{ ... }}
ExTwilio.Api.create(ExTwilio.Call, [])
{:error, "No 'To' number is specified", 400}
Destroy an existing resource in the Twilio Api.
Examples
ExTwilio.Api.destroy(ExTwilio.Call, "<sid>")
:ok
ExTwilio.Api.destroy(ExTwilio.Call, "nonexistent")
{:error, "The requested resource ... was not found", 404}
Specs
find(atom, String.t | nil, list) ::
ExTwilio.Parser.success |
ExTwilio.Parser.error
Find a given resource in the Twilio API by its SID.
Examples
If the resource was found, find/2
will return a two-element tuple in this format, {:ok, item}
.
ExTwilio.Api.find(ExTwilio.Call, "<sid here>")
{:ok, %Call{ ... }}
If the resource could not be loaded, find/2
will return a 3-element tuple in this format, {:error, message, code}
. The code
is the HTTP status code returned by the Twilio API, for example, 404.
ExTwilio.Api.find(ExTwilio.Call, "nonexistent sid")
{:error, "The requested resource couldn't be found...", 404}
Specs
process_options(list) :: list
Adds the Account SID and Auth Token to every request through HTTP basic auth.
Example
iex> ExTwilio.Api.process_options([])
[basic_auth: { nil, nil }]
If the request body is a list, then convert the list to a query string. Otherwise, pass it through unmodified.
Examples
iex> ExTwilio.Api.process_request_body([hello: "world"])
"Hello=world"
iex> ExTwilio.Api.process_request_body("Hello, world!")
"Hello, world!"
Specs
process_request_headers(list) :: list
Automatically add the Content-Type application/x-www-form-urlencoded. This allows POST request data to be processed properly. It seems to have no negative effect on GET calls, so it is added to all requests.
Example
iex> ExTwilio.Api.process_request_headers([])
[{:"Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"}]
Specs
Sends an HTTP request. Args: * method - HTTP method, atom (:get, :head, :post, :put, :delete, etc.) * url - URL, binary string or char list * options - orddict of options Options: * body - request body, binary string or char list * headers - HTTP headers, orddict (eg. ["Accept": "application/json"]) * timeout - timeout in ms, integer * basic_auth - basic auth credentials (eg. {"user", "password"}) * stream_to - if you want to make an async request, the pid of the process * direct - if you want to use ibrowse's direct feature, the pid of the worker spawned by spawn_worker_process or spawn_link_worker_process Returns HTTPotion.Response or HTTPotion.AsyncResponse if successful. Raises HTTPotion.HTTPError if failed.
Deprecated form of request; body and headers are now options, see request/3.
Deprecated form of request with the direct option; body and headers are now options, see request/3.
Specs
update(atom, String.t, list, list) ::
ExTwilio.Parser.success |
ExTwilio.Parser.error
Update an existing resource in the Twilio Api.
Examples
ExTwilio.Api.update(ExTwilio.Call, "<sid>", [status: "canceled"])
{:ok, %Call{ status: "canceled" ... }}
ExTwilio.Api.update(ExTwilio.Call, "nonexistent", [status: "complete"])
{:error, "The requested resource ... was not found", 404}