api: Allow to use any HeaderName with the header keyword of ruma_api

This commit is contained in:
Kévin Commaille 2022-10-02 15:52:18 +02:00 committed by Kévin Commaille
parent b57338c1cf
commit 739c35aaca
19 changed files with 30 additions and 11 deletions

View File

@ -5,6 +5,7 @@ pub mod v3 {
//! //!
//! [spec]: https://spec.matrix.org/v1.2/client-server-api/#post_matrixmediav3upload //! [spec]: https://spec.matrix.org/v1.2/client-server-api/#post_matrixmediav3upload
use http::header::CONTENT_TYPE;
use ruma_common::{api::ruma_api, OwnedMxcUri}; use ruma_common::{api::ruma_api, OwnedMxcUri};
ruma_api! { ruma_api! {

View File

@ -5,6 +5,7 @@ pub mod v3 {
//! //!
//! [spec]: https://spec.matrix.org/v1.2/client-server-api/#get_matrixmediav3downloadservernamemediaid //! [spec]: https://spec.matrix.org/v1.2/client-server-api/#get_matrixmediav3downloadservernamemediaid
use http::header::{CONTENT_DISPOSITION, CONTENT_TYPE};
#[cfg(feature = "unstable-msc2246")] #[cfg(feature = "unstable-msc2246")]
use js_int::UInt; use js_int::UInt;
use ruma_common::{api::ruma_api, IdParseError, MxcUri, ServerName}; use ruma_common::{api::ruma_api, IdParseError, MxcUri, ServerName};

View File

@ -5,6 +5,7 @@ pub mod v3 {
//! //!
//! [spec]: https://spec.matrix.org/v1.2/client-server-api/#get_matrixmediav3downloadservernamemediaidfilename //! [spec]: https://spec.matrix.org/v1.2/client-server-api/#get_matrixmediav3downloadservernamemediaidfilename
use http::header::{CONTENT_DISPOSITION, CONTENT_TYPE};
use ruma_common::{api::ruma_api, IdParseError, MxcUri, ServerName}; use ruma_common::{api::ruma_api, IdParseError, MxcUri, ServerName};
ruma_api! { ruma_api! {

View File

@ -5,6 +5,7 @@ pub mod v3 {
//! //!
//! [spec]: https://spec.matrix.org/v1.2/client-server-api/#get_matrixmediav3thumbnailservernamemediaid //! [spec]: https://spec.matrix.org/v1.2/client-server-api/#get_matrixmediav3thumbnailservernamemediaid
use http::header::CONTENT_TYPE;
use js_int::UInt; use js_int::UInt;
use ruma_common::{api::ruma_api, serde::StringEnum, IdParseError, MxcUri, ServerName}; use ruma_common::{api::ruma_api, serde::StringEnum, IdParseError, MxcUri, ServerName};

View File

@ -5,6 +5,7 @@ pub mod v3 {
//! //!
//! [spec]: https://spec.matrix.org/v1.2/client-server-api/#get_matrixclientv3loginssoredirect //! [spec]: https://spec.matrix.org/v1.2/client-server-api/#get_matrixclientv3loginssoredirect
use http::header::LOCATION;
use ruma_common::api::ruma_api; use ruma_common::api::ruma_api;
ruma_api! { ruma_api! {

View File

@ -7,6 +7,7 @@ pub mod v3 {
//! //!
//! [spec]: https://spec.matrix.org/v1.2/client-server-api/#get_matrixclientv3loginssoredirectidpid //! [spec]: https://spec.matrix.org/v1.2/client-server-api/#get_matrixclientv3loginssoredirectidpid
use http::header::LOCATION;
use ruma_common::api::ruma_api; use ruma_common::api::ruma_api;
ruma_api! { ruma_api! {

View File

@ -5,6 +5,7 @@ pub mod v3 {
//! //!
//! [spec]: https://spec.matrix.org/v1.2/client-server-api/#fallback //! [spec]: https://spec.matrix.org/v1.2/client-server-api/#fallback
use http::header::LOCATION;
use ruma_common::api::ruma_api; use ruma_common::api::ruma_api;
ruma_api! { ruma_api! {

View File

@ -5,6 +5,7 @@ pub mod v3 {
//! //!
//! [spec]: https://spec.matrix.org/v1.2/client-server-api/#post_matrixclientv3user_directorysearch //! [spec]: https://spec.matrix.org/v1.2/client-server-api/#post_matrixclientv3user_directorysearch
use http::header::ACCEPT_LANGUAGE;
use js_int::{uint, UInt}; use js_int::{uint, UInt};
use ruma_common::{api::ruma_api, OwnedMxcUri, OwnedUserId}; use ruma_common::{api::ruma_api, OwnedMxcUri, OwnedUserId};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};

View File

@ -6,6 +6,10 @@ Breaking changes:
* Remove deprecated constructors for `RoomMessageEventContent` * Remove deprecated constructors for `RoomMessageEventContent`
* Remove `serde::vec_as_map_of_empty` from the public API * Remove `serde::vec_as_map_of_empty` from the public API
* Remove the `api::AuthScheme::QueryOnlyAccessToken` variant, which is no longer used * Remove the `api::AuthScheme::QueryOnlyAccessToken` variant, which is no longer used
* The `#[ruma_api(header)]` attribute of the `ruma_api` macro now accepts an arbitrary
`http::header::HeaderName`
* To continue using constants from `http::header`, they must be imported in
the module calling the macro.
Improvements: Improvements:

View File

@ -78,8 +78,9 @@ use crate::UserId;
/// ///
/// * `#[ruma_api(header = HEADER_NAME)]`: Fields with this attribute will be treated as HTTP /// * `#[ruma_api(header = HEADER_NAME)]`: Fields with this attribute will be treated as HTTP
/// headers on the request. The value must implement `AsRef<str>`. Generally this is a /// headers on the request. The value must implement `AsRef<str>`. Generally this is a
/// `String`. The attribute value shown above as `HEADER_NAME` must be a header name constant /// `String`. The attribute value shown above as `HEADER_NAME` must be a `const` expression
/// from `http::header`, e.g. `CONTENT_TYPE`. /// of the type `http::header::HeaderName`, like one of the constants from `http::header`,
/// e.g. `CONTENT_TYPE`.
/// * `#[ruma_api(path)]`: Fields with this attribute will be inserted into the matching path /// * `#[ruma_api(path)]`: Fields with this attribute will be inserted into the matching path
/// component of the request URL. /// component of the request URL.
/// * `#[ruma_api(query)]`: Fields with this attribute will be inserting into the URL's query /// * `#[ruma_api(query)]`: Fields with this attribute will be inserting into the URL's query
@ -123,6 +124,7 @@ use crate::UserId;
/// ///
/// ``` /// ```
/// pub mod some_endpoint { /// pub mod some_endpoint {
/// use http::header::CONTENT_TYPE;
/// use ruma_common::api::ruma_api; /// use ruma_common::api::ruma_api;
/// ///
/// ruma_api! { /// ruma_api! {

View File

@ -1,5 +1,6 @@
#![allow(clippy::exhaustive_structs)] #![allow(clippy::exhaustive_structs)]
use http::header::CONTENT_TYPE;
use ruma_common::{ use ruma_common::{
api::{ api::{
ruma_api, IncomingRequest as _, MatrixVersion, OutgoingRequest as _, ruma_api, IncomingRequest as _, MatrixVersion, OutgoingRequest as _,
@ -121,7 +122,7 @@ fn request_with_user_id_serde() {
mod without_query { mod without_query {
use ruma_common::{api::MatrixVersion, OwnedUserId}; use ruma_common::{api::MatrixVersion, OwnedUserId};
use super::{ruma_api, user_id, OutgoingRequestAppserviceExt, SendAccessToken}; use super::{ruma_api, user_id, OutgoingRequestAppserviceExt, SendAccessToken, CONTENT_TYPE};
ruma_api! { ruma_api! {
metadata: { metadata: {

View File

@ -1,6 +1,6 @@
#![allow(clippy::exhaustive_structs)] #![allow(clippy::exhaustive_structs)]
use http::header::{Entry, CONTENT_TYPE}; use http::header::{Entry, CONTENT_TYPE, LOCATION};
use ruma_common::api::{ use ruma_common::api::{
ruma_api, MatrixVersion, OutgoingRequest as _, OutgoingResponse as _, SendAccessToken, ruma_api, MatrixVersion, OutgoingRequest as _, OutgoingResponse as _, SendAccessToken,
}; };

View File

@ -1,3 +1,4 @@
use http::header::LOCATION;
use ruma_common::api::ruma_api; use ruma_common::api::ruma_api;
ruma_api! { ruma_api! {

View File

@ -58,6 +58,7 @@ mod nested_types {
} }
mod full_request_response { mod full_request_response {
use http::header::CONTENT_TYPE;
use ruma_common::api::ruma_api; use ruma_common::api::ruma_api;
use super::{IncomingOtherThing, OtherThing}; use super::{IncomingOtherThing, OtherThing};
@ -92,6 +93,7 @@ mod full_request_response {
} }
mod full_request_response_with_query_map { mod full_request_response_with_query_map {
use http::header::CONTENT_TYPE;
use ruma_common::api::ruma_api; use ruma_common::api::ruma_api;
ruma_api! { ruma_api! {

View File

@ -1,6 +1,7 @@
#![allow(clippy::exhaustive_structs)] #![allow(clippy::exhaustive_structs)]
pub mod some_endpoint { pub mod some_endpoint {
use http::header::CONTENT_TYPE;
use ruma_common::{ use ruma_common::{
api::ruma_api, api::ruma_api,
events::{tag::TagEvent, AnyTimelineEvent}, events::{tag::TagEvent, AnyTimelineEvent},

View File

@ -113,7 +113,7 @@ impl Request {
let decl = quote! { let decl = quote! {
#( #cfg_attrs )* #( #cfg_attrs )*
let #field_name = match headers.get(#http::header::#header_name) { let #field_name = match headers.get(#header_name) {
Some(header_value) => { Some(header_value) => {
let str_value = header_value.to_str()?; let str_value = header_value.to_str()?;
#some_case #some_case

View File

@ -88,7 +88,7 @@ impl Request {
quote! { quote! {
if let Some(header_val) = self.#field_name.as_ref() { if let Some(header_val) = self.#field_name.as_ref() {
req_headers.insert( req_headers.insert(
#http::header::#header_name, #header_name,
#http::header::HeaderValue::from_str(header_val)?, #http::header::HeaderValue::from_str(header_val)?,
); );
} }
@ -96,7 +96,7 @@ impl Request {
} }
_ => quote! { _ => quote! {
req_headers.insert( req_headers.insert(
#http::header::#header_name, #header_name,
#http::header::HeaderValue::from_str(self.#field_name.as_ref())?, #http::header::HeaderValue::from_str(self.#field_name.as_ref())?,
); );
}, },

View File

@ -60,7 +60,7 @@ impl Response {
quote! { quote! {
#( #cfg_attrs )* #( #cfg_attrs )*
#field_name: { #field_name: {
headers.remove(#http::header::#header_name) headers.remove(#header_name)
.map(|h| h.to_str().map(|s| s.to_owned())) .map(|h| h.to_str().map(|s| s.to_owned()))
.transpose()? .transpose()?
} }
@ -69,7 +69,7 @@ impl Response {
_ => quote! { _ => quote! {
#( #cfg_attrs )* #( #cfg_attrs )*
#field_name: { #field_name: {
headers.remove(#http::header::#header_name) headers.remove(#header_name)
.expect("response missing expected header") .expect("response missing expected header")
.to_str()? .to_str()?
.to_owned() .to_owned()

View File

@ -20,7 +20,7 @@ impl Response {
quote! { quote! {
if let Some(header) = self.#field_name { if let Some(header) = self.#field_name {
headers.insert( headers.insert(
#http::header::#header_name, #header_name,
header.parse()?, header.parse()?,
); );
} }
@ -28,7 +28,7 @@ impl Response {
} }
_ => quote! { _ => quote! {
headers.insert( headers.insert(
#http::header::#header_name, #header_name,
self.#field_name.parse()?, self.#field_name.parse()?,
); );
}, },