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

fix: use product api endpoint to fetch vehicles #3630

Merged
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
12 changes: 9 additions & 3 deletions lib/tesla_api/vehicle.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ defmodule TeslaApi.Vehicle do
_global -> "https://owner-api.teslamotors.com"
end

TeslaApi.get(endpoint_url <> "/api/1/vehicles", opts: [access_token: auth.token])
|> handle_response(transform: &result/1)
TeslaApi.get(endpoint_url <> "/api/1/products", opts: [access_token: auth.token])
|> handle_response(transform: &list_result/1)
end

def get(%Auth{} = auth, id) do
Expand Down Expand Up @@ -61,6 +61,12 @@ defmodule TeslaApi.Vehicle do
|> handle_response(transform: &result/1)
end

def list_result(result) do
result
|> Enum.filter(fn x -> Map.has_key?(x, "vehicle_id") end)
|> Enum.map(&result/1)
end

def result(v) do
%__MODULE__{
id: v["id"],
Expand Down Expand Up @@ -88,7 +94,7 @@ defmodule TeslaApi.Vehicle do
case env do
%Tesla.Env{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))}
{:ok, transform.(res)}

%Tesla.Env{status: 401} = env ->
{:error, %Error{reason: :unauthorized, env: env}}
Expand Down
Loading