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

create(module, data, options \\ [])

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}
delete(url, options \\ [])
destroy(module, sid, options \\ [])

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}
find(module, sid, options \\ [])

Specs

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}
get(url, options \\ [])
handle_response(response)
head(url, options \\ [])
options(url, options \\ [])
patch(url, options \\ [])
post(url, options \\ [])
process_arguments(method, url, options)

Specs

process_arguments(atom, String.t, Dict.t) :: Dict.t
process_options(options)

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 }]
process_request_body(body)

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!"
process_request_headers(headers)

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"}]
process_response_body(body)
process_response_chunk(chunk)
process_response_headers(headers)
process_status_code(status_code)
process_url(url)
put(url, options \\ [])
request(method, url, options \\ [])

Specs

request(atom, String.t, Dict.t) ::
  %HTTPotion.Response{body: term, headers: term, status_code: term} |
  %HTTPotion.AsyncResponse{id: term}

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.

request(method, url, body, headers, options)

Deprecated form of request; body and headers are now options, see request/3.

request_direct(conn_pid, method, url, body \\ "", headers \\ [], options \\ [])

Deprecated form of request with the direct option; body and headers are now options, see request/3.

spawn_worker_process(url, options \\ [])
start()
stop_worker_process(pid)
transformer(target)
update(module, sid, data, options \\ [])

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}