use ruma_api::ruma_api;
use ruma_events::{AnyStateEventContent, EventType};
use ruma_identifiers::RoomId;
use ruma_serde::{Outgoing, Raw};
ruma_api! {
metadata: {
description: "Get state events associated with a given key.",
method: GET,
name: "get_state_events_for_key",
path: "/_matrix/client/r0/rooms/:room_id/state/:event_type/:state_key",
rate_limited: false,
authentication: AccessToken,
}
response: {
#[ruma_api(body)]
pub content: Raw<AnyStateEventContent>,
}
error: crate::Error
}
#[derive(Clone, Debug, Outgoing)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
#[incoming_derive(!Deserialize)]
pub struct Request<'a> {
pub room_id: &'a RoomId,
pub event_type: EventType,
pub state_key: &'a str,
}
impl<'a> Request<'a> {
pub fn new(room_id: &'a RoomId, event_type: EventType, state_key: &'a str) -> Self {
Self { room_id, event_type, state_key }
}
}
impl Response {
pub fn new(content: Raw<AnyStateEventContent>) -> Self {
Self { content }
}
}
#[cfg(feature = "client")]
impl<'a> ruma_api::OutgoingRequest for Request<'a> {
type EndpointError = crate::Error;
type IncomingResponse = <Response as ruma_serde::Outgoing>::Incoming;
const METADATA: ruma_api::Metadata = METADATA;
fn try_into_http_request<T: Default + bytes::BufMut>(
self,
base_url: &str,
access_token: ruma_api::SendAccessToken<'_>,
) -> Result<http::Request<T>, ruma_api::error::IntoHttpError> {
use std::borrow::Cow;
use http::header;
use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC};
let mut url = format!(
"{}/_matrix/client/r0/rooms/{}/state/{}",
base_url.strip_suffix('/').unwrap_or(base_url),
utf8_percent_encode(&self.room_id.to_string(), NON_ALPHANUMERIC),
utf8_percent_encode(&self.event_type.to_string(), NON_ALPHANUMERIC)
);
if !self.state_key.is_empty() {
url.push('/');
url.push_str(&Cow::from(utf8_percent_encode(self.state_key, NON_ALPHANUMERIC)));
}
http::Request::builder()
.method(http::Method::GET)
.uri(url)
.header(header::CONTENT_TYPE, "application/json")
.header(
header::AUTHORIZATION,
format!(
"Bearer {}",
access_token
.get_required_for_endpoint()
.ok_or(ruma_api::error::IntoHttpError::NeedsAuthentication)?,
),
)
.body(T::default())
.map_err(Into::into)
}
}
#[cfg(feature = "server")]
impl ruma_api::IncomingRequest for IncomingRequest {
type EndpointError = crate::Error;
type OutgoingResponse = Response;
const METADATA: ruma_api::Metadata = METADATA;
fn try_from_http_request<T: AsRef<[u8]>>(
request: http::Request<T>,
) -> Result<Self, ruma_api::error::FromHttpRequestError> {
use std::convert::TryFrom;
let path_segments: Vec<&str> = request.uri().path()[1..].split('/').collect();
let room_id = {
let decoded =
percent_encoding::percent_decode(path_segments[4].as_bytes()).decode_utf8()?;
RoomId::try_from(&*decoded)?
};
let event_type = {
let decoded =
percent_encoding::percent_decode(path_segments[6].as_bytes()).decode_utf8()?;
EventType::try_from(&*decoded)?
};
let state_key = match path_segments.get(7) {
Some(segment) => {
let decoded = percent_encoding::percent_decode(segment.as_bytes()).decode_utf8()?;
String::try_from(&*decoded)?
}
None => "".into(),
};
Ok(Self { room_id, event_type, state_key })
}
}