Twittex v0.3.5 Twittex.Client.Base View Source

A behaviour module for implementing your own Twitter client.

Provides convenience functions for working with Twitter’s RESTful API. You can use get/3 and post/4using a relative url pointing to the API endpoint.

Example

To create your own client, create a new module and use Twittex.Client.Base as follow:

defmodule TwitterBot do
  use Twittex.Client.Base

  def search(term, options \ []) do
    get "/search/tweets.json?" <> URI.encode_query(Keyword.merge(%{q: term}, options))
  end
end

This client works as a singleton and can be added to a supervisor tree:

Supervisor.start_link([TwittexBot.child_spec], strategy: :one_for_one)

And here’s how you may use it:

TwitterBot.search "#myelixirstatus", count: 3

Authentication

Twittex supports following OAuth authentication methods:

To request an access token with one of the method listed above. See get_token/1 and get_token/3. Here’s, a brief example for application-only authentication:

iex> token = Twittex.Client.Base.get_token!
%OAuth2.AccessToken{...}

Under the hood, the Twittex.Client.Base module uses HTTPoison.Base and overrides the request/5 method to pass the authentication headers along the request.

Link to this section Summary

Functions

Issues a GET request to the given url

Same as get/4 but raises HTTPoison.Error if an error occurs during the request

Returns a OAuth2 application-only token

Returns a OAuth1 token for the given token and token_secret

Same as get_token/1 but raises OAuth2.Error if an error occurs during the request

Same as get_token/3 but raises HTTPoison.Error if an error occurs during the request

Issues a POST request to the given url

Same as post/5 but raises HTTPoison.Error if an error occurs during the request

Same as stage/6 but raises HTTPoison.Error if an error occurs during the request

Link to this section Functions

Link to this function get(pid, url, headers \\ [], options \\ []) View Source
get(pid, String.t, List.t, Keyword.t) ::
  {:ok, %{}} |
  {:error, HTTPoison.Error.t}

Issues a GET request to the given url.

Returns {:ok, response} if the request is successful, {:error, reason} otherwise.

Link to this function get!(pid, url, headers \\ [], options \\ []) View Source
get!(pid, String.t, List.t, Keyword.t) :: %{}

Same as get/4 but raises HTTPoison.Error if an error occurs during the request.

Link to this function get_token(options \\ []) View Source
get_token(Keyword.t) ::
  {:ok, OAuth2.AccessToken.t} |
  {:error, OAuth2.Error.t}

Returns a OAuth2 application-only token.

With application-only authentication you don’t have the context of an authenticated user and this means that accessing APIs that require user context, will not work.

Link to this function get_token(token, token_secret, options \\ []) View Source

Returns a OAuth1 token for the given token and token_secret.

Same as get_token/1 but raises OAuth2.Error if an error occurs during the request.

Link to this function get_token!(token, token_secret, options \\ []) View Source

Same as get_token/3 but raises HTTPoison.Error if an error occurs during the request.

Link to this function post(pid, url, body \\ [], headers \\ [], options \\ []) View Source
post(pid, String.t, binary, List.t, Keyword.t) ::
  {:ok, %{}} |
  {:error, HTTPoison.Error.t}

Issues a POST request to the given url.

Link to this function post!(pid, url, body, headers \\ [], options \\ []) View Source
post!(pid, String.t, binary, List.t, Keyword.t) :: %{}

Same as post/5 but raises HTTPoison.Error if an error occurs during the request.

Link to this function stage(pid, method, url, body \\ [], headers \\ [], options \\ []) View Source
stage(pid, Atom.t, String.t, binary, List.t, Keyword.t) ::
  {:ok, GenStage.t} |
  {:error, HTTPoison.Error.t}

Streams data from the given url.

Link to this function stage!(pid, method, url, body \\ [], headers \\ [], options \\ []) View Source
stage!(pid, Atom.t, String.t, binary, List.t, Keyword.t) :: Twittex.Client.Stream.t

Same as stage/6 but raises HTTPoison.Error if an error occurs during the request.