Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Covering all patterns and preventing errors #133

Merged
merged 3 commits into from
Nov 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions lib/extwitter/api/auth.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@ defmodule ExTwitter.API.Auth do
def request_token(redirect_url \\ nil) do
oauth = ExTwitter.Config.get_tuples |> verify_params
params = if redirect_url, do: [{"oauth_callback", redirect_url}], else: []
{:ok, {{_, 200, _}, _headers, body}} =
response =
ExTwitter.OAuth.request(:post, request_url("oauth/request_token"),
params, oauth[:consumer_key], oauth[:consumer_secret], "", "")

Elixir.URI.decode_query(to_string body)
|> Enum.map(fn {k,v} -> {String.to_atom(k), v} end)
|> Enum.into(%{})
|> ExTwitter.Parser.parse_request_token
case response do
{:ok, {{_, 200, _}, _headers, body}} ->
token =
Elixir.URI.decode_query(to_string body)
|> Enum.map(fn {k,v} -> {String.to_atom(k), v} end)
|> Enum.into(%{})
|> ExTwitter.Parser.parse_request_token
{:ok, token}
_ -> {:error, :unknown}
end
end

def authorize_url(oauth_token, options \\ %{}) do
Expand All @@ -32,16 +38,16 @@ defmodule ExTwitter.API.Auth do
def access_token(verifier, request_token) do
oauth = ExTwitter.Config.get_tuples |> verify_params
response = ExTwitter.OAuth.request(:post, request_url("oauth/access_token"),
[oauth_verifier: verifier], oauth[:consumer_key], oauth[:consumer_secret], request_token, nil)
[{"oauth_verifier", verifier}], oauth[:consumer_key], oauth[:consumer_secret], request_token, nil)
case response do
{:ok, {{_, 200, _}, _headers, body}} ->
access_token = Elixir.URI.decode_query(to_string body)
|> Enum.map(fn {k,v} -> {String.to_atom(k), v} end)
|> Enum.into(%{})
|> ExTwitter.Parser.parse_access_token
{:ok, access_token}
{:ok, {{_, code, _}, _, _}} ->
{:error, code}
_ -> {:error, :unknown}
end
end
end

6 changes: 3 additions & 3 deletions lib/extwitter/behaviour.ex
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ defmodule ExTwitter.Behaviour do
@callback rate_limit_status(Keyword.t()) :: map()
@callback lookup_status(String.t()) :: [ExTwitter.Model.Tweet.t()]
@callback lookup_status(String.t(), Keyword.t()) :: [ExTwitter.Model.Tweet.t()]
@callback request_token(String.t()) :: ExTwitter.Model.RequestToken.t()
@callback request_token :: ExTwitter.Model.RequestToken.t()
@callback request_token(String.t()) :: {:ok, ExTwitter.Model.RequestToken.t()} | {:error, atom()}
@callback request_token() :: {:ok, ExTwitter.Model.RequestToken.t()} | {:error, atom()}
@callback authorize_url(String.t()) :: {:ok, String.t()} | {:error, String.t()}
@callback authorize_url(String.t(), map()) :: {:ok, String.t()} | {:error, String.t()}
@callback authenticate_url(String.t(), map()) :: {:ok, String.t()} | {:error, String.t()}
@callback authenticate_url(String.t()) :: {:ok, String.t()} | {:error, String.t()}
@callback access_token(String.t(), String.t()) :: {:ok, ExTwitter.Model.AccessToken.t()} | {:error, String.t()}
@callback access_token(String.t(), String.t()) :: {:ok, ExTwitter.Model.AccessToken.t()} | {:error, atom()}
end
Loading