From 16a7e9914fb01f4a471bac766b10e8b5be414c2f Mon Sep 17 00:00:00 2001 From: Phil Wade Date: Tue, 16 Apr 2019 11:33:47 -0400 Subject: [PATCH 1/2] Change how list query parameters are constructed --- lib/gmail/base.ex | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/lib/gmail/base.ex b/lib/gmail/base.ex index ed30283..f0aeffe 100644 --- a/lib/gmail/base.ex +++ b/lib/gmail/base.ex @@ -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 From 63223075faae40233c1d70867f60b64df25d83f2 Mon Sep 17 00:00:00 2001 From: Phil Wade Date: Tue, 16 Apr 2019 14:25:32 -0400 Subject: [PATCH 2/2] Fix test, add test --- test/gmail/thread_test.exs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/test/gmail/thread_test.exs b/test/gmail/thread_test.exs index ed33624..947aacb 100644 --- a/test/gmail/thread_test.exs +++ b/test/gmail/thread_test.exs @@ -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) @@ -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,