From 15cd3b70f862bc5e4111c522af2e13f86dac1941 Mon Sep 17 00:00:00 2001 From: othonalberto Date: Fri, 18 Feb 2022 18:27:36 -0300 Subject: [PATCH 1/4] implements recent items api --- lib/ex_force.ex | 15 +++++++++++++ lib/ex_force/sobject.ex | 6 ++++++ test/ex_force_test.exs | 47 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) diff --git a/lib/ex_force.ex b/lib/ex_force.ex index e5752c1..688bd3d 100644 --- a/lib/ex_force.ex +++ b/lib/ex_force.ex @@ -376,6 +376,21 @@ defmodule ExForce do ) end + @doc """ + Get recently viewed items + """ + @spec get_recently_viewed_items(client, limit :: integer) :: Enumerable.t() + def get_recently_viewed_items(client, limit) do + case Client.request(client, %Request{ + method: :get, + url: "recent/?limit=#{limit}" + }) do + {:ok, %Response{status: 200, body: body}} -> {:ok, SObject.build(body)} + {:ok, %Response{body: body}} -> {:error, body} + {:error, _} = other -> other + end + end + defp stream_next({client, :halt}), do: {:halt, client} defp stream_next({client, {:error, _} = error_tuple}), do: {[error_tuple], {client, :halt}} diff --git a/lib/ex_force/sobject.ex b/lib/ex_force/sobject.ex index e1681b9..e6fc833 100644 --- a/lib/ex_force/sobject.ex +++ b/lib/ex_force/sobject.ex @@ -13,6 +13,12 @@ defmodule ExForce.SObject do @spec build(map) :: t def build(%{"attributes" => %{}} = raw), do: do_build(raw) + def build([%{"attributes" => %{"url" => _, "type" => _}, "Id" => _, "Name" => _} | _] = raw) do + Enum.map(raw, fn val -> + do_build(val) + end) + end + defp do_build(%{"attributes" => %{"type" => type, "url" => url}} = val) do id = url |> String.split("/") |> List.last() %__MODULE__{type: type, id: id, data: do_build_data(val)} diff --git a/test/ex_force_test.exs b/test/ex_force_test.exs index ab8cd95..6f005d4 100644 --- a/test/ex_force_test.exs +++ b/test/ex_force_test.exs @@ -1202,5 +1202,52 @@ defmodule ExForceTest do ] end + test "get_recently_viewed_items/2", %{bypass: bypass, client: client} do + Bypass.expect_once(bypass, "GET", "/services/data/v53.0/recent/", fn conn -> + %{"limit" => "2"} = URI.decode_query(conn.query_string) + + conn + |> Conn.put_resp_content_type("application/json") + |> Conn.resp(200, """ + [{ + "attributes" : + { + "type" : "Account", + "url" : "/services/data/v53.0/sobjects/Account/a06U000000CelH0IAJ" + }, + "Id" : "a06U000000CelH0IAJ", + "Name" : "Acme" + }, + { + "attributes" : + { + "type" : "Opportunity", + "url" : "/services/data/v53.0/sobjects/Opportunity/a06U000000CelGvIAJ" + }, + "Id" : "a06U000000CelGvIAJ", + "Name" : "Acme - 600 Widgets" + }] + """) + end) + + assert ExForce.get_recently_viewed_items(client, 2) == + {:ok, + [ + %SObject{ + id: "a06U000000CelH0IAJ", + type: "Account", + data: %{ + "Id" => "a06U000000CelH0IAJ", + "Name" => "Acme" + } + }, + %SObject{ + id: "a06U000000CelGvIAJ", + type: "Opportunity", + data: %{"Id" => "a06U000000CelGvIAJ", "Name" => "Acme - 600 Widgets"} + } + ]} + end + defp get(client, url), do: Client.request(client, %Request{url: url, method: :get}) end From 9087666dc3ca4ec9cf95f1e8d7980d0b72709e65 Mon Sep 17 00:00:00 2001 From: othonalberto Date: Mon, 21 Feb 2022 08:31:08 -0300 Subject: [PATCH 2/4] add failure test for get_recently_viewed_items --- test/ex_force_test.exs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/ex_force_test.exs b/test/ex_force_test.exs index 6f005d4..9298420 100644 --- a/test/ex_force_test.exs +++ b/test/ex_force_test.exs @@ -1249,5 +1249,21 @@ defmodule ExForceTest do ]} end + test "get_recently_viewed_items/2 - failure", %{bypass: bypass, client: client} do + Bypass.expect_once(bypass, "GET", "/services/data/v53.0/recent/", fn conn -> + %{"limit" => "2"} = URI.decode_query(conn.query_string) + + conn + |> Conn.put_resp_content_type("application/json") + |> Conn.resp(500, """ + [{ + }] + """) + end) + + assert ExForce.get_recently_viewed_items(client, 2) == + {:error, [%{}]} + end + defp get(client, url), do: Client.request(client, %Request{url: url, method: :get}) end From adafc9f18e892a5787d548c8e6799366d4c52c80 Mon Sep 17 00:00:00 2001 From: othonalberto Date: Mon, 21 Feb 2022 08:36:04 -0300 Subject: [PATCH 3/4] improve spec --- lib/ex_force.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ex_force.ex b/lib/ex_force.ex index 688bd3d..d2c05b4 100644 --- a/lib/ex_force.ex +++ b/lib/ex_force.ex @@ -379,7 +379,7 @@ defmodule ExForce do @doc """ Get recently viewed items """ - @spec get_recently_viewed_items(client, limit :: integer) :: Enumerable.t() + @spec get_recently_viewed_items(client, limit :: integer) :: {:ok, Enumerable.t()} | {:error, any()} def get_recently_viewed_items(client, limit) do case Client.request(client, %Request{ method: :get, From 8dbf18469692033143d7665b9133b054a1483a7a Mon Sep 17 00:00:00 2001 From: othonalberto Date: Mon, 21 Feb 2022 18:39:30 -0300 Subject: [PATCH 4/4] handle empty response - no recentyle viewed items --- lib/ex_force.ex | 4 +++- test/ex_force_test.exs | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/ex_force.ex b/lib/ex_force.ex index d2c05b4..4ad024e 100644 --- a/lib/ex_force.ex +++ b/lib/ex_force.ex @@ -379,12 +379,14 @@ defmodule ExForce do @doc """ Get recently viewed items """ - @spec get_recently_viewed_items(client, limit :: integer) :: {:ok, Enumerable.t()} | {:error, any()} + @spec get_recently_viewed_items(client, limit :: integer) :: + {:ok, Enumerable.t()} | {:error, any()} def get_recently_viewed_items(client, limit) do case Client.request(client, %Request{ method: :get, url: "recent/?limit=#{limit}" }) do + {:ok, %Response{status: 200, body: []}} -> {:ok, []} {:ok, %Response{status: 200, body: body}} -> {:ok, SObject.build(body)} {:ok, %Response{body: body}} -> {:error, body} {:error, _} = other -> other diff --git a/test/ex_force_test.exs b/test/ex_force_test.exs index 9298420..160047a 100644 --- a/test/ex_force_test.exs +++ b/test/ex_force_test.exs @@ -1202,6 +1202,21 @@ defmodule ExForceTest do ] end + test "get_recently_viewed_items/2 - no recently viewed items", %{bypass: bypass, client: client} do + Bypass.expect_once(bypass, "GET", "/services/data/v53.0/recent/", fn conn -> + %{"limit" => "2"} = URI.decode_query(conn.query_string) + + conn + |> Conn.put_resp_content_type("application/json") + |> Conn.resp(200, """ + [] + """) + end) + + assert ExForce.get_recently_viewed_items(client, 2) == + {:ok, []} + end + test "get_recently_viewed_items/2", %{bypass: bypass, client: client} do Bypass.expect_once(bypass, "GET", "/services/data/v53.0/recent/", fn conn -> %{"limit" => "2"} = URI.decode_query(conn.query_string)