Replace mojito with finch

This commit is contained in:
Adrian Kumpf
2020-05-09 00:16:59 +02:00
parent df570f801b
commit 60631907ef
15 changed files with 101 additions and 66 deletions

View File

@@ -18,8 +18,8 @@ config :teslamate, TeslaMateWeb.Endpoint,
patterns: [
~r"priv/static/.*(js|css|png|jpeg|jpg|gif|svg)$",
~r"priv/gettext/.*(po)$",
~r"lib/test_web/(live|views)/.*(ex)$",
~r"lib/test_web/templates/.*(eex)$",
~r"lib/teslamate_web/(live|views)/.*(ex)$",
~r"lib/teslamate_web/templates/.*(eex)$",
~r"grafana/dashboards/.*(json)$"
]
]

View File

@@ -1,6 +1,6 @@
defmodule TeslaApi do
alias Mojito.Response, as: Res
alias Mojito.Error, as: Err
alias Finch.Response, as: Res
alias TeslaMate.HTTP
alias __MODULE__.Error
@base_url URI.parse("https://owner-api.teslamotors.com/")
@@ -10,40 +10,40 @@ defmodule TeslaApi do
def get(path, token, opts \\ []) when is_binary(token) do
headers = [{"user-agent", @user_agent}, {"Authorization", "Bearer " <> token}]
case Mojito.get(url(path), headers, timeout: @timeout) do
case HTTP.get(url(path), headers, receive_timeout: @timeout) do
{:ok, %Res{} = response} ->
case decode_body(response) do
%Res{complete: false} = env ->
{:error, %Error{reason: :incomplete_response, env: env}}
%Res{status_code: status, body: %{"response" => res}} when status in 200..299 ->
%Res{status: status, body: %{"response" => res}} when status in 200..299 ->
transform = Keyword.get(opts, :transform, & &1)
{:ok, if(is_list(res), do: Enum.map(res, transform), else: transform.(res))}
%Res{status_code: 401} = env ->
%Res{status: 401} = env ->
{:error, %Error{reason: :unauthorized, env: env}}
%Res{status_code: 404, body: %{"error" => "not_found"}} = env ->
%Res{status: 404, body: %{"error" => "not_found"}} = env ->
{:error, %Error{reason: :vehicle_not_found, env: env}}
%Res{status_code: 405, body: %{"error" => "vehicle is curently in service"}} = env ->
%Res{status: 405, body: %{"error" => "vehicle is curently in service"}} = env ->
{:error, %Error{reason: :vehicle_in_service, env: env}}
%Res{status_code: 408, body: %{"error" => "vehicle unavailable:" <> _}} = env ->
%Res{status: 408, body: %{"error" => "vehicle unavailable:" <> _}} = env ->
{:error, %Error{reason: :vehicle_unavailable, env: env}}
%Res{status_code: 504} = env ->
%Res{status: 504} = env ->
{:error, %Error{reason: :timeout, env: env}}
%Res{status_code: status, body: %{"error" => msg}} = env when status >= 500 ->
%Res{status: status, body: %{"error" => msg}} = env when status >= 500 ->
{:error, %Error{reason: :unknown, message: msg, env: env}}
%Res{body: body} = env ->
{:error, %Error{reason: :unknown, message: inspect(body), env: env}}
end
{:error, %Err{reason: reason, message: msg}} ->
{:error, %Error{reason: reason, message: msg}}
{:error, %Mint.TransportError{reason: reason} = transport_error} ->
{:error, %Error{reason: reason, message: Exception.message(transport_error)}}
{:error, %Mint.HTTPError{reason: reason} = transport_error} ->
{:error, %Error{reason: reason, message: Exception.message(transport_error)}}
end
end
@@ -56,7 +56,7 @@ defmodule TeslaApi do
| if(is_nil(token), do: [], else: [{"Authorization", "Bearer " <> token}])
]
with {:ok, response} <- path |> url() |> Mojito.post(headers, body, timeout: @timeout) do
with {:ok, response} <- HTTP.post(url(path), headers, body, receive_timeout: @timeout) do
{:ok, decode_body(response)}
end
end

View File

