api: Allow to use any HeaderName with the header keyword of ruma_api
This commit is contained in:
		
							parent
							
								
									b57338c1cf
								
							
						
					
					
						commit
						739c35aaca
					
				| @ -5,6 +5,7 @@ pub mod v3 { | ||||
|     //!
 | ||||
|     //! [spec]: https://spec.matrix.org/v1.2/client-server-api/#post_matrixmediav3upload
 | ||||
| 
 | ||||
|     use http::header::CONTENT_TYPE; | ||||
|     use ruma_common::{api::ruma_api, OwnedMxcUri}; | ||||
| 
 | ||||
|     ruma_api! { | ||||
|  | ||||
| @ -5,6 +5,7 @@ pub mod v3 { | ||||
|     //!
 | ||||
|     //! [spec]: https://spec.matrix.org/v1.2/client-server-api/#get_matrixmediav3downloadservernamemediaid
 | ||||
| 
 | ||||
|     use http::header::{CONTENT_DISPOSITION, CONTENT_TYPE}; | ||||
|     #[cfg(feature = "unstable-msc2246")] | ||||
|     use js_int::UInt; | ||||
|     use ruma_common::{api::ruma_api, IdParseError, MxcUri, ServerName}; | ||||
|  | ||||
| @ -5,6 +5,7 @@ pub mod v3 { | ||||
|     //!
 | ||||
|     //! [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}; | ||||
| 
 | ||||
|     ruma_api! { | ||||
|  | ||||
| @ -5,6 +5,7 @@ pub mod v3 { | ||||
|     //!
 | ||||
|     //! [spec]: https://spec.matrix.org/v1.2/client-server-api/#get_matrixmediav3thumbnailservernamemediaid
 | ||||
| 
 | ||||
|     use http::header::CONTENT_TYPE; | ||||
|     use js_int::UInt; | ||||
|     use ruma_common::{api::ruma_api, serde::StringEnum, IdParseError, MxcUri, ServerName}; | ||||
| 
 | ||||
|  | ||||
| @ -5,6 +5,7 @@ pub mod v3 { | ||||
|     //!
 | ||||
|     //! [spec]: https://spec.matrix.org/v1.2/client-server-api/#get_matrixclientv3loginssoredirect
 | ||||
| 
 | ||||
|     use http::header::LOCATION; | ||||
|     use ruma_common::api::ruma_api; | ||||
| 
 | ||||
|     ruma_api! { | ||||
|  | ||||
| @ -7,6 +7,7 @@ pub mod v3 { | ||||
|     //!
 | ||||
|     //! [spec]: https://spec.matrix.org/v1.2/client-server-api/#get_matrixclientv3loginssoredirectidpid
 | ||||
| 
 | ||||
|     use http::header::LOCATION; | ||||
|     use ruma_common::api::ruma_api; | ||||
| 
 | ||||
|     ruma_api! { | ||||
|  | ||||
| @ -5,6 +5,7 @@ pub mod v3 { | ||||
|     //!
 | ||||
|     //! [spec]: https://spec.matrix.org/v1.2/client-server-api/#fallback
 | ||||
| 
 | ||||
|     use http::header::LOCATION; | ||||
|     use ruma_common::api::ruma_api; | ||||
| 
 | ||||
|     ruma_api! { | ||||
|  | ||||
| @ -5,6 +5,7 @@ pub mod v3 { | ||||
|     //!
 | ||||
|     //! [spec]: https://spec.matrix.org/v1.2/client-server-api/#post_matrixclientv3user_directorysearch
 | ||||
| 
 | ||||
|     use http::header::ACCEPT_LANGUAGE; | ||||
|     use js_int::{uint, UInt}; | ||||
|     use ruma_common::{api::ruma_api, OwnedMxcUri, OwnedUserId}; | ||||
|     use serde::{Deserialize, Serialize}; | ||||
|  | ||||
| @ -6,6 +6,10 @@ Breaking changes: | ||||
| * Remove deprecated constructors for `RoomMessageEventContent` | ||||
| * Remove `serde::vec_as_map_of_empty` from the public API | ||||
| * 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: | ||||
| 
 | ||||
|  | ||||
| @ -78,8 +78,9 @@ use crate::UserId; | ||||
| ///
 | ||||
| /// * `#[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
 | ||||
| ///   `String`. The attribute value shown above as `HEADER_NAME` must be a header name constant
 | ||||
| ///   from `http::header`, e.g. `CONTENT_TYPE`.
 | ||||
| ///   `String`. The attribute value shown above as `HEADER_NAME` must be a `const` expression
 | ||||
| ///   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
 | ||||
| ///   component of the request URL.
 | ||||
| /// * `#[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 {
 | ||||
| ///     use http::header::CONTENT_TYPE;
 | ||||
| ///     use ruma_common::api::ruma_api;
 | ||||
| ///
 | ||||
| ///     ruma_api! {
 | ||||
|  | ||||
| @ -1,5 +1,6 @@ | ||||
| #![allow(clippy::exhaustive_structs)] | ||||
| 
 | ||||
| use http::header::CONTENT_TYPE; | ||||
| use ruma_common::{ | ||||
|     api::{ | ||||
|         ruma_api, IncomingRequest as _, MatrixVersion, OutgoingRequest as _, | ||||
| @ -121,7 +122,7 @@ fn request_with_user_id_serde() { | ||||
| mod without_query { | ||||
|     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! { | ||||
|         metadata: { | ||||
|  | ||||
| @ -1,6 +1,6 @@ | ||||
| #![allow(clippy::exhaustive_structs)] | ||||
| 
 | ||||
| use http::header::{Entry, CONTENT_TYPE}; | ||||
| use http::header::{Entry, CONTENT_TYPE, LOCATION}; | ||||
| use ruma_common::api::{ | ||||
|     ruma_api, MatrixVersion, OutgoingRequest as _, OutgoingResponse as _, SendAccessToken, | ||||
| }; | ||||
|  | ||||
| @ -1,3 +1,4 @@ | ||||
| use http::header::LOCATION; | ||||
| use ruma_common::api::ruma_api; | ||||
| 
 | ||||
| ruma_api! { | ||||
|  | ||||
| @ -58,6 +58,7 @@ mod nested_types { | ||||
| } | ||||
| 
 | ||||
| mod full_request_response { | ||||
|     use http::header::CONTENT_TYPE; | ||||
|     use ruma_common::api::ruma_api; | ||||
| 
 | ||||
|     use super::{IncomingOtherThing, OtherThing}; | ||||
| @ -92,6 +93,7 @@ mod full_request_response { | ||||
| } | ||||
| 
 | ||||
| mod full_request_response_with_query_map { | ||||
|     use http::header::CONTENT_TYPE; | ||||
|     use ruma_common::api::ruma_api; | ||||
| 
 | ||||
|     ruma_api! { | ||||
|  | ||||
| @ -1,6 +1,7 @@ | ||||
| #![allow(clippy::exhaustive_structs)] | ||||
| 
 | ||||
| pub mod some_endpoint { | ||||
|     use http::header::CONTENT_TYPE; | ||||
|     use ruma_common::{ | ||||
|         api::ruma_api, | ||||
|         events::{tag::TagEvent, AnyTimelineEvent}, | ||||
|  | ||||
| @ -113,7 +113,7 @@ impl Request { | ||||
| 
 | ||||
|                     let decl = quote! { | ||||
|                         #( #cfg_attrs )* | ||||
|                         let #field_name = match headers.get(#http::header::#header_name) { | ||||
|                         let #field_name = match headers.get(#header_name) { | ||||
|                             Some(header_value) => { | ||||
|                                 let str_value = header_value.to_str()?; | ||||
|                                 #some_case | ||||
|  | ||||
| @ -88,7 +88,7 @@ impl Request { | ||||
|                     quote! { | ||||
|                         if let Some(header_val) = self.#field_name.as_ref() { | ||||
|                             req_headers.insert( | ||||
|                                 #http::header::#header_name, | ||||
|                                 #header_name, | ||||
|                                 #http::header::HeaderValue::from_str(header_val)?, | ||||
|                             ); | ||||
|                         } | ||||
| @ -96,7 +96,7 @@ impl Request { | ||||
|                 } | ||||
|                 _ => quote! { | ||||
|                     req_headers.insert( | ||||
|                         #http::header::#header_name, | ||||
|                         #header_name, | ||||
|                         #http::header::HeaderValue::from_str(self.#field_name.as_ref())?, | ||||
|                     ); | ||||
|                 }, | ||||
|  | ||||
| @ -60,7 +60,7 @@ impl Response { | ||||
|                                 quote! { | ||||
|                                     #( #cfg_attrs )* | ||||
|                                     #field_name: { | ||||
|                                         headers.remove(#http::header::#header_name) | ||||
|                                         headers.remove(#header_name) | ||||
|                                             .map(|h| h.to_str().map(|s| s.to_owned())) | ||||
|                                             .transpose()? | ||||
|                                     } | ||||
| @ -69,7 +69,7 @@ impl Response { | ||||
|                             _ => quote! { | ||||
|                                 #( #cfg_attrs )* | ||||
|                                 #field_name: { | ||||
|                                     headers.remove(#http::header::#header_name) | ||||
|                                     headers.remove(#header_name) | ||||
|                                         .expect("response missing expected header") | ||||
|                                         .to_str()? | ||||
|                                         .to_owned() | ||||
|  | ||||
| @ -20,7 +20,7 @@ impl Response { | ||||
|                         quote! { | ||||
|                             if let Some(header) = self.#field_name { | ||||
|                                 headers.insert( | ||||
|                                     #http::header::#header_name, | ||||
|                                     #header_name, | ||||
|                                     header.parse()?, | ||||
|                                 ); | ||||
|                             } | ||||
| @ -28,7 +28,7 @@ impl Response { | ||||
|                     } | ||||
|                     _ => quote! { | ||||
|                         headers.insert( | ||||
|                             #http::header::#header_name, | ||||
|                             #header_name, | ||||
|                             self.#field_name.parse()?, | ||||
|                         ); | ||||
|                     }, | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user