diff options
author | Hubert Chathi <hubert@uhoreg.ca> | 2020-05-09 11:03:08 -0400 |
---|---|---|
committer | Hubert Chathi <hubert@uhoreg.ca> | 2020-05-09 11:03:08 -0400 |
commit | 654920f4419edef75757e14a9f9aa6c6a0f3ba41 (patch) | |
tree | 1f89c2bd726f0d0e50b7c2ca936e94884a9d2875 /lib | |
parent | release 0.2.2 (diff) |
parse errors and reduce code duplication
Diffstat (limited to 'lib')
-rw-r--r-- | lib/polyjuice/client/endpoint.ex | 34 | ||||
-rw-r--r-- | lib/polyjuice/client/endpoint/get_rooms_messages.ex | 10 | ||||
-rw-r--r-- | lib/polyjuice/client/endpoint/get_sync.ex | 10 | ||||
-rw-r--r-- | lib/polyjuice/client/endpoint/post_join.ex | 14 | ||||
-rw-r--r-- | lib/polyjuice/client/endpoint/post_login.ex | 10 | ||||
-rw-r--r-- | lib/polyjuice/client/endpoint/post_logout.ex | 14 | ||||
-rw-r--r-- | lib/polyjuice/client/endpoint/post_rooms_receipt.ex | 14 | ||||
-rw-r--r-- | lib/polyjuice/client/endpoint/post_user_filter.ex | 14 | ||||
-rw-r--r-- | lib/polyjuice/client/endpoint/put_rooms_send.ex | 14 | ||||
-rw-r--r-- | lib/polyjuice/client/endpoint/put_rooms_state.ex | 14 |
10 files changed, 82 insertions, 66 deletions
diff --git a/lib/polyjuice/client/endpoint.ex b/lib/polyjuice/client/endpoint.ex index 1b6b7b5..f6c8247 100644 --- a/lib/polyjuice/client/endpoint.ex +++ b/lib/polyjuice/client/endpoint.ex @@ -68,4 +68,38 @@ defmodule Polyjuice.Client.Endpoint do ) :: any def transform_http_result(endpoint_args, status_code, headers, body) end + + defprotocol BodyParser do + @fallback_to_any true + @spec parse(endpoint_args :: __MODULE__.t(), body :: any) :: {:ok, any} | any + def parse(endpoint_args, body) + end + + defimpl BodyParser, for: Any do + def parse(_endpoint_args, body), do: {:ok, body} + end + + @spec parse_response( + endpoint_args :: map, + status_code :: integer(), + headers :: [{String.t(), String.t()}, ...], + body :: String.t() + ) :: {:ok, any} | any + def parse_response(%{} = endpoint_args, status_code, headers, body) + when is_integer(status_code) and is_list(headers) and is_binary(body) do + # FIXME: check if content type is "application/json" + with {:ok, json} <- Poison.decode(body) do + case status_code do + 200 -> + Polyjuice.Client.Endpoint.BodyParser.parse(endpoint_args, json) + + _ -> + {:error, status_code, json} + end + else + _ -> + {:error, if(status_code == 200, do: 500, else: status_code), + %{"errcode" => "CA_UHOREG_POLYJUICE_BAD_RESPONSE", "body" => body}} + end + end end diff --git a/lib/polyjuice/client/endpoint/get_rooms_messages.ex b/lib/polyjuice/client/endpoint/get_rooms_messages.ex index bceea3f..3a5d2df 100644 --- a/lib/polyjuice/client/endpoint/get_rooms_messages.ex +++ b/lib/polyjuice/client/endpoint/get_rooms_messages.ex @@ -78,14 +78,8 @@ defmodule Polyjuice.Client.Endpoint.GetRoomsMessages do } end - def transform_http_result(_req, status_code, _resp_headers, body) do - case status_code do - 200 -> - {:ok, Poison.decode!(body)} - - _ -> - {:error, status_code, body} - end + def transform_http_result(req, status_code, resp_headers, body) do + Polyjuice.Client.Endpoint.parse_response(req, status_code, resp_headers, body) end end end diff --git a/lib/polyjuice/client/endpoint/get_sync.ex b/lib/polyjuice/client/endpoint/get_sync.ex index c30e680..511fb21 100644 --- a/lib/polyjuice/client/endpoint/get_sync.ex +++ b/lib/polyjuice/client/endpoint/get_sync.ex @@ -84,14 +84,8 @@ defmodule Polyjuice.Client.Endpoint.GetSync do } end - def transform_http_result(_req, status_code, _resp_headers, body) do - case status_code do - 200 -> - {:ok, Poison.decode!(body)} - - _ -> - {:error, status_code, body} - end + def transform_http_result(req, status_code, resp_headers, body) do + Polyjuice.Client.Endpoint.parse_response(req, status_code, resp_headers, body) end end end diff --git a/lib/polyjuice/client/endpoint/post_join.ex b/lib/polyjuice/client/endpoint/post_join.ex index bdc8c0a..6689f59 100644 --- a/lib/polyjuice/client/endpoint/post_join.ex +++ b/lib/polyjuice/client/endpoint/post_join.ex @@ -76,14 +76,14 @@ defmodule Polyjuice.Client.Endpoint.PostJoin do } end - def transform_http_result(_req, status_code, _resp_headers, body) do - case status_code do - 200 -> - {:ok, body |> Poison.decode!() |> Map.get("room_id")} + def transform_http_result(req, status_code, resp_headers, body) do + Polyjuice.Client.Endpoint.parse_response(req, status_code, resp_headers, body) + end + end - _ -> - {:error, status_code, body} - end + defimpl Polyjuice.Client.Endpoint.BodyParser do + def parse(_req, parsed) do + {:ok, Map.get(parsed, "room_id")} end end end diff --git a/lib/polyjuice/client/endpoint/post_login.ex b/lib/polyjuice/client/endpoint/post_login.ex index e374b41..23fa5c3 100644 --- a/lib/polyjuice/client/endpoint/post_login.ex +++ b/lib/polyjuice/client/endpoint/post_login.ex @@ -84,14 +84,8 @@ defmodule Polyjuice.Client.Endpoint.PostLogin do } end - def transform_http_result(_req, status_code, _resp_headers, body) do - case status_code do - 200 -> - {:ok, Poison.decode!(body)} - - _ -> - {:error, status_code, body} - end + def transform_http_result(req, status_code, resp_headers, body) do + Polyjuice.Client.Endpoint.parse_response(req, status_code, resp_headers, body) end end end diff --git a/lib/polyjuice/client/endpoint/post_logout.ex b/lib/polyjuice/client/endpoint/post_logout.ex index e29c28b..d0e4e1a 100644 --- a/lib/polyjuice/client/endpoint/post_logout.ex +++ b/lib/polyjuice/client/endpoint/post_logout.ex @@ -45,14 +45,14 @@ defmodule Polyjuice.Client.Endpoint.PostLogout do } end - def transform_http_result(_req, status_code, _resp_headers, body) do - case status_code do - 200 -> - {:ok} + def transform_http_result(req, status_code, resp_headers, body) do + Polyjuice.Client.Endpoint.parse_response(req, status_code, resp_headers, body) + end + end - _ -> - {:error, status_code, body} - end + defimpl Polyjuice.Client.Endpoint.BodyParser do + def parse(_req, _body) do + {:ok} end end end diff --git a/lib/polyjuice/client/endpoint/post_rooms_receipt.ex b/lib/polyjuice/client/endpoint/post_rooms_receipt.ex index 45cff8e..5b92803 100644 --- a/lib/polyjuice/client/endpoint/post_rooms_receipt.ex +++ b/lib/polyjuice/client/endpoint/post_rooms_receipt.ex @@ -63,14 +63,14 @@ defmodule Polyjuice.Client.Endpoint.PostRoomsReceipt do } end - def transform_http_result(_req, status_code, _resp_headers, body) do - case status_code do - 200 -> - {:ok} + def transform_http_result(req, status_code, resp_headers, body) do + Polyjuice.Client.Endpoint.parse_response(req, status_code, resp_headers, body) + end + end - _ -> - {:error, status_code, body} - end + defimpl Polyjuice.Client.Endpoint.BodyParser do + def parse(_req, _body) do + {:ok} end end end diff --git a/lib/polyjuice/client/endpoint/post_user_filter.ex b/lib/polyjuice/client/endpoint/post_user_filter.ex index 39b0f4a..cf57437 100644 --- a/lib/polyjuice/client/endpoint/post_user_filter.ex +++ b/lib/polyjuice/client/endpoint/post_user_filter.ex @@ -57,14 +57,14 @@ defmodule Polyjuice.Client.Endpoint.PostUserFilter do } end - def transform_http_result(_req, status_code, _resp_headers, body) do - case status_code do - 200 -> - {:ok, body |> Poison.decode!() |> Map.get("filter_id")} + def transform_http_result(req, status_code, resp_headers, body) do + Polyjuice.Client.Endpoint.parse_response(req, status_code, resp_headers, body) + end + end - _ -> - {:error, status_code, body} - end + defimpl Polyjuice.Client.Endpoint.BodyParser do + def parse(_req, parsed) do + {:ok, Map.get(parsed, "filter_id")} end end end diff --git a/lib/polyjuice/client/endpoint/put_rooms_send.ex b/lib/polyjuice/client/endpoint/put_rooms_send.ex index 717624d..549bc2b 100644 --- a/lib/polyjuice/client/endpoint/put_rooms_send.ex +++ b/lib/polyjuice/client/endpoint/put_rooms_send.ex @@ -65,14 +65,14 @@ defmodule Polyjuice.Client.Endpoint.PutRoomsSend do } end - def transform_http_result(_req, status_code, _resp_headers, body) do - case status_code do - 200 -> - {:ok, body |> Poison.decode!() |> Map.get("event_id")} + def transform_http_result(req, status_code, resp_headers, body) do + Polyjuice.Client.Endpoint.parse_response(req, status_code, resp_headers, body) + end + end - _ -> - {:error, status_code, body} - end + defimpl Polyjuice.Client.Endpoint.BodyParser do + def parse(_req, parsed) do + {:ok, Map.get(parsed, "event_id")} end end end diff --git a/lib/polyjuice/client/endpoint/put_rooms_state.ex b/lib/polyjuice/client/endpoint/put_rooms_state.ex index 98481ed..51e9d64 100644 --- a/lib/polyjuice/client/endpoint/put_rooms_state.ex +++ b/lib/polyjuice/client/endpoint/put_rooms_state.ex @@ -65,14 +65,14 @@ defmodule Polyjuice.Client.Endpoint.PutRoomsState do } end - def transform_http_result(_req, status_code, _resp_headers, body) do - case status_code do - 200 -> - {:ok, body |> Poison.decode!() |> Map.get("event_id")} + def transform_http_result(req, status_code, resp_headers, body) do + Polyjuice.Client.Endpoint.parse_response(req, status_code, resp_headers, body) + end + end - _ -> - {:error, status_code, body} - end + defimpl Polyjuice.Client.Endpoint.BodyParser do + def parse(_req, parsed) do + {:ok, Map.get(parsed, "event_id")} end end end |