client-api: Fix serialization of endpoints with optional path segment
This commit is contained in:
parent
a8ba82d585
commit
9f11510427
@ -80,26 +80,16 @@ pub mod v3 {
|
|||||||
access_token: ruma_common::api::SendAccessToken<'_>,
|
access_token: ruma_common::api::SendAccessToken<'_>,
|
||||||
considering_versions: &'_ [ruma_common::api::MatrixVersion],
|
considering_versions: &'_ [ruma_common::api::MatrixVersion],
|
||||||
) -> Result<http::Request<T>, ruma_common::api::error::IntoHttpError> {
|
) -> Result<http::Request<T>, ruma_common::api::error::IntoHttpError> {
|
||||||
use std::borrow::Cow;
|
|
||||||
|
|
||||||
use http::header;
|
use http::header;
|
||||||
use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC};
|
|
||||||
|
|
||||||
let mut url = METADATA.make_endpoint_url(
|
|
||||||
considering_versions,
|
|
||||||
base_url,
|
|
||||||
&[&self.room_id, &self.event_type],
|
|
||||||
None,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
if !self.state_key.is_empty() {
|
|
||||||
url.push('/');
|
|
||||||
url.push_str(&Cow::from(utf8_percent_encode(self.state_key, NON_ALPHANUMERIC)));
|
|
||||||
}
|
|
||||||
|
|
||||||
http::Request::builder()
|
http::Request::builder()
|
||||||
.method(http::Method::GET)
|
.method(http::Method::GET)
|
||||||
.uri(url)
|
.uri(METADATA.make_endpoint_url(
|
||||||
|
considering_versions,
|
||||||
|
base_url,
|
||||||
|
&[&self.room_id, &self.event_type, &self.state_key],
|
||||||
|
None,
|
||||||
|
)?)
|
||||||
.header(header::CONTENT_TYPE, "application/json")
|
.header(header::CONTENT_TYPE, "application/json")
|
||||||
.header(
|
.header(
|
||||||
header::AUTHORIZATION,
|
header::AUTHORIZATION,
|
||||||
|
@ -122,31 +122,20 @@ pub mod v3 {
|
|||||||
access_token: ruma_common::api::SendAccessToken<'_>,
|
access_token: ruma_common::api::SendAccessToken<'_>,
|
||||||
considering_versions: &'_ [ruma_common::api::MatrixVersion],
|
considering_versions: &'_ [ruma_common::api::MatrixVersion],
|
||||||
) -> Result<http::Request<T>, ruma_common::api::error::IntoHttpError> {
|
) -> Result<http::Request<T>, ruma_common::api::error::IntoHttpError> {
|
||||||
use std::borrow::Cow;
|
|
||||||
|
|
||||||
use http::header::{self, HeaderValue};
|
use http::header::{self, HeaderValue};
|
||||||
use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC};
|
|
||||||
|
|
||||||
let mut url = METADATA.make_endpoint_url(
|
let query_string = ruma_common::serde::urlencoded::to_string(RequestQuery {
|
||||||
considering_versions,
|
timestamp: self.timestamp,
|
||||||
base_url,
|
})?;
|
||||||
&[&self.room_id, &self.event_type],
|
|
||||||
None,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
// Last URL segment is optional, that is why this trait impl is not generated.
|
|
||||||
if !self.state_key.is_empty() {
|
|
||||||
url.push('/');
|
|
||||||
url.push_str(&Cow::from(utf8_percent_encode(self.state_key, NON_ALPHANUMERIC)));
|
|
||||||
}
|
|
||||||
|
|
||||||
let request_query = RequestQuery { timestamp: self.timestamp };
|
|
||||||
url.push('?');
|
|
||||||
url.push_str(&ruma_common::serde::urlencoded::to_string(request_query)?);
|
|
||||||
|
|
||||||
let http_request = http::Request::builder()
|
let http_request = http::Request::builder()
|
||||||
.method(http::Method::PUT)
|
.method(http::Method::PUT)
|
||||||
.uri(url)
|
.uri(METADATA.make_endpoint_url(
|
||||||
|
considering_versions,
|
||||||
|
base_url,
|
||||||
|
&[&self.room_id, &self.event_type, &self.state_key],
|
||||||
|
Some(&query_string),
|
||||||
|
)?)
|
||||||
.header(header::CONTENT_TYPE, "application/json")
|
.header(header::CONTENT_TYPE, "application/json")
|
||||||
.header(
|
.header(
|
||||||
header::AUTHORIZATION,
|
header::AUTHORIZATION,
|
||||||
@ -220,4 +209,28 @@ pub mod v3 {
|
|||||||
#[serde(rename = "ts", skip_serializing_if = "Option::is_none")]
|
#[serde(rename = "ts", skip_serializing_if = "Option::is_none")]
|
||||||
pub timestamp: Option<MilliSecondsSinceUnixEpoch>,
|
pub timestamp: Option<MilliSecondsSinceUnixEpoch>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "client")]
|
||||||
|
#[test]
|
||||||
|
fn serialize() {
|
||||||
|
use ruma_common::{
|
||||||
|
api::{MatrixVersion, OutgoingRequest as _, SendAccessToken},
|
||||||
|
events::{room::name::RoomNameEventContent, EmptyStateKey},
|
||||||
|
room_id,
|
||||||
|
};
|
||||||
|
|
||||||
|
// This used to panic in make_endpoint_url because of a mismatch in the path parameter count
|
||||||
|
Request::new(
|
||||||
|
room_id!("!room:server.tld"),
|
||||||
|
&EmptyStateKey,
|
||||||
|
&RoomNameEventContent::new(Some("Test room".to_owned())),
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
|
.try_into_http_request::<Vec<u8>>(
|
||||||
|
"https://server.tld",
|
||||||
|
SendAccessToken::IfRequired("access_token"),
|
||||||
|
&[MatrixVersion::V1_1],
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user