ExTwilio v0.1.9 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

Functions

Create a new resource in the Twilio API with a POST request

A shortcut for request(:delete, url, options)

A shortcut for request!(:delete, url, options)

Destroy an existing resource in the Twilio Api

Find a given resource in the Twilio API by its SID

A shortcut for request(:get, url, options)

A shortcut for request!(:get, url, options)

A shortcut for request(:head, url, options)

A shortcut for request!(:head, url, options)

A shortcut for request(:options, url, options)

A shortcut for request!(:options, url, options)

A shortcut for request(:patch, url, options)

A shortcut for request!(:patch, url, options)

A shortcut for request(:post, url, options)

A shortcut for request!(:post, url, options)

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

A shortcut for request(:put, url, options)

A shortcut for request!(:put, url, options)

Sends an HTTP request

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

Like request, but raises HTTPotion.HTTPError if failed

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

Starts a linked worker process for use with the direct option

Starts a worker process for use with the direct option

Ensures that HTTPotion and its dependencies are started

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 \\ [])

A shortcut for request(:delete, url, options).

delete!(url, options \\ [])

A shortcut for request!(: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 \\ [])

A shortcut for request(:get, url, options).

get!(url, options \\ [])

A shortcut for request!(:get, url, options).

handle_response(response)
head(url, options \\ [])

A shortcut for request(:head, url, options).

head!(url, options \\ [])

A shortcut for request!(:head, url, options).

is_redirect(response)
options(url, options \\ [])

A shortcut for request(:options, url, options).

options!(url, options \\ [])

A shortcut for request!(:options, url, options).

patch(url, options \\ [])

A shortcut for request(:patch, url, options).

patch!(url, options \\ [])

A shortcut for request!(:patch, url, options).

post(url, options \\ [])

A shortcut for request(:post, url, options).

post!(url, options \\ [])

A shortcut for request!(:post, url, options).

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_response_location(response)
process_status_code(status_code)
process_url(url)
process_url(url, options)
put(url, options \\ [])

A shortcut for request(:put, url, options).

put!(url, options \\ [])

A shortcut for request!(:put, url, options).

request(method, url, options \\ [])

Specs

request(atom, String.t, [{atom, any}]) ::
  %HTTPotion.Response{body: term, headers: term, status_code: term} |
  %HTTPotion.AsyncResponse{id: term} |
  %HTTPotion.ErrorResponse{message: 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/2 or spawn_link_worker_process/2
  • follow_redirects - if true and a response is a redirect, header[:Location] is taken for the next request

Returns HTTPotion.Response or HTTPotion.AsyncResponse if successful. Returns HTTPotion.ErrorResponse if failed.

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

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

request!(method, url, options \\ [])

Specs

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

Like request, but raises HTTPotion.HTTPError if failed.

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.

response_ok(response)
spawn_worker_process(url, options \\ [])

Starts a worker process for use with the direct option.

start()

Ensures that HTTPotion and its dependencies are started.

stop_worker_process(pid)

Stops a worker process started with spawn_worker_process/2 or spawn_link_worker_process/2.

transformer(target, method, url, options)
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}