Skip to content

Commit

Permalink
feat: add handle_response function in Vehicle module to filter for on…
Browse files Browse the repository at this point in the history
…ly vehicles in products api endpoint
  • Loading branch information
JakobLichterfeld committed Jan 26, 2024
1 parent eac5e01 commit 8522cf7
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/tesla_api/vehicle.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ defmodule TeslaApi.Vehicle do
end

TeslaApi.get(endpoint_url <> "/api/1/products", opts: [access_token: auth.token])
|> handle_response(transform: &result/1)
|> 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

0 comments on commit 8522cf7

Please sign in to comment.