ruma_client_api/session/
login_fallback.rs

1//! `GET /_matrix/static/client/login/` ([spec])
2//!
3//! Get login fallback web page.
4//!
5//! [spec]: https://spec.matrix.org/latest/client-server-api/#login-fallback
6
7use ruma_common::{
8    api::{auth_scheme::NoAuthentication, request},
9    metadata, OwnedDeviceId,
10};
11
12metadata! {
13    method: GET,
14    rate_limited: false,
15    authentication: NoAuthentication,
16    path: "/_matrix/static/client/login/",
17}
18
19/// Request type for the `login_fallback` endpoint.
20#[request(error = crate::Error)]
21#[derive(Default)]
22pub struct Request {
23    /// ID of the client device.
24    #[ruma_api(query)]
25    #[serde(skip_serializing_if = "Option::is_none")]
26    pub device_id: Option<OwnedDeviceId>,
27
28    /// A display name to assign to the newly-created device.
29    ///
30    /// Ignored if `device_id` corresponds to a known device.
31    #[ruma_api(query)]
32    #[serde(skip_serializing_if = "Option::is_none")]
33    pub initial_device_display_name: Option<String>,
34}
35
36impl Request {
37    /// Creates a new `Request` with the given auth type and session ID.
38    pub fn new(
39        device_id: Option<OwnedDeviceId>,
40        initial_device_display_name: Option<String>,
41    ) -> Self {
42        Self { device_id, initial_device_display_name }
43    }
44}
45
46/// Response type for the `login_fallback` endpoint.
47#[derive(Debug, Clone)]
48#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
49pub struct Response {
50    /// HTML to return to client.
51    pub body: Vec<u8>,
52}
53
54impl Response {
55    /// Creates a new `Response` with the given HTML body.
56    pub fn new(body: Vec<u8>) -> Self {
57        Self { body }
58    }
59}
60
61#[cfg(feature = "server")]
62impl ruma_common::api::OutgoingResponse for Response {
63    fn try_into_http_response<T: Default + bytes::BufMut>(
64        self,
65    ) -> Result<http::Response<T>, ruma_common::api::error::IntoHttpError> {
66        Ok(http::Response::builder()
67            .status(http::StatusCode::OK)
68            .header(http::header::CONTENT_TYPE, "text/html")
69            .body(ruma_common::serde::slice_to_buf(&self.body))?)
70    }
71}
72
73#[cfg(feature = "client")]
74impl ruma_common::api::IncomingResponse for Response {
75    type EndpointError = crate::Error;
76
77    fn try_from_http_response<T: AsRef<[u8]>>(
78        response: http::Response<T>,
79    ) -> Result<Self, ruma_common::api::error::FromHttpResponseError<Self::EndpointError>> {
80        use ruma_common::api::{error::FromHttpResponseError, EndpointError};
81
82        if response.status().as_u16() >= 400 {
83            return Err(FromHttpResponseError::Server(Self::EndpointError::from_http_response(
84                response,
85            )));
86        }
87
88        let body = response.into_body().as_ref().to_owned();
89        Ok(Self { body })
90    }
91}