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

Implements recently viewed records api #67

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
17 changes: 17 additions & 0 deletions lib/ex_force.ex
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,23 @@ defmodule ExForce do
)
end

@doc """
Get recently viewed items
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the same convention with other functions

  • End the first sentence with period
  • Add link to the API doc

"""
@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}"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Use query option like other functions - instead of baking the query params into url option.
  • Set the default limit (200) per doc

}) do
{:ok, %Response{status: 200, body: []}} -> {:ok, []}
{:ok, %Response{status: 200, body: body}} -> {:ok, SObject.build(body)}
Comment on lines +389 to +390
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This introduces SObject.build/1 to take a list of sobject, not a sobject - which adds new responsibility to the public function, more than the function name implies.

We can just use Enum.map/2 here.

      {:ok, %Response{status: 200, body: body}} -> {:ok, Enum.map(body, &SObject.build/1) }

{: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}}
Expand Down
6 changes: 6 additions & 0 deletions lib/ex_force/sobject.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beside it's necessary addition...

  • This breaks the typespec (@spec build(map) :: t) as now it returns the list.
  • This adds get_recently_viewed_items specific handling to this function; such handling should happen there, not here.

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)}
Expand Down
78 changes: 78 additions & 0 deletions test/ex_force_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1202,5 +1202,83 @@ 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)

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

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