Skip to content

Change how list query parameters are constructed #15

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

Open
wants to merge 2 commits into
base: master
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
40 changes: 26 additions & 14 deletions lib/gmail/base.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,39 @@ defmodule Gmail.Base do
if Enum.empty?(params) do
path
else
query =
{query, list_params} =
params
|> Map.keys
|> Enum.filter(fn key -> key in available_options end)
|> Enum.reduce(Map.new, fn key, query ->
string_key = Utils.camelize(key)
val = if is_list(params[key]) do
Enum.join(params[key], ",")
else
params[key]
end
Map.put(query, string_key, val)
end)
if Enum.empty?(query) do
|> Enum.reduce({ Map.new, Map.new}, fn {key, value}, {q, lps} ->
if key in available_options do
string_key = Utils.camelize(key)
if is_list(value) do
{q, Map.put(lps, string_key, value)}
else
{Map.put(q, string_key, value), lps}
end
else
{q, lps}
end
end)
if Enum.empty?(query) && Enum.empty?(list_params) do
path
else
path <> "?" <> URI.encode_query(query)
path <> "?" <> URI.encode_query(query) <> queryify_arrays(list_params)
end
end
end

@spec queryify_arrays(map) :: String.t
defp queryify_arrays(params) do
params
|> Map.keys
|> Enum.reduce("", fn key, query_string ->
string_key = "&" <> key <> "="
query_string <> string_key <> Enum.join(params[key], string_key)
end)
|> URI.encode
end

@spec handle_error({atom, map}) :: {atom, String.t} | {atom, map}
@spec handle_error({atom, String.t}) :: {atom, String.t} | {atom, map}
def handle_error(response) do
Expand Down
21 changes: 20 additions & 1 deletion test/gmail/thread_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ defmodule Gmail.ThreadTest do
} do
Bypass.expect bypass, fn conn ->
assert "/gmail/v1/users/#{user_id}/threads/#{thread_id}" == conn.request_path
assert URI.encode_query(%{"format" => "metadata", "metadataHeaders" => "header1,header1"}) == conn.query_string
assert URI.encode("format=metadata&metadataHeaders=header1&metadataHeaders=header1") == conn.query_string
assert "GET" == conn.method
assert {"authorization", "Bearer #{access_token}"} in conn.req_headers
{:ok, json} = Poison.encode(thread)
Expand All @@ -237,6 +237,25 @@ defmodule Gmail.ThreadTest do
assert expected_result == thread
end

test "gets threads, specifying multiple labels", %{
access_token: access_token,
expected_search_results: expected_search_results,
list_results: list_results,
bypass: bypass,
user_id: user_id
} do
Bypass.expect bypass, fn conn ->
assert "/gmail/v1/users/#{user_id}/threads" == conn.request_path
assert URI.encode("&labelIds=sample&labelIds=blah") == conn.query_string
assert "GET" == conn.method
assert {"authorization", "Bearer #{access_token}"} in conn.req_headers
{:ok, json} = Poison.encode(list_results)
Plug.Conn.resp(conn, 200, json)
end
{:ok, results, _} = Gmail.User.threads(user_id, %{label_ids: ["sample", "blah"]})
assert expected_search_results == results
end

test "reports :not_found for a thread that doesn't exist", %{
thread_not_found: thread_not_found,
thread_id: thread_id,
Expand Down