@@ -36,13 +36,13 @@ defmodule TeslaApi.Auth do
defp handle_response(response) do
case response do
{:ok, %Mojito.Response{status_code: 200, body: body}} when body == %{} ->
{:ok, %Finch.Response{status: 200, body: body}} when body == %{} ->
:ok
{:ok, %Mojito.Response{status_code: 200, body: %{"response" => true}}} ->
{:ok, %Finch.Response{status: 200, body: %{"response" => true}}} ->
:ok
{:ok, %Mojito.Response{status_code: 200, body: body}} when is_map(body) ->
{:ok, %Finch.Response{status: 200, body: body}} when is_map(body) ->
auth = %__MODULE__{
token: body["access_token"],
type: body["token_type"],
@@ -53,7 +53,7 @@ defmodule TeslaApi.Auth do
{:ok, auth}
{:ok, %Mojito.Response{status_code: 401} = e} ->
{:ok, %Finch.Response{status: 401} = e} ->
error = %Error{
reason: :authentication_failure,
message: "Failed to authenticate.",
@@ -62,10 +62,10 @@ defmodule TeslaApi.Auth do
{:error, error}
{:ok, %Mojito.Response{} = e} ->
{:ok, %Finch.Response{} = e} ->
{:error, %Error{reason: :unknown, message: "An unknown error has occurred.", env: e}}
{:error, %Mojito.Error{reason: reason} = e} ->
{:error, %{reason: reason} = e} ->
error = %Error{
reason: :unknown,
message: "An unknown error has occurred: #{inspect(reason)}",

View File

@@ -7,7 +7,7 @@ defmodule TeslaMate.Api do
alias TeslaMate.Vehicles
alias TeslaApi.Auth
alias Mojito.Response
alias Finch.Response
import Core.Dependency, only: [call: 3, call: 2]
@@ -179,7 +179,7 @@ defmodule TeslaMate.Api do
true = :ets.delete(name, :auth)
{:error, :not_signed_in}
{:error, %TeslaApi.Error{reason: reason, env: %Response{status_code: status, body: body}}} ->
{:error, %TeslaApi.Error{reason: reason, env: %Response{status: status, body: body}}} ->
Logger.error("TeslaApi.Error / #{status} #{inspect(body, pretty: true)}")
{:error, reason}

View File

@@ -20,6 +20,7 @@ defmodule TeslaMate.Application do
nil ->
[
TeslaMate.Repo,
TeslaMate.HTTP,
TeslaMate.Api,
TeslaMate.Updater,
{Phoenix.PubSub, name: TeslaMate.PubSub},
@@ -34,6 +35,7 @@ defmodule TeslaMate.Application do
import_directory ->
[
TeslaMate.Repo,
TeslaMate.HTTP,
TeslaMate.Api,
TeslaMate.Updater,
{Phoenix.PubSub, name: TeslaMate.PubSub},

31
lib/teslamate/http.ex Normal file
View File

@@ -0,0 +1,31 @@
defmodule TeslaMate.HTTP do
def child_spec(arg) do
%{
id: __MODULE__,
start:
{Finch, :start_link,
[
Keyword.merge(
[
name: __MODULE__,
pools: %{
:default => [size: 5],
"https://owner-api.teslamotors.com" => [size: 10],
"https://nominatim.openstreetmap.org" => [size: 3],
"https://api.github.com" => [size: 1]
}
],
arg
)
]}
}
end
def get(url, headers \\ [], opts \\ []) do
Finch.request(__MODULE__, :get, url, headers, nil, opts)
end
def post(url, headers \\ [], body \\ nil, opts \\ []) do
Finch.request(__MODULE__, :post, url, headers, body, opts)
end
end

View File

@@ -1,5 +1,6 @@
defmodule TeslaMate.Locations.Geocoder do
alias Mojito.{Response, Error}
alias Finch.Response
alias TeslaMate.HTTP
alias TeslaMate.Locations.Address
def reverse_lookup(lat, lon, lang \\ "en") do
@@ -42,10 +43,10 @@ defmodule TeslaMate.Locations.Geocoder do
defp fetch(url, lang, params) do
url = assemble_url(url, params)
case Mojito.get(url, headers(lang), timeout: 15_000) do
{:ok, %Response{status_code: 200, body: body}} -> {:ok, Jason.decode!(body)}
case HTTP.get(url, headers(lang), receive_timeout: 15_000) do
{:ok, %Response{status: 200, body: body}} -> {:ok, Jason.decode!(body)}
{:ok, %Response{body: body}} -> {:error, Jason.decode!(body) |> Map.get("error")}
{:error, %Error{reason: reason}} -> {:error, reason}
{:error, %{reason: reason}} -> {:error, reason}
end
end

View File

@@ -1,7 +1,8 @@
defmodule TeslaMate.Updater do
use GenServer
alias Mojito.Response
alias Finch.Response
alias TeslaMate.HTTP
require Logger
defmodule State, do: defstruct([:update, :version])
@@ -80,8 +81,8 @@ defmodule TeslaMate.Updater do
defp version, do: "#{Application.spec(:teslamate, :vsn)}"
defp fetch_release do
case Mojito.get(@url, [], timeout: 30_000) do
{:ok, %Response{status_code: 200, body: body}} ->
case HTTP.get(@url, [], receive_timeout: 30_000) do
{:ok, %Response{status: 200, body: body}} ->
with {:ok, release} <- Jason.decode(body) do
parse_release(release)
end

View File

@@ -531,7 +531,7 @@ defmodule TeslaMate.Vehicles.Vehicle do
end
def handle_event(:info, message, _state, data) do
Logger.debug("Unhandled message: #{inspect(message, pretty: true)}", car_id: data.car.id)
Logger.info("Unhandled message: #{inspect(message, pretty: true)}", car_id: data.car.id)
:keep_state_and_data
end

View File

@@ -51,7 +51,6 @@ defmodule TeslaMate.MixProject do
{:floki, "~> 0.23", only: :test},
{:tortoise, "~> 0.9"},
{:excoveralls, "~> 0.10", only: :test},
{:mojito, "~> 0.5"},
{:srtm, "~> 0.5"},
{:fuse, "~> 2.4"},
{:mock, "~> 0.3", only: :test},
@@ -60,7 +59,8 @@ defmodule TeslaMate.MixProject do
{:csv, "~> 2.3"},
{:timex, "~> 3.0"},
{:websockex, "~> 0.4"},
{:tzdata, "~> 1.0"}
{:tzdata, "~> 1.0"},
{:finch, "~> 0.2"}
]
end

View File

@@ -1,7 +1,7 @@
%{
"castore": {:hex, :castore, "0.1.5", "591c763a637af2cc468a72f006878584bc6c306f8d111ef8ba1d4c10e0684010", [:mix], [], "hexpm", "6db356b2bc6cc22561e051ff545c20ad064af57647e436650aa24d7d06cd941a"},
"certifi": {:hex, :certifi, "2.5.1", "867ce347f7c7d78563450a18a6a28a8090331e77fa02380b4a21962a65d36ee5", [:rebar3], [{:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm", "805abd97539caf89ec6d4732c91e62ba9da0cda51ac462380bbd28ee697a8c42"},
"cldr_utils": {:hex, :cldr_utils, "2.8.0", "a1355a658fdf7118a678002a5333562d464e1bbdc3c89a5e0c3d8088038e57f5", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}], "hexpm", "b37b2ae815bf62a01ba4deb90bd75c70fa0ed07f05b5a1b530d21a99b223844b"},
"cldr_utils": {:hex, :cldr_utils, "2.9.1", "be714403abe1a7abed5ee4f7dd3823a9067f96ab4b0613a454177b51ca204236", [:mix], [{:decimal, "~> 1.6", [hex: :decimal, repo: "hexpm", optional: false]}], "hexpm", "6cba0a485f57feb773291ca1816469ddd887e22d73d9b12a1b207d82a67a4e71"},
"combine": {:hex, :combine, "0.10.0", "eff8224eeb56498a2af13011d142c5e7997a80c8f5b97c499f84c841032e429f", [:mix], [], "hexpm", "1b1dbc1790073076580d0d1d64e42eae2366583e7aecd455d1215b0d16f2451b"},
"connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], [], "hexpm", "4a0850c9be22a43af9920a71ab17c051f5f7d45c209e40269a1938832510e4d9"},
"cowboy": {:hex, :cowboy, "2.7.0", "91ed100138a764355f43316b1d23d7ff6bdb0de4ea618cb5d8677c93a7a2f115", [:rebar3], [{:cowlib, "~> 2.8.0", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.7.1", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "04fd8c6a39edc6aaa9c26123009200fc61f92a3a94f3178c527b70b767c6e605"},
@@ -9,12 +9,13 @@
"csv": {:hex, :csv, "2.3.1", "9ce11eff5a74a07baf3787b2b19dd798724d29a9c3a492a41df39f6af686da0e", [:mix], [{:parallel_stream, "~> 1.0.4", [hex: :parallel_stream, repo: "hexpm", optional: false]}], "hexpm", "86626e1c89a4ad9a96d0d9c638f9e88c2346b89b4ba1611988594ebe72b5d5ee"},
"db_connection": {:hex, :db_connection, "2.2.2", "3bbca41b199e1598245b716248964926303b5d4609ff065125ce98bcd368939e", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, repo: "hexpm", optional: false]}], "hexpm", "642af240d8a8affb93b4ba5a6fcd2bbcbdc327e1a524b825d383711536f8070c"},
"decimal": {:hex, :decimal, "1.8.1", "a4ef3f5f3428bdbc0d35374029ffcf4ede8533536fa79896dd450168d9acdf3c", [:mix], [], "hexpm", "3cb154b00225ac687f6cbd4acc4b7960027c757a5152b369923ead9ddbca7aec"},
"ecto": {:hex, :ecto, "3.4.3", "3a14c2500c3964165245a4f24a463e080762f7ccd0c632c763ea589f75ca205f", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "9b6f18dea95f2004d0369f6a8346513ca3f706614f4ede219a5f3fe5db5dd962"},
"ecto": {:hex, :ecto, "3.4.4", "a2c881e80dc756d648197ae0d936216c0308370332c5e77a2325a10293eef845", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "cc4bd3ad62abc3b21fb629f0f7a3dab23a192fca837d257dd08449fba7373561"},
"ecto_enum": {:hex, :ecto_enum, "1.4.0", "d14b00e04b974afc69c251632d1e49594d899067ee2b376277efd8233027aec8", [:mix], [{:ecto, ">= 3.0.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:ecto_sql, "> 3.0.0", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:mariaex, ">= 0.0.0", [hex: :mariaex, repo: "hexpm", optional: true]}, {:postgrex, ">= 0.0.0", [hex: :postgrex, repo: "hexpm", optional: true]}], "hexpm", "8fb55c087181c2b15eee406519dc22578fa60dd82c088be376d0010172764ee4"},
"ecto_sql": {:hex, :ecto_sql, "3.4.3", "c552aa8a7ccff2b64024f835503b3155d8e73452c180298527fbdbcd6e79710b", [:mix], [{:db_connection, "~> 2.2", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.4.3", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.3.0 or ~> 0.4.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.15.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.0", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "ec9e59d6fa3f8cfda9963ada371e9e6659167c2338a997bd7ea23b10b245842b"},
"ex_cldr": {:hex, :ex_cldr, "2.13.0", "742f14a4afcfea61a190d603d8e555d2c91d71e4e8fc2520d5dc35616969e225", [:mix], [{:cldr_utils, "~> 2.3", [hex: :cldr_utils, repo: "hexpm", optional: false]}, {:decimal, "~> 1.6", [hex: :decimal, repo: "hexpm", optional: false]}, {:gettext, "~> 0.13", [hex: :gettext, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:plug, "~> 1.4", [hex: :plug, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm", "5e4cf3e945ee60156a3342e2a762f69036ffbe1f80520cc88592d68f12c5db55"},
"excoveralls": {:hex, :excoveralls, "0.12.3", "2142be7cb978a3ae78385487edda6d1aff0e482ffc6123877bb7270a8ffbcfe0", [:mix], [{:hackney, "~> 1.0", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "568a3e616c264283f5dea5b020783ae40eef3f7ee2163f7a67cbd7b35bcadada"},
"file_system": {:hex, :file_system, "0.2.8", "f632bd287927a1eed2b718f22af727c5aeaccc9a98d8c2bd7bff709e851dc986", [:mix], [], "hexpm", "97a3b6f8d63ef53bd0113070102db2ce05352ecf0d25390eb8d747c2bde98bca"},
"finch": {:hex, :finch, "0.2.0", "be68a5cb459f6994faeb8a43de78eb19e3d3a9676665b79d2d890f17b8c6aec0", [:mix], [{:castore, "~> 0.1.5", [hex: :castore, repo: "hexpm", optional: false]}, {:mint, "~> 1.0", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.2.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 0.1.0", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "f1ed3147f22d61beeb5ff48d10f218e4ef8ad76e727526bf3816a40186ab1cbc"},
"floki": {:hex, :floki, "0.26.0", "4df88977e2e357c6720e1b650f613444bfb48c5acfc6a0c646ab007d08ad13bf", [:mix], [{:html_entities, "~> 0.5.0", [hex: :html_entities, repo: "hexpm", optional: false]}], "hexpm", "e7b66ce7feef5518a9cd9fc7b52dd62a64028bd9cb6d6ad282a0f0fc90a4ae52"},
"fuse": {:hex, :fuse, "2.4.2", "9106b08db8793a34cc156177d7e24c41bd638ee1b28463cb76562fde213e8ced", [:rebar3], [], "hexpm", "d733fe913ba3e61ca51f78a3958237e5a24295a03d2ee358288d53bd947f7788"},
"gen_state_machine": {:hex, :gen_state_machine, "2.1.0", "a38b0e53fad812d29ec149f0d354da5d1bc0d7222c3711f3a0bd5aa608b42992", [:mix], [], "hexpm", "ae367038808db25cee2f2c4b8d0531522ea587c4995eb6f96ee73410a60fa06b"},
@@ -29,7 +30,8 @@
"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"},
"mint": {:hex, :mint, "1.0.0", "ca5ab33497ba2bdcc42f6cdd3927420a6159116be87c8173658e93c8746703da", [:mix], [{:castore, "~> 0.1.0", [hex: :castore, repo: "hexpm", optional: true]}], "hexpm", "b8943ef1e630879538dd6620bfc189d4d75fab3ad39f3fe9c50539879f7efd84"},
"mock": {:hex, :mock, "0.3.4", "c5862eb3b8c64237f45f586cf00c9d892ba07bb48305a43319d428ce3c2897dd", [:mix], [{:meck, "~> 0.8.13", [hex: :meck, repo: "hexpm", optional: false]}], "hexpm", "e6d886252f1a41f4ba06ecf2b4c8d38760b34b1c08a11c28f7397b2e03995964"},
"mojito": {:hex, :mojito, "0.6.3", "1d9af4bee09c5489d189263c4558c1d5b0cc2c2a6d94d2426590077a6b4287ef", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: false]}, {:mint, "~> 1.0", [hex: :mint, repo: "hexpm", optional: false]}, {:poolboy, "~> 1.5", [hex: :poolboy, repo: "hexpm", optional: false]}], "hexpm", "99c40fe806bfc8bdecdd9b8ac111cc3834ae638afd3e233be554f3ed5811c1f9"},
"nimble_options": {:hex, :nimble_options, "0.2.1", "7eac99688c2544d4cc3ace36ee8f2bf4d738c14d031bd1e1193aab096309d488", [:mix], [], "hexpm", "ca48293609306791ce2634818d849b7defe09330adb7e4e1118a0bc59bed1cf4"},
"nimble_pool": {:hex, :nimble_pool, "0.1.0", "ffa9d5be27eee2b00b0c634eb649aa27f97b39186fec3c493716c2a33e784ec6", [:mix], [], "hexpm", "343a1eaa620ddcf3430a83f39f2af499fe2370390d4f785cd475b4df5acaf3f9"},
"parallel_stream": {:hex, :parallel_stream, "1.0.6", "b967be2b23f0f6787fab7ed681b4c45a215a81481fb62b01a5b750fa8f30f76c", [:mix], [], "hexpm", "639b2e8749e11b87b9eb42f2ad325d161c170b39b288ac8d04c4f31f8f0823eb"},
"parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm", "17ef63abde837ad30680ea7f857dd9e7ced9476cdd7b0394432af4bfc241b960"},
"phoenix": {:hex, :phoenix, "1.5.1", "95156589879dc69201d5fc0ebdbfdfc7901a09a3616ea611ec297f81340275a2", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_html, "~> 2.13", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 2.0", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:plug, "~> 1.10", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 1.0 or ~> 2.2", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:plug_crypto, "~> 1.1.2 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "fc272b38e79d2881790fccae6f67a9fbe9b790103d6878175ea03d23003152eb"},
@@ -41,7 +43,6 @@
"plug": {:hex, :plug, "1.10.0", "6508295cbeb4c654860845fb95260737e4a8838d34d115ad76cd487584e2fc4d", [:mix], [{:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm", "422a9727e667be1bf5ab1de03be6fa0ad67b775b2d84ed908f3264415ef29d4a"},
"plug_cowboy": {:hex, :plug_cowboy, "2.2.1", "fcf58aa33227a4322a050e4783ee99c63c031a2e7f9a2eb7340d55505e17f30f", [:mix], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "3b43de24460d87c0971887286e7a20d40462e48eb7235954681a20cee25ddeb6"},
"plug_crypto": {:hex, :plug_crypto, "1.1.2", "bdd187572cc26dbd95b87136290425f2b580a116d3fb1f564216918c9730d227", [:mix], [], "hexpm", "6b8b608f895b6ffcfad49c37c7883e8df98ae19c6a28113b02aa1e9c5b22d6b5"},
"poolboy": {:hex, :poolboy, "1.5.2", "392b007a1693a64540cead79830443abf5762f5d30cf50bc95cb2c1aaafa006b", [:rebar3], [], "hexpm", "dad79704ce5440f3d5a3681c8590b9dc25d1a561e8f5a9c995281012860901e3"},
"postgrex": {:hex, :postgrex, "0.15.4", "5d691c25fc79070705a2ff0e35ce0822b86a0ee3c6fdb7a4fb354623955e1aed", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 2.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm", "306515b9d975fcb2478dc337a1d27dc3bf8af7cd71017c333fe9db3a3d211b0a"},
"ranch": {:hex, :ranch, "1.7.1", "6b1fab51b49196860b733a49c07604465a47bdb78aa10c1c16a3d199f7f8c881", [:rebar3], [], "hexpm", "451d8527787df716d99dc36162fca05934915db0b6141bbdac2ea8d3c7afc7d7"},
"srtm": {:hex, :srtm, "0.5.1", "042c33dff1ba3824f371a6813335062597861617cf951532e8b7343137cb294d", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: false]}, {:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: false]}, {:mint, "~> 1.0", [hex: :mint, repo: "hexpm", optional: false]}, {:tesla, "~> 1.3", [hex: :tesla, repo: "hexpm", optional: false]}], "hexpm", "6be647847b1070a1294f3a59c4de75a9e07159f393479e36e76e09e593dacd28"},

View File

@@ -148,7 +148,7 @@ defmodule TeslaMate.ApiTest do
test "fails if api returns error", %{test: name} do
login = fn _email, _password ->
{:error, %TeslaApi.Error{reason: :unauthorized, env: %Mojito.Response{}}}
{:error, %TeslaApi.Error{reason: :unauthorized, env: %Finch.Response{}}}
end
with_mock TeslaApi.Auth, login: login do
@@ -222,13 +222,13 @@ defmodule TeslaMate.ApiTest do
{TeslaApi.Vehicle, [],
[
list: fn _ ->
{:error, %TeslaApi.Error{reason: :unauthorized, env: %Mojito.Response{}}}
{:error, %TeslaApi.Error{reason: :unauthorized, env: %Finch.Response{}}}
end,
get: fn _, _ ->
{:error, %TeslaApi.Error{reason: :unauthorized, env: %Mojito.Response{}}}
{:error, %TeslaApi.Error{reason: :unauthorized, env: %Finch.Response{}}}
end,
get_with_state: fn _, _ ->
{:error, %TeslaApi.Error{reason: :unauthorized, env: %Mojito.Response{}}}
{:error, %TeslaApi.Error{reason: :unauthorized, env: %Finch.Response{}}}
end
]}
@@ -255,13 +255,13 @@ defmodule TeslaMate.ApiTest do
{TeslaApi.Vehicle, [],
[
list: fn _ ->
{:error, %TeslaApi.Error{reason: :unauthorized, env: %Mojito.Response{}}}
{:error, %TeslaApi.Error{reason: :unauthorized, env: %Finch.Response{}}}
end,
get: fn _, _ ->
{:error, %TeslaApi.Error{reason: :unauthorized, env: %Mojito.Response{}}}
{:error, %TeslaApi.Error{reason: :unauthorized, env: %Finch.Response{}}}
end,
get_with_state: fn _, _ ->
{:error, %TeslaApi.Error{reason: :unauthorized, env: %Mojito.Response{}}}
{:error, %TeslaApi.Error{reason: :unauthorized, env: %Finch.Response{}}}
end
]}
@@ -281,7 +281,7 @@ defmodule TeslaMate.ApiTest do
@tag :capture_log
test ":vehicle_not_found", %{test: name} do
api_error = %TeslaApi.Error{reason: :vehicle_not_found, env: %Mojito.Response{}}
api_error = %TeslaApi.Error{reason: :vehicle_not_found, env: %Finch.Response{}}
vehicle_mock =
{TeslaApi.Vehicle, [],
@@ -304,7 +304,7 @@ defmodule TeslaMate.ApiTest do
api_error = %TeslaApi.Error{
reason: :unknown,
message: "",
env: %Mojito.Response{status_code: 503, body: ""}
env: %Finch.Response{status: 503, body: ""}
}
vehicle_mock =

View File

@@ -6,10 +6,9 @@ defmodule TeslaMate.Locations.GeocoderTest do
import Mock
@response {:ok,
%Mojito.Response{
%Finch.Response{
body:
"{\"place_id\":241575531,\"licence\":\"Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright\",\"osm_type\":\"node\",\"osm_id\":5983038298,\"lat\":\"37.8895442\",\"lon\":\"41.1288167\",\"place_rank\":30,\"category\":\"amenity\",\"type\":\"cafe\",\"importance\":0,\"addresstype\":\"amenity\",\"name\":\"Kahve Deryası\",\"display_name\":\"Kahve Deryası, Cihan Kavşağı, Batman, Ziyagökalp Mahallesi, Batman merkez, Batman, Southeastern Anatolia Region, 72060, Turkey\",\"address\":{\"cafe\":\"Kahve Deryası\",\"road\":\"Cihan Kavşağı\",\"residential\":\"Batman\",\"suburb\":\"Ziyagökalp Mahallesi\",\"city\":\"Batman merkez\",\"state\":\"Southeastern Anatolia Region\",\"postcode\":\"72060\",\"country\":\"Turkey\",\"country_code\":\"tr\"},\"extratags\":{},\"namedetails\":{\"name\":\"Kahve Deryası\"},\"boundingbox\":[\"37.8894442\",\"37.8896442\",\"41.1287167\",\"41.1289167\"]}",
complete: true,
headers: [
{"date", "Sun, 01 Sep 2019 21:03:23 GMT"},
{"server", "Apache/2.4.29 (Ubuntu)"},
@@ -20,11 +19,11 @@ defmodule TeslaMate.Locations.GeocoderTest do
"max-age=0, report-uri=\"https://openstreetmap.report-uri.com/r/d/ct/reportOnly\""},
{"content-type", "application/json; charset=UTF-8"}
],
status_code: 200
status: 200
}}
test "geocoders coordinates" do
with_mock Mojito,
with_mock TeslaMate.HTTP,
get:
fn "https://nominatim.openstreetmap.org/reverse?format=jsonv2&addressdetails=1&extratags=1&namedetails=1&zoom=19&lat=37.889602&lon=41.129182",
_headers,

View File

@@ -4,7 +4,7 @@ defmodule TeslaMate.UpdaterTest do
alias TeslaMate.Updater
import Mock
defmodule MojitoMock do
defmodule HTTPMocck do
def vsn(tag) do
json(%{"tag_name" => tag, "prerelease" => false, "draft" => false})
end
@@ -12,12 +12,12 @@ defmodule TeslaMate.UpdaterTest do
def json(data) do
data
|> Jason.encode!()
|> (&{:ok, %Mojito.Response{status_code: 200, body: &1}}).()
|> (&{:ok, %Finch.Response{status: 200, body: &1}}).()
|> response()
end
def response(resp) do
[{Mojito, [], get: fn _, _, _ -> resp end}]
[{TeslaMate.HTTP, [], get: fn _, _, _ -> resp end}]
end
end
@@ -31,7 +31,7 @@ defmodule TeslaMate.UpdaterTest do
end
test "informs if an update is available", %{test: name} do
with_mocks MojitoMock.vsn("v5.1.2") do
with_mocks HTTPMocck.vsn("v5.1.2") do
## current_version > new_version
{:ok, pid} = start_updater(name, "5.1.3-dev", id: 0)
Process.sleep(100)
@@ -50,7 +50,7 @@ defmodule TeslaMate.UpdaterTest do
end
test "returns early even though update check is still in progress", %{test: name} do
with_mocks [{Mojito, [], get: fn _, _, _ -> Process.sleep(1_000_000) end}] do
with_mocks [{TeslaMate.HTTP, [], get: fn _, _, _ -> Process.sleep(1_000_000) end}] do
{:ok, pid} = start_updater(name, "1.0.0")
assert nil == Updater.get_update(pid)
end
@@ -58,7 +58,7 @@ defmodule TeslaMate.UpdaterTest do
@tag :capture_log
test "handles invalid tags", %{test: name} do
with_mocks MojitoMock.vsn("2.0.0") do
with_mocks HTTPMocck.vsn("2.0.0") do
{:ok, pid} = start_updater(name, "1.0.0")
assert nil == Updater.get_update(pid)
end
@@ -66,7 +66,7 @@ defmodule TeslaMate.UpdaterTest do
@tag :capture_log
test "handles invalid json", %{test: name} do
with_mocks MojitoMock.json(%{foo: :bar}) do
with_mocks HTTPMocck.json(%{foo: :bar}) do
{:ok, pid} = start_updater(name, "1.0.0")
assert nil == Updater.get_update(pid)
end
@@ -74,24 +74,24 @@ defmodule TeslaMate.UpdaterTest do
@tag :capture_log
test "handles HTTP errors", %{test: name} do
with_mocks MojitoMock.response({:ok, %Mojito.Response{status_code: 404}}) do
with_mocks HTTPMocck.response({:ok, %Finch.Response{status: 404}}) do
{:ok, pid} = start_updater(name, "1.0.0", id: 0)
assert nil == Updater.get_update(pid)
end
with_mocks MojitoMock.response({:error, :timeout}) do
with_mocks HTTPMocck.response({:error, :timeout}) do
{:ok, pid} = start_updater(name, "1.0.0", id: 1)
assert nil == Updater.get_update(pid)
end
end
test "handles prereleases and drafts", %{test: name} do
with_mocks MojitoMock.json(%{"tag_name" => "v99.0.0", "prerelease" => true, "draft" => false}) do
with_mocks HTTPMocck.json(%{"tag_name" => "v99.0.0", "prerelease" => true, "draft" => false}) do
{:ok, pid} = start_updater(name, "1.0.0", id: 0)
assert nil == Updater.get_update(pid)
end
with_mocks MojitoMock.json(%{"tag_name" => "v99.0.0", "prerelease" => false, "draft" => true}) do
with_mocks HTTPMocck.json(%{"tag_name" => "v99.0.0", "prerelease" => false, "draft" => true}) do
{:ok, pid} = start_updater(name, "1.0.0", id: 1)
assert nil == Updater.get_update(pid)
end

View File

@@ -515,8 +515,8 @@ defmodule TeslaMateWeb.SettingsLiveTest do
def github_mock do
release = %{"tag_name" => "v1.1.3", "prerelease" => false, "draft" => false}
resp = %Mojito.Response{status_code: 200, body: Jason.encode!(release)}
{Mojito, [], get: fn _, _, _ -> {:ok, resp} end}
resp = %Finch.Response{status: 200, body: Jason.encode!(release)}
{TeslaMate.HTTP, [], get: fn _, _, _ -> {:ok, resp} end}
end
test "informs if an update is available", %{conn: conn} do