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/4
using 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:
- application-only authentication.
- owner-token from dev.twitter.com.
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
Streams data from the given url
Same as stage/6
but raises HTTPoison.Error
if an error occurs during the request
Link to this section Functions
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.
Same as get/4
but raises HTTPoison.Error
if an error occurs during the
request.
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.
get_token(String.t, String.t, Keyword.t) :: {:ok, OAuther.Credentials.t} | {:error, HTTPoison.Error.t}
Returns a OAuth1 token for the given token
and token_secret
.
get_token!(Keyword.t) :: OAuth2.AccessToken.t
Same as get_token/1
but raises OAuth2.Error
if an error occurs during the
request.
get_token!(String.t, String.t, Keyword.t) :: OAuther.Credentials.t
Same as get_token/3
but raises HTTPoison.Error
if an error occurs during the request.
post(pid, String.t, binary, List.t, Keyword.t) :: {:ok, %{}} | {:error, HTTPoison.Error.t}
Issues a POST request to the given url.
Same as post/5
but raises HTTPoison.Error
if an error occurs during the request.
stage(pid, Atom.t, String.t, binary, List.t, Keyword.t) :: {:ok, GenStage.t} | {:error, HTTPoison.Error.t}
Streams data from the given url.
Same as stage/6
but raises HTTPoison.Error
if an error occurs during the request.