diff options
Diffstat (limited to 'lib/polyjuice/client.ex')
-rw-r--r-- | lib/polyjuice/client.ex | 31 |
1 files changed, 26 insertions, 5 deletions
diff --git a/lib/polyjuice/client.ex b/lib/polyjuice/client.ex index dfe8775..0d3bc75 100644 --- a/lib/polyjuice/client.ex +++ b/lib/polyjuice/client.ex @@ -64,6 +64,8 @@ defmodule Polyjuice.Client do def prefix_r0, do: "_matrix/client/r0" @doc "The unstable client URL prefix" def prefix_unstable, do: "_matrix/client/unstable" + @doc "The r0 media URL prefix" + def prefix_media_r0, do: "_matrix/media/r0" defprotocol API do @moduledoc """ @@ -122,10 +124,11 @@ defmodule Polyjuice.Client do headers: headers, url: url, body: body, - auth_required: auth_required + auth_required: auth_required, + stream_response: stream_response } = Polyjuice.Client.Endpoint.Proto.http_spec(endpoint, base_url) - Logger.debug("calling #{url}") + Logger.debug("calling #{method} #{url}") if auth_required and access_token == nil do {:error, :auth_required} @@ -141,16 +144,21 @@ defmodule Polyjuice.Client do headers end, body, - [:with_body] + [] ) do - {:ok, status_code, resp_headers, body} -> + {:ok, status_code, resp_headers, client_ref} -> Logger.debug("status code #{status_code}") Polyjuice.Client.Endpoint.Proto.transform_http_result( endpoint, status_code, resp_headers, - body + if stream_response do + Polyjuice.Client.hackney_response_stream(client_ref) + else + {:ok, body} = :hackney.body(client_ref) + body + end ) err -> @@ -169,6 +177,19 @@ defmodule Polyjuice.Client do end end + @doc false + def hackney_response_stream(client_ref) do + Stream.unfold( + client_ref, + fn client_ref -> + case :hackney.stream_body(client_ref) do + {:ok, data} -> {data, client_ref} + _ -> nil + end + end + ) + end + @doc """ Synchronize messages from the server. |