diff options
author | Hubert Chathi <hubert@uhoreg.ca> | 2020-04-21 02:50:49 -0400 |
---|---|---|
committer | Hubert Chathi <hubert@uhoreg.ca> | 2020-04-21 02:50:49 -0400 |
commit | fd9d9ed9b14f34fb9d7863f5c03e4942b16a7adc (patch) | |
tree | ad7da10f745fa7e596fcd7237bbc56b22522faa5 /lib/polyjuice/client | |
parent | allow listeners to be functions instead of processes (diff) |
fix login endpoint add logout endpoint, and add functions for logging in/out
Diffstat (limited to 'lib/polyjuice/client')
-rw-r--r-- | lib/polyjuice/client/endpoint/post_login.ex | 30 | ||||
-rw-r--r-- | lib/polyjuice/client/endpoint/post_logout.ex | 59 |
2 files changed, 74 insertions, 15 deletions
diff --git a/lib/polyjuice/client/endpoint/post_login.ex b/lib/polyjuice/client/endpoint/post_login.ex index 6d516c2..bb068e2 100644 --- a/lib/polyjuice/client/endpoint/post_login.ex +++ b/lib/polyjuice/client/endpoint/post_login.ex @@ -52,21 +52,20 @@ defmodule Polyjuice.Client.Endpoint.PostLogin do base_url ) when is_binary(base_url) do - {:ok, body} = - Poison.encode( - Map.new( - Enum.concat([ - [{"type", type}, {"identifier", identifier}], - if(password != nil, do: [{"password", password}], else: []), - if(token != nil, do: [{"token", token}], else: []), - if(device_id != nil, do: [{"device_id", device_id}], else: []), - if(initial_device_display_name != nil, - do: [{"initial_device_display_name", initial_device_display_name}], - else: [] - ) - ]) + body = + [ + [{"type", type}, {"identifier", identifier}], + if(password != nil, do: [{"password", password}], else: []), + if(token != nil, do: [{"token", token}], else: []), + if(device_id != nil, do: [{"device_id", device_id}], else: []), + if(initial_device_display_name != nil, + do: [{"initial_device_display_name", initial_device_display_name}], + else: [] ) - ) + ] + |> Enum.concat() + |> Map.new() + |> Poison.encode!() %Polyjuice.Client.Endpoint.HttpSpec{ method: :post, @@ -81,7 +80,8 @@ defmodule Polyjuice.Client.Endpoint.PostLogin do ) |> to_string(), body: body, - transform: &Polyjuice.Client.Endpoint.PostLogin.transform/3 + transform: &Polyjuice.Client.Endpoint.PostLogin.transform/3, + auth_required: false } end end diff --git a/lib/polyjuice/client/endpoint/post_logout.ex b/lib/polyjuice/client/endpoint/post_logout.ex new file mode 100644 index 0000000..d23f725 --- /dev/null +++ b/lib/polyjuice/client/endpoint/post_logout.ex @@ -0,0 +1,59 @@ +# Copyright 2020 Hubert Chathi <hubert@uhoreg.ca> +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +defmodule Polyjuice.Client.Endpoint.PostLogout do + @moduledoc """ + Logs out a session + + https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-logout + """ + + @type t :: %__MODULE__{} + + defstruct [] + + defimpl Polyjuice.Client.Endpoint.Proto do + def http_spec( + %Polyjuice.Client.Endpoint.PostLogout{}, + base_url + ) + when is_binary(base_url) do + %Polyjuice.Client.Endpoint.HttpSpec{ + method: :post, + headers: [ + {"Accept", "application/json"} + ], + url: + URI.merge( + base_url, + "#{Polyjuice.Client.prefix_r0()}/logout" + ) + |> to_string(), + body: "", + transform: &Polyjuice.Client.Endpoint.PostLogout.transform/3 + } + end + end + + @doc false + def transform(status_code, _resp_headers, body) do + case status_code do + 200 -> + :ok + + _ -> + {:error, status_code, body} + end + end +end |