ruma_common/api.rs
1//! Core types used to define the requests and responses for each endpoint in the various
2//! [Matrix API specifications][apis].
3//!
4//! When implementing a new Matrix API, each endpoint has a request type which implements
5//! [`IncomingRequest`] and [`OutgoingRequest`], and a response type connected via an associated
6//! type.
7//!
8//! An implementation of [`IncomingRequest`] or [`OutgoingRequest`] contains all the information
9//! about the HTTP method, the path and input parameters for requests, and the structure of a
10//! successful response. Such types can then be used by client code to make requests, and by server
11//! code to fulfill those requests.
12//!
13//! [apis]: https://spec.matrix.org/v1.19/#matrix-apis
14
15use std::{convert::TryInto as _, error::Error as StdError};
16
17use bytes::BufMut;
18pub use ruma_macros::OutgoingBodyJson;
19/// Generates [`OutgoingRequest`] and [`IncomingRequest`] implementations.
20///
21/// The `OutgoingRequest` impl is feature-gated behind `cfg(feature = "client")`.
22/// The `IncomingRequest` impl is feature-gated behind `cfg(feature = "server")`.
23///
24/// The generated code expects the `Request` type to implement [`Metadata`], alongside a
25/// `Response` type that implements [`OutgoingResponse`] (for `cfg(feature = "server")`) and /
26/// or [`IncomingResponse`] (for `cfg(feature = "client")`).
27///
28/// The `Content-Type` header of the `OutgoingRequest` is unset for endpoints using the `GET`
29/// method, and defaults to `application/json` for all other methods, except if the `raw_body`
30/// attribute is set on a field, in which case it defaults to `application/octet-stream`.
31///
32/// By default, the type this macro is used on gets a `#[non_exhaustive]` attribute. This
33/// behavior can be controlled by setting the `ruma_unstable_exhaustive_types` compile-time
34/// `cfg` setting as `--cfg=ruma_unstable_exhaustive_types` using `RUSTFLAGS` or
35/// `.cargo/config.toml` (under `[build]` -> `rustflags = ["..."]`). When that setting is
36/// activated, the attribute is not applied so the type is exhaustive.
37///
38/// ## Container Attributes
39///
40/// * `#[request(error = ERROR_TYPE)]`: Override the `EndpointError` associated type of the
41/// `OutgoingRequest` and `IncomingRequest` implementations. The default error type is
42/// [`Error`](error::Error).
43///
44/// ## Field Attributes
45///
46/// To declare which part of the request a field belongs to:
47///
48/// * `#[ruma_api(header = HEADER_NAME)]`: Fields with this attribute will be treated as HTTP
49/// headers on the request. The value must implement `ToString` and `FromStr`. Generally this
50/// is a `String`. The attribute value shown above as `HEADER_NAME` must be a `const`
51/// expression of the type `http::header::HeaderName`, like one of the constants from
52/// `http::header`, e.g. `CONTENT_TYPE`. During deserialization of the request, if the field
53/// is an `Option` and parsing the header fails, the error will be ignored and the value will
54/// be `None`.
55/// * `#[ruma_api(path)]`: Fields with this attribute will be inserted into the matching path
56/// component of the request URL. If there are multiple of these fields, the order in which
57/// they are declared must match the order in which they occur in the request path.
58/// * `#[ruma_api(query)]`: Fields with this attribute will be inserting into the URL's query
59/// string.
60/// * `#[ruma_api(query_all)]`: Instead of individual query fields, one query_all field, of any
61/// type that can be (de)serialized by [serde_html_form], can be used for cases where
62/// multiple endpoints should share a query fields type, the query fields are better
63/// expressed as an `enum` rather than a `struct`, or the endpoint supports arbitrary query
64/// parameters.
65/// * No attribute: Fields without an attribute are part of the body. They can use `#[serde]`
66/// attributes to customize (de)serialization.
67/// * `#[ruma_api(body)]`: Use this if multiple endpoints should share a request body type, or
68/// the request body is better expressed as an `enum` rather than a `struct`. The value of
69/// the field will be used as the JSON body (rather than being a field in the request body
70/// object).
71/// * `#[ruma_api(raw_body)]`: Like `body` in that the field annotated with it represents the
72/// entire request body, but this attribute is for endpoints where the body can be anything,
73/// not just JSON. The field type must be `Vec<u8>`.
74///
75/// ## Examples
76///
77/// ```
78/// pub mod do_a_thing {
79/// use ruma_common::{OwnedRoomId, api::request};
80/// # use ruma_common::{api::{auth_scheme::NoAuthentication, response}, metadata};
81///
82/// // metadata! { ... };
83/// # metadata! {
84/// # method: POST,
85/// # rate_limited: false,
86/// # authentication: NoAuthentication,
87/// # history: {
88/// # unstable => "/_matrix/some/endpoint/{room_id}",
89/// # },
90/// # }
91///
92/// #[request]
93/// pub struct Request {
94/// #[ruma_api(path)]
95/// pub room_id: OwnedRoomId,
96///
97/// #[ruma_api(query)]
98/// pub bar: String,
99///
100/// #[serde(default)]
101/// pub foo: String,
102/// }
103///
104/// // #[response]
105/// // pub struct Response { ... }
106/// # #[response]
107/// # pub struct Response {}
108/// }
109///
110/// pub mod upload_file {
111/// use http::header::CONTENT_TYPE;
112/// use ruma_common::api::request;
113/// # use ruma_common::{api::{auth_scheme::NoAuthentication, response}, metadata};
114///
115/// // metadata! { ... };
116/// # metadata! {
117/// # method: POST,
118/// # rate_limited: false,
119/// # authentication: NoAuthentication,
120/// # history: {
121/// # unstable => "/_matrix/some/endpoint/{file_name}",
122/// # },
123/// # }
124///
125/// #[request]
126/// pub struct Request {
127/// #[ruma_api(path)]
128/// pub file_name: String,
129///
130/// #[ruma_api(header = CONTENT_TYPE)]
131/// pub content_type: String,
132///
133/// #[ruma_api(raw_body)]
134/// pub file: Vec<u8>,
135/// }
136///
137/// // #[response]
138/// // pub struct Response { ... }
139/// # #[response]
140/// # pub struct Response {}
141/// }
142/// ```
143///
144/// [serde_html_form]: https://crates.io/crates/serde_html_form
145pub use ruma_macros::request;
146/// Generates [`OutgoingResponse`] and [`IncomingResponse`] implementations.
147///
148/// The `OutgoingResponse` impl is feature-gated behind `cfg(feature = "server")`.
149/// The `IncomingResponse` impl is feature-gated behind `cfg(feature = "client")`.
150///
151/// The `Content-Type` header of the `OutgoingResponse` defaults to `application/json`, except
152/// if the `raw_body` attribute is set on a field, in which case it defaults to
153/// `application/octet-stream`.
154///
155/// By default, the type this macro is used on gets a `#[non_exhaustive]` attribute. This
156/// behavior can be controlled by setting the `ruma_unstable_exhaustive_types` compile-time
157/// `cfg` setting as `--cfg=ruma_unstable_exhaustive_types` using `RUSTFLAGS` or
158/// `.cargo/config.toml` (under `[build]` -> `rustflags = ["..."]`). When that setting is
159/// activated, the attribute is not applied so the type is exhaustive.
160///
161/// ## Container Attributes
162///
163/// * `#[response(error = ERROR_TYPE)]`: Override the `EndpointError` associated type of the
164/// `IncomingResponse` implementation. The default error type is [`Error`](error::Error).
165/// * `#[response(status = HTTP_STATUS)]`: Override the status code of `OutgoingResponse`.
166/// `HTTP_STATUS` must be a status code constant from [`http::StatusCode`], e.g.
167/// `IM_A_TEAPOT`. The default status code is [`200 OK`](http::StatusCode::OK);
168///
169/// ## Field Attributes
170///
171/// To declare which part of the response a field belongs to:
172///
173/// * `#[ruma_api(header = HEADER_NAME)]`: Fields with this attribute will be treated as HTTP
174/// headers on the response. `HEADER_NAME` must implement
175/// `TryInto<http::header::HeaderName>`, this is usually a constant from [`http::header`].
176/// The value of the field must implement `ToString` and `FromStr`, this is usually a
177/// `String`. During deserialization of the response, if the field is an `Option` and parsing
178/// the header fails, the error will be ignored and the value will be `None`.
179/// * No attribute: Fields without an attribute are part of the body. They can use `#[serde]`
180/// attributes to customize (de)serialization.
181/// * `#[ruma_api(body)]`: Use this if multiple endpoints should share a response body type, or
182/// the response body is better expressed as an `enum` rather than a `struct`. The value of
183/// the field will be used as the JSON body (rather than being a field in the response body
184/// object).
185/// * `#[ruma_api(raw_body)]`: Like `body` in that the field annotated with it represents the
186/// entire response body, but this attribute is for endpoints where the body can be anything,
187/// not just JSON. The field type must be `Vec<u8>`.
188///
189/// ## Examples
190///
191/// ```
192/// pub mod do_a_thing {
193/// use ruma_common::{OwnedRoomId, api::response};
194/// # use ruma_common::{api::{auth_scheme::NoAuthentication, request}, metadata};
195///
196/// // metadata! { ... };
197/// # metadata! {
198/// # method: POST,
199/// # rate_limited: false,
200/// # authentication: NoAuthentication,
201/// # history: {
202/// # unstable => "/_matrix/some/endpoint",
203/// # },
204/// # }
205///
206/// // #[request]
207/// // pub struct Request { ... }
208/// # #[request]
209/// # pub struct Request { }
210///
211/// #[response(status = IM_A_TEAPOT)]
212/// pub struct Response {
213/// #[serde(skip_serializing_if = "Option::is_none")]
214/// pub foo: Option<String>,
215/// }
216/// }
217///
218/// pub mod download_file {
219/// use http::header::CONTENT_TYPE;
220/// use ruma_common::api::response;
221/// # use ruma_common::{api::{auth_scheme::NoAuthentication, request}, metadata};
222///
223/// // metadata! { ... };
224/// # metadata! {
225/// # method: POST,
226/// # rate_limited: false,
227/// # authentication: NoAuthentication,
228/// # history: {
229/// # unstable => "/_matrix/some/endpoint",
230/// # },
231/// # }
232///
233/// // #[request]
234/// // pub struct Request { ... }
235/// # #[request]
236/// # pub struct Request { }
237///
238/// #[response]
239/// pub struct Response {
240/// #[ruma_api(header = CONTENT_TYPE)]
241/// pub content_type: String,
242///
243/// #[ruma_api(raw_body)]
244/// pub file: Vec<u8>,
245/// }
246/// }
247/// ```
248pub use ruma_macros::response;
249use serde::{Deserialize, Serialize};
250
251use self::error::{FromHttpRequestError, FromHttpResponseError, IntoHttpError};
252#[doc(inline)]
253pub use crate::metadata;
254use crate::{DeviceId, UserId};
255
256pub mod auth_scheme;
257mod body;
258pub mod error;
259mod metadata;
260pub mod path_builder;
261
262use self::error::DeserializationError;
263pub use self::{
264 body::{BytesBody, EmptyBody, OutgoingBody},
265 metadata::{FeatureFlag, MatrixVersion, Metadata, SupportedVersions},
266};
267
268/// A request type for a Matrix API endpoint, used for sending requests.
269pub trait OutgoingRequest: Metadata + Clone {
270 /// HTTP body type pre-serialization.
271 type Body: OutgoingBody;
272
273 /// A type capturing the expected error conditions the server can return.
274 type EndpointError: EndpointError;
275
276 /// Response type returned when the request is successful.
277 type IncomingResponse: IncomingResponse<EndpointError = Self::EndpointError>;
278
279 /// Tries to convert this request into an `http::Request`.
280 ///
281 /// The endpoints path will be appended to the given `base_url`, for example
282 /// `https://matrix.org`. Since all paths begin with a slash, it is not necessary for the
283 /// `base_url` to have a trailing slash. If it has one however, it will be ignored.
284 ///
285 /// ## Errors
286 ///
287 /// This method can return an error in the following cases:
288 ///
289 /// * On endpoints that have several versions for the path, when there are no supported versions
290 /// for the endpoint, i.e. when [`PathBuilder::make_endpoint_url()`] returns an error.
291 /// * If the request serialization fails, which should only happen in case of bugs in Ruma.
292 ///
293 /// [`AuthScheme::add_authentication()`]: auth_scheme::AuthScheme::add_authentication
294 /// [`PathBuilder::make_endpoint_url()`]: path_builder::PathBuilder::make_endpoint_url
295 fn try_into_http_request_inner(
296 self,
297 base_url: &str,
298 path_builder_input: <Self::PathBuilder as path_builder::PathBuilder>::Input<'_>,
299 ) -> Result<http::Request<Self::Body>, IntoHttpError>;
300}
301
302/// Convenience functionality on top of [`OutgoingRequest`].
303pub trait OutgoingRequestExt: OutgoingRequest {
304 /// Tries to convert this request into an `http::Request`.
305 ///
306 /// The endpoints path will be appended to the given `base_url`, for example
307 /// `https://matrix.org`. Since all paths begin with a slash, it is not necessary for the
308 /// `base_url` to have a trailing slash. If it has one however, it will be ignored.
309 ///
310 /// ## Errors
311 ///
312 /// This method can return an error in the following cases:
313 ///
314 /// * On endpoints that require authentication, when adequate information isn't provided through
315 /// `authentication_input`, i.e. when [`AuthScheme::add_authentication()`] returns an error.
316 /// * On endpoints that have several versions for the path, when there are no supported versions
317 /// for the endpoint, i.e. when [`PathBuilder::make_endpoint_url()`] returns an error.
318 /// * If the request serialization fails, which should only happen in case of bugs in Ruma.
319 ///
320 /// [`AuthScheme::add_authentication()`]: auth_scheme::AuthScheme::add_authentication
321 /// [`PathBuilder::make_endpoint_url()`]: path_builder::PathBuilder::make_endpoint_url
322 fn try_into_http_request<T: Default + BufMut + AsRef<[u8]>>(
323 self,
324 base_url: &str,
325 authentication_input: <Self::Authentication as auth_scheme::AuthScheme>::Input<'_>,
326 path_builder_input: <Self::PathBuilder as path_builder::PathBuilder>::Input<'_>,
327 ) -> Result<http::Request<T>, IntoHttpError> {
328 let (parts, body) =
329 self.try_into_http_request_inner(base_url, path_builder_input)?.into_parts();
330 let mut request =
331 http::Request::from_parts(parts, body.try_into_buf().map_err(Into::into)?);
332
333 <Self::Authentication as auth_scheme::AuthScheme>::add_authentication(
334 &mut request,
335 authentication_input,
336 )
337 .map_err(IntoHttpError::authentication)?;
338
339 Ok(request)
340 }
341}
342
343impl<T: OutgoingRequest> OutgoingRequestExt for T {}
344
345/// A response type for a Matrix API endpoint, used for receiving responses.
346pub trait IncomingResponse: Sized {
347 /// A type capturing the expected error conditions the server can return.
348 type EndpointError: EndpointError;
349
350 /// Tries to convert the given `http::Response` into this response type.
351 ///
352 /// Only called for successful responses (HTTP status code < 400).
353 fn try_from_http_response_inner(
354 response: http::Response<&[u8]>,
355 ) -> Result<Self, DeserializationError>;
356}
357
358/// Convenience functionality on top of [`IncomingResponse`].
359pub trait IncomingResponseExt: IncomingResponse {
360 /// Tries to convert the given `http::Response` into this response type.
361 fn try_from_http_response(
362 response: http::Response<&[u8]>,
363 ) -> Result<Self, FromHttpResponseError<Self::EndpointError>> {
364 if response.status().as_u16() >= 400 {
365 return Err(FromHttpResponseError::Server(Self::EndpointError::from_http_response(
366 response,
367 )));
368 }
369
370 Self::try_from_http_response_inner(response).map_err(Into::into)
371 }
372}
373
374impl<T: IncomingResponse> IncomingResponseExt for T {}
375
376/// An extension to [`OutgoingRequest`] which provides Appservice specific methods.
377///
378/// This is only implemented for implementors of [`AuthScheme`](auth_scheme::AuthScheme) that use a
379/// [`SendAccessToken`](auth_scheme::SendAccessToken), because application services should only use
380/// these methods with the Client-Server API.
381pub trait OutgoingRequestAppserviceExt: OutgoingRequest
382where
383 for<'a> Self::Authentication:
384 auth_scheme::AuthScheme<Input<'a> = auth_scheme::SendAccessToken<'a>>,
385{
386 /// Tries to convert this request into an `http::Request` and adds the given
387 /// [`AppserviceUserIdentity`] to it, if the identity is not empty.
388 fn try_into_http_request_with_identity<T: Default + BufMut + AsRef<[u8]>>(
389 self,
390 base_url: &str,
391 access_token: auth_scheme::SendAccessToken<'_>,
392 identity: AppserviceUserIdentity<'_>,
393 path_builder_input: <Self::PathBuilder as path_builder::PathBuilder>::Input<'_>,
394 ) -> Result<http::Request<T>, IntoHttpError> {
395 let mut http_request =
396 self.try_into_http_request(base_url, access_token, path_builder_input)?;
397
398 identity.maybe_add_to_uri(http_request.uri_mut())?;
399
400 Ok(http_request)
401 }
402}
403
404impl<T: OutgoingRequest> OutgoingRequestAppserviceExt for T where
405 for<'a> Self::Authentication:
406 auth_scheme::AuthScheme<Input<'a> = auth_scheme::SendAccessToken<'a>>
407{
408}
409
410/// A request type for a Matrix API endpoint, used for receiving requests.
411pub trait IncomingRequest: Metadata {
412 /// A type capturing the error conditions that can be returned in the response.
413 type EndpointError: EndpointError;
414
415 /// Response type to return when the request is successful.
416 type OutgoingResponse: OutgoingResponse;
417
418 /// Check whether the given HTTP method from an incoming request is compatible with the expected
419 /// [`METHOD`](Metadata::METHOD) of this endpoint.
420 fn check_request_method(method: &http::Method) -> Result<(), FromHttpRequestError> {
421 if !(method == Self::METHOD
422 || (Self::METHOD == http::Method::GET && method == http::Method::HEAD))
423 {
424 return Err(FromHttpRequestError::MethodMismatch {
425 expected: Self::METHOD,
426 received: method.clone(),
427 });
428 }
429
430 Ok(())
431 }
432
433 /// Tries to turn the given `http::Request` into this request type,
434 /// together with the corresponding path arguments.
435 ///
436 /// Note: The strings in path_args need to be percent-decoded.
437 fn try_from_http_request<B, S>(
438 req: http::Request<B>,
439 path_args: &[S],
440 ) -> Result<Self, FromHttpRequestError>
441 where
442 B: AsRef<[u8]>,
443 S: AsRef<str>;
444}
445
446/// A request type for a Matrix API endpoint, used for sending responses.
447pub trait OutgoingResponse {
448 /// Tries to convert this response into an `http::Response`.
449 ///
450 /// This method should only fail when when invalid header values are specified. It may also
451 /// fail with a serialization error in case of bugs in Ruma though.
452 fn try_into_http_response<T: Default + BufMut>(
453 self,
454 ) -> Result<http::Response<T>, IntoHttpError>;
455}
456
457/// Gives users the ability to define their own serializable / deserializable errors.
458pub trait EndpointError: OutgoingResponse + StdError + Sized + Send + 'static {
459 /// Tries to construct `Self` from an `http::Response`.
460 ///
461 /// This will always return `Err` variant when no `error` field is defined in
462 /// the `ruma_api` macro.
463 fn from_http_response(response: http::Response<&[u8]>) -> Self;
464}
465
466/// The direction to return events from.
467#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Deserialize, Serialize)]
468#[allow(clippy::exhaustive_enums)]
469pub enum Direction {
470 /// Return events backwards in time from the requested `from` token.
471 #[default]
472 #[serde(rename = "b")]
473 Backward,
474
475 /// Return events forwards in time from the requested `from` token.
476 #[serde(rename = "f")]
477 Forward,
478}
479
480impl Direction {
481 /// method for providing forward as the default direction to serde instead
482 pub fn forward() -> Self {
483 Self::Forward
484 }
485}
486
487/// Data to [assert the identity] of an appservice virtual user.
488///
489/// [assert the identity]: https://spec.matrix.org/v1.19/application-service-api/#identity-assertion
490#[derive(Debug, Clone, Copy, Default, Serialize)]
491#[non_exhaustive]
492pub struct AppserviceUserIdentity<'a> {
493 /// The ID of the virtual user.
494 ///
495 /// If this is not set, the user implied by the `sender_localpart` property of the registration
496 /// will be used by the server.
497 #[serde(skip_serializing_if = "Option::is_none")]
498 pub user_id: Option<&'a UserId>,
499
500 /// The ID of a specific device belonging to the virtual user.
501 #[serde(skip_serializing_if = "Option::is_none")]
502 pub device_id: Option<&'a DeviceId>,
503}
504
505impl<'a> AppserviceUserIdentity<'a> {
506 /// Construct a new `AppserviceUserIdentity` with the given user ID.
507 pub fn new(user_id: &'a UserId) -> Self {
508 Self { user_id: Some(user_id), device_id: None }
509 }
510
511 /// Whether this identity is empty.
512 fn is_empty(&self) -> bool {
513 self.user_id.is_none() && self.device_id.is_none()
514 }
515
516 /// Add this identity to the given URI, if the identity is not empty.
517 pub fn maybe_add_to_uri(&self, uri: &mut http::Uri) -> Result<(), IntoHttpError> {
518 if self.is_empty() {
519 // There will be no change to the URI.
520 return Ok(());
521 }
522
523 // Serialize the query arguments of the identity.
524 let identity_query = serde_html_form::to_string(self)?;
525
526 // Add the query arguments to the URI.
527 let mut parts = uri.clone().into_parts();
528
529 let path_and_query_with_user_id = match &parts.path_and_query {
530 Some(path_and_query) => match path_and_query.query() {
531 Some(_) => format!("{path_and_query}&{identity_query}"),
532 None => format!("{path_and_query}?{identity_query}"),
533 },
534 None => format!("/?{identity_query}"),
535 };
536
537 parts.path_and_query =
538 Some(path_and_query_with_user_id.try_into().map_err(http::Error::from)?);
539
540 *uri = parts.try_into().map_err(http::Error::from)?;
541
542 Ok(())
543 }
544}