Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Breaking changes:
compatible with all requests from ruma-client-api and ruma-appservice-api.
- `HttpRequest::RequestBuilder` has an extra `AsRef<[u8]>` bound.
- Bump MSRV to 1.88
- `Client::send_request_as()` now takes `AppserviceUserIdentity` instead of `&UserId`.

Improvements:

Expand Down
17 changes: 10 additions & 7 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use assign::assign;
use async_stream::try_stream;
use futures_core::stream::Stream;
use ruma::{
DeviceId, UserId,
DeviceId,
api::{
OutgoingRequest, SupportedVersions,
AppserviceUserIdentity, OutgoingRequest, SupportedVersions,
auth_scheme::{AuthScheme, SendAccessToken},
client::{
account::register::{self, RegistrationKind},
Expand All @@ -23,9 +23,7 @@ use ruma::{
presence::PresenceState,
};

use crate::{
Error, HttpClient, ResponseError, ResponseResult, add_user_id_to_query, send_customized_request,
};
use crate::{Error, HttpClient, ResponseError, ResponseResult, send_customized_request};

mod builder;

Expand Down Expand Up @@ -133,13 +131,18 @@ impl<C: HttpClient> Client<C> {
///
/// This method is meant to be used by application services when interacting with the
/// client-server API.
pub async fn send_request_as<R>(&self, user_id: &UserId, request: R) -> ResponseResult<C, R>
pub async fn send_request_as<R>(
&self,
identity: AppserviceUserIdentity<'_>,
request: R,
) -> ResponseResult<C, R>
where
R: OutgoingRequest,
for<'a> R::Authentication: AuthScheme<Input<'a> = SendAccessToken<'a>>,
R::PathBuilder: SupportedPathBuilder,
{
self.send_customized_request(request, add_user_id_to_query::<C, R>(user_id)).await
self.send_customized_request(request, |uri| Ok(identity.maybe_add_to_uri(uri.uri_mut())?))
.await
}

/// Log in with a username and password.
Expand Down
23 changes: 9 additions & 14 deletions src/http_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@
use std::{future::Future, pin::Pin};

use bytes::BufMut;
use ruma::{
UserId,
api::{
OutgoingRequest,
auth_scheme::{AuthScheme, SendAccessToken},
path_builder::PathBuilder,
},
use ruma::api::{
AppserviceUserIdentity, OutgoingRequest,
auth_scheme::{AuthScheme, SendAccessToken},
path_builder::PathBuilder,
};

use crate::{ResponseError, ResponseResult, add_user_id_to_query};
use crate::{ResponseError, ResponseResult};

#[cfg(feature = "hyper")]
mod hyper;
Expand Down Expand Up @@ -59,7 +56,6 @@ pub trait DefaultConstructibleHttpClient: HttpClient {
/// trait should make that relatively easy.
pub trait HttpClientExt: HttpClient {
/// Send a strongly-typed matrix request to get back a strongly-typed response.
// TODO: `R: 'a` bound should not be needed
fn send_matrix_request<'a, R>(
&'a self,
homeserver_url: &str,
Expand All @@ -82,7 +78,6 @@ pub trait HttpClientExt: HttpClient {

/// Turn a strongly-typed matrix request into an `http::Request`, customize it and send it to
/// get back a strongly-typed response.
// TODO: `R: 'a` and `F: 'a` should not be needed
fn send_customized_matrix_request<'a, R, F>(
&'a self,
homeserver_url: &str,
Expand All @@ -106,8 +101,8 @@ pub trait HttpClientExt: HttpClient {
))
}

/// Turn a strongly-typed matrix request into an `http::Request`, add a `user_id` query
/// parameter to it and send it to get back a strongly-typed response.
/// Turn a strongly-typed matrix request into an `http::Request`, add `user_id` and/or
/// `device_id` query parameters to it and send it to get back a strongly-typed response.
///
/// This method is meant to be used by application services when interacting with the
/// client-server API.
Expand All @@ -116,7 +111,7 @@ pub trait HttpClientExt: HttpClient {
homeserver_url: &str,
access_token: SendAccessToken<'a>,
path_builder_input: <R::PathBuilder as PathBuilder>::Input<'_>,
user_id: &'a UserId,
identity: AppserviceUserIdentity<'a>,
request: R,
) -> Pin<Box<dyn Future<Output = ResponseResult<Self, R>> + 'a>>
where
Expand All @@ -128,7 +123,7 @@ pub trait HttpClientExt: HttpClient {
access_token,
path_builder_input,
request,
add_user_id_to_query::<Self, R>(user_id),
|uri| Ok(identity.maybe_add_to_uri(uri.uri_mut())?),
)
}
}
Expand Down
32 changes: 4 additions & 28 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,10 @@ use std::{any::type_name, future::Future};

#[doc(no_inline)]
pub use ruma;
use ruma::{
UserId,
api::{
OutgoingRequest,
auth_scheme::{AuthScheme, SendAccessToken},
path_builder::PathBuilder,
},
use ruma::api::{
OutgoingRequest,
auth_scheme::{AuthScheme, SendAccessToken},
path_builder::PathBuilder,
};
use tracing::{Instrument, info_span};

Expand Down Expand Up @@ -177,24 +174,3 @@ where
Ok(res)
}
}

fn add_user_id_to_query<C: HttpClient + ?Sized, R: OutgoingRequest>(
user_id: &UserId,
) -> impl FnOnce(&mut http::Request<C::RequestBody>) -> Result<(), ResponseError<C, R>> + '_ {
use assign::assign;
use http::uri::Uri;

move |http_request| {
let extra_params = serde_html_form::to_string([("user_id", user_id)]).unwrap();
let uri = http_request.uri_mut();
let new_path_and_query = match uri.query() {
Some(params) => format!("{}?{params}&{extra_params}", uri.path()),
None => format!("{}?{extra_params}", uri.path()),
};
*uri = Uri::from_parts(assign!(uri.clone().into_parts(), {
path_and_query: Some(new_path_and_query.parse()?),
}))?;

Ok(())
}
}
Loading