Configurable json library

This commit is contained in:
snewcomer 2018-12-22 22:45:39 -08:00
parent c3cb072511
commit 1c5a5e7229
2 changed files with 28 additions and 4 deletions

View File

@ -10,6 +10,30 @@ defmodule Ueberauth.Strategy.Twitter do
alias Ueberauth.Auth.Extra alias Ueberauth.Auth.Extra
alias Ueberauth.Strategy.Twitter alias Ueberauth.Strategy.Twitter
@json_library Application.get_env(:ueberauth, :json_library, Ueberauth.Config.json_library())
unless Code.ensure_loaded?(@json_library) do
IO.puts("""
The json_library '#{inspect(@json_library)}' does not appear
to be available. A json library is required
for Ueberauth to operate. Is it configured as a
dependency in mix.exs?
In config.exs your expicit or implicit configuration is:
config ueberauth,
json_library: #{inspect(@json_library)}
In mix.exs you will need something like:
def deps() do
[
...
{:#{String.downcase(inspect(@json_library))}, :version}
]
end
""")
raise ArgumentError,
"Json library #{String.downcase(inspect(@json_library))} does " <>
"not appear to be a dependency"
end
@doc """ @doc """
Handles initial request for Twitter authentication. Handles initial request for Twitter authentication.
""" """
@ -105,13 +129,13 @@ defmodule Ueberauth.Strategy.Twitter do
{:ok, %{status_code: 401, body: _, headers: _}} -> {:ok, %{status_code: 401, body: _, headers: _}} ->
set_errors!(conn, [error("token", "unauthorized")]) set_errors!(conn, [error("token", "unauthorized")])
{:ok, %{status_code: status_code, body: body, headers: _}} when status_code in 200..399 -> {:ok, %{status_code: status_code, body: body, headers: _}} when status_code in 200..399 ->
body = Poison.decode!(body) body = @json_library.decode!(body)
conn conn
|> put_private(:twitter_token, token) |> put_private(:twitter_token, token)
|> put_private(:twitter_user, body) |> put_private(:twitter_user, body)
{:ok, %{status_code: _, body: body, headers: _}} -> {:ok, %{status_code: _, body: body, headers: _}} ->
body = Poison.decode!(body) body = @json_library.decode!(body)
error = List.first(body["errors"]) error = List.first(body["errors"])
set_errors!(conn, [error("token", error["message"])]) set_errors!(conn, [error("token", error["message"])])
end end

View File

@ -19,7 +19,7 @@ defmodule Ueberauth.Strategy.Twitter.OAuth do
def access_token({token, token_secret}, verifier, opts \\ []) do def access_token({token, token_secret}, verifier, opts \\ []) do
opts opts
|> client |> client()
|> to_url(:access_token) |> to_url(:access_token)
|> Internal.get([{"oauth_verifier", verifier}], consumer(client()), token, token_secret) |> Internal.get([{"oauth_verifier", verifier}], consumer(client()), token, token_secret)
|> decode_response |> decode_response
@ -34,7 +34,7 @@ defmodule Ueberauth.Strategy.Twitter.OAuth do
def authorize_url!({token, _token_secret}, opts \\ []) do def authorize_url!({token, _token_secret}, opts \\ []) do
opts opts
|> client |> client()
|> to_url(:authorize_url, %{"oauth_token" => token}) |> to_url(:authorize_url, %{"oauth_token" => token})
end end