diff options
author | Hubert Chathi <hubert@uhoreg.ca> | 2020-10-22 22:08:46 -0400 |
---|---|---|
committer | Hubert Chathi <hubert@uhoreg.ca> | 2020-10-22 22:08:46 -0400 |
commit | 3f94502e2d3aa544f4e32446783aa47b59d28b14 (patch) | |
tree | e3d7e501a6443cb1531f6d10c4d4d7a6e0bab2cd /lib/polyjuice/client/endpoint.ex | |
parent | add a method to create delete requests (diff) |
allow compressed server responses
Diffstat (limited to 'lib/polyjuice/client/endpoint.ex')
-rw-r--r-- | lib/polyjuice/client/endpoint.ex | 32 |
1 files changed, 29 insertions, 3 deletions
diff --git a/lib/polyjuice/client/endpoint.ex b/lib/polyjuice/client/endpoint.ex index 7869ec6..e22f758 100644 --- a/lib/polyjuice/client/endpoint.ex +++ b/lib/polyjuice/client/endpoint.ex @@ -54,11 +54,17 @@ defmodule Polyjuice.Client.Endpoint do def prefix_media_r0, do: "_matrix/media/r0" @doc "Headers for endpoints that accept JSON." - def accept_json, do: [{"Accept", "application/json"}] + def accept_json, + do: [ + {"Accept", "application/json"}, + {"Accept-Encoding", "gzip, deflate"} + ] + @doc "Headers for endpoints that send and accept JSON." def send_json, do: [ {"Accept", "application/json"}, + {"Accept-Encoding", "gzip, deflate"}, {"Content-Type", "application/json"} ] @@ -168,9 +174,10 @@ defmodule Polyjuice.Client.Endpoint do ) :: any def parse_response(%{} = endpoint_args, status_code, headers, body) when is_integer(status_code) and is_list(headers) and is_binary(body) do + {:ok, decoded_body} = content_decode(body, headers) # make sure it's JSON content with "application/json" <- get_header(headers, "content-type"), - {:ok, json} <- Jason.decode(body) do + {:ok, json} <- Jason.decode(decoded_body) do case status_code do 200 -> Polyjuice.Client.Endpoint.BodyParser.parse(endpoint_args, json) @@ -181,7 +188,7 @@ defmodule Polyjuice.Client.Endpoint do else _ -> {:error, if(status_code == 200, do: 500, else: status_code), - %{"errcode" => "CA_UHOREG_POLYJUICE_BAD_RESPONSE", "body" => body}} + %{"errcode" => "CA_UHOREG_POLYJUICE_BAD_RESPONSE", "body" => decoded_body}} end end @@ -202,4 +209,23 @@ defmodule Polyjuice.Client.Endpoint do value end + + def content_decode(encoded, headers) do + case get_header(headers, "content-encoding", "identity") do + "identity" -> + {:ok, encoded} + + "gzip" -> + {:ok, :zlib.gunzip(encoded)} + + "deflate" -> + {:ok, :zlib.uncompress(encoded)} + + "x-gzip" -> + {:ok, :zlib.gunzip(encoded)} + + _ -> + {:error, :unknown_method} + end + end end |