From fd9d9ed9b14f34fb9d7863f5c03e4942b16a7adc Mon Sep 17 00:00:00 2001 From: Hubert Chathi Date: Tue, 21 Apr 2020 02:50:49 -0400 Subject: fix login endpoint add logout endpoint, and add functions for logging in/out --- lib/polyjuice/client/endpoint/post_login.ex | 30 +++++++------- lib/polyjuice/client/endpoint/post_logout.ex | 59 ++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 15 deletions(-) create mode 100644 lib/polyjuice/client/endpoint/post_logout.ex (limited to 'lib/polyjuice/client') 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 +# +# 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 -- cgit v1.2.3