Skip to content

Commit

Permalink
refactor!: Put the RequestConfig argument of Client::send() into a bu…
Browse files Browse the repository at this point in the history
…ilder method

Instead of `Client::send(request, request_config)`, consumers can now do
`Client::send(request).with_request_config(request_config)`.
  • Loading branch information
bnjbvr authored and poljar committed Dec 20, 2024
1 parent 519f281 commit 5f5e979
Show file tree
Hide file tree
Showing 23 changed files with 178 additions and 157 deletions.
15 changes: 6 additions & 9 deletions bindings/matrix-sdk-ffi/src/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,15 +449,12 @@ impl Room {
let int_score = score.map(|value| value.into());
self.inner
.client()
.send(
report_content::v3::Request::new(
self.inner.room_id().into(),
event_id,
int_score,
reason,
),
None,
)
.send(report_content::v3::Request::new(
self.inner.room_id().into(),
event_id,
int_score,
reason,
))
.await?;
Ok(())
}
Expand Down
7 changes: 7 additions & 0 deletions crates/matrix-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ All notable changes to this project will be documented in this file.

## [Unreleased] - ReleaseDate

### Refactor

- [**breaking**] Move the optional `RequestConfig` argument of the
`Client::send()` method to the `with_request_config()` builder method. You
should call `Client::send(request).with_request_config(request_config).awat`
now instead.

## [0.9.0] - 2024-12-18

### Bug Fixes
Expand Down
34 changes: 19 additions & 15 deletions crates/matrix-sdk/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl Account {
let user_id = self.client.user_id().ok_or(Error::AuthenticationRequired)?;
let request = get_display_name::v3::Request::new(user_id.to_owned());
let request_config = self.client.request_config().force_auth();
let response = self.client.send(request, Some(request_config)).await?;
let response = self.client.send(request).with_request_config(request_config).await?;
Ok(response.displayname)
}

Expand All @@ -115,7 +115,7 @@ impl Account {
let user_id = self.client.user_id().ok_or(Error::AuthenticationRequired)?;
let request =
set_display_name::v3::Request::new(user_id.to_owned(), name.map(ToOwned::to_owned));
self.client.send(request, None).await?;
self.client.send(request).await?;
Ok(())
}

Expand Down Expand Up @@ -147,7 +147,7 @@ impl Account {

let config = Some(RequestConfig::new().force_auth());

let response = self.client.send(request, config).await?;
let response = self.client.send(request).with_request_config(config).await?;
if let Some(url) = response.avatar_url.clone() {
// If an avatar is found cache it.
let _ = self
Expand Down Expand Up @@ -181,7 +181,7 @@ impl Account {
let user_id = self.client.user_id().ok_or(Error::AuthenticationRequired)?;
let request =
set_avatar_url::v3::Request::new(user_id.to_owned(), url.map(ToOwned::to_owned));
self.client.send(request, None).await?;
self.client.send(request).await?;
Ok(())
}

Expand Down Expand Up @@ -291,7 +291,11 @@ impl Account {
user_id: &UserId,
) -> Result<get_profile::v3::Response> {
let request = get_profile::v3::Request::new(user_id.to_owned());
Ok(self.client.send(request, Some(RequestConfig::short_retry().force_auth())).await?)
Ok(self
.client
.send(request)
.with_request_config(RequestConfig::short_retry().force_auth())
.await?)
}

/// Change the password of the account.
Expand Down Expand Up @@ -344,7 +348,7 @@ impl Account {
let request = assign!(change_password::v3::Request::new(new_password.to_owned()), {
auth: auth_data,
});
Ok(self.client.send(request, None).await?)
Ok(self.client.send(request).await?)
}

/// Deactivate this account definitively.
Expand Down Expand Up @@ -398,7 +402,7 @@ impl Account {
auth: auth_data,
erase: erase_data,
});
Ok(self.client.send(request, None).await?)
Ok(self.client.send(request).await?)
}

/// Get the registered [Third Party Identifiers][3pid] on the homeserver of
Expand Down Expand Up @@ -428,7 +432,7 @@ impl Account {
/// [3pid]: https://spec.matrix.org/v1.2/appendices/#3pid-types
pub async fn get_3pids(&self) -> Result<get_3pids::v3::Response> {
let request = get_3pids::v3::Request::new();
Ok(self.client.send(request, None).await?)
Ok(self.client.send(request).await?)
}

/// Request a token to validate an email address as a [Third Party
Expand Down Expand Up @@ -499,7 +503,7 @@ impl Account {
email.to_owned(),
send_attempt,
);
Ok(self.client.send(request, None).await?)
Ok(self.client.send(request).await?)
}

/// Request a token to validate a phone number as a [Third Party
Expand Down Expand Up @@ -576,7 +580,7 @@ impl Account {
phone_number.to_owned(),
send_attempt,
);
Ok(self.client.send(request, None).await?)
Ok(self.client.send(request).await?)
}

/// Add a [Third Party Identifier][3pid] on the homeserver for this
Expand Down Expand Up @@ -619,7 +623,7 @@ impl Account {
assign!(add_3pid::v3::Request::new(client_secret.to_owned(), sid.to_owned()), {
auth: auth_data
});
Ok(self.client.send(request, None).await?)
Ok(self.client.send(request).await?)
}

/// Delete a [Third Party Identifier][3pid] from the homeserver for this
Expand Down Expand Up @@ -679,7 +683,7 @@ impl Account {
let request = assign!(delete_3pid::v3::Request::new(medium, address.to_owned()), {
id_server: id_server.map(ToOwned::to_owned),
});
Ok(self.client.send(request, None).await?)
Ok(self.client.send(request).await?)
}

/// Get the content of an account data event of statically-known type.
Expand Down Expand Up @@ -749,7 +753,7 @@ impl Account {

let request = get_global_account_data::v3::Request::new(own_user.to_owned(), event_type);

match self.client.send(request, None).await {
match self.client.send(request).await {
Ok(r) => Ok(Some(r.account_data)),
Err(e) => {
if let Some(kind) = e.client_api_error_kind() {
Expand Down Expand Up @@ -802,7 +806,7 @@ impl Account {

let request = set_global_account_data::v3::Request::new(own_user.to_owned(), &content)?;

Ok(self.client.send(request, None).await?)
Ok(self.client.send(request).await?)
}

/// Set the given raw account data event.
Expand All @@ -816,7 +820,7 @@ impl Account {
let request =
set_global_account_data::v3::Request::new_raw(own_user.to_owned(), event_type, content);

Ok(self.client.send(request, None).await?)
Ok(self.client.send(request).await?)
}

/// Marks the room identified by `room_id` as a "direct chat" with each
Expand Down
7 changes: 7 additions & 0 deletions crates/matrix-sdk/src/client/futures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ impl<R> SendRequest<R> {
self
}

/// Use the given [`RequestConfig`] for this send request, instead of the
/// one provided by default.
pub fn with_request_config(mut self, request_config: impl Into<Option<RequestConfig>>) -> Self {
self.config = request_config.into();
self
}

/// Get a subscriber to observe the progress of sending the request
/// body.
#[cfg(not(target_arch = "wasm32"))]
Expand Down
42 changes: 19 additions & 23 deletions crates/matrix-sdk/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ impl Client {
/// # anyhow::Ok(()) };
/// ```
pub async fn get_capabilities(&self) -> HttpResult<Capabilities> {
let res = self.send(get_capabilities::v3::Request::new(), None).await?;
let res = self.send(get_capabilities::v3::Request::new()).await?;
Ok(res.capabilities)
}

Expand Down Expand Up @@ -563,7 +563,7 @@ impl Client {
request.limit = limit;
}

self.send(request, None).await
self.send(request).await
}

/// Get the user id of the current owner of the client.
Expand Down Expand Up @@ -1186,7 +1186,7 @@ impl Client {
room_alias: &RoomAliasId,
) -> HttpResult<get_alias::v3::Response> {
let request = get_alias::v3::Request::new(room_alias.to_owned());
self.send(request, None).await
self.send(request).await
}

/// Checks if a room alias is not in use yet.
Expand All @@ -1213,7 +1213,7 @@ impl Client {
/// Creates a new room alias associated with a room.
pub async fn create_room_alias(&self, alias: &RoomAliasId, room_id: &RoomId) -> HttpResult<()> {
let request = create_alias::v3::Request::new(alias.to_owned(), room_id.to_owned());
self.send(request, None).await?;
self.send(request).await?;
Ok(())
}

Expand Down Expand Up @@ -1351,7 +1351,7 @@ impl Client {
debug!("Didn't find filter locally");
let user_id = self.user_id().ok_or(Error::AuthenticationRequired)?;
let request = FilterUploadRequest::new(user_id.to_owned(), definition);
let response = self.send(request, None).await?;
let response = self.send(request).await?;

self.inner.base_client.receive_filter_upload(filter_name, &response).await?;

Expand All @@ -1369,7 +1369,7 @@ impl Client {
/// * `room_id` - The `RoomId` of the room to be joined.
pub async fn join_room_by_id(&self, room_id: &RoomId) -> Result<Room> {
let request = join_room_by_id::v3::Request::new(room_id.to_owned());
let response = self.send(request, None).await?;
let response = self.send(request).await?;
let base_room = self.base_client().room_joined(&response.room_id).await?;
Ok(Room::new(self.clone(), base_room))
}
Expand All @@ -1391,7 +1391,7 @@ impl Client {
let request = assign!(join_room_by_id_or_alias::v3::Request::new(alias.to_owned()), {
via: server_names.to_owned(),
});
let response = self.send(request, None).await?;
let response = self.send(request).await?;
let base_room = self.base_client().room_joined(&response.room_id).await?;
Ok(Room::new(self.clone(), base_room))
}
Expand Down Expand Up @@ -1438,7 +1438,7 @@ impl Client {
since: since.map(ToOwned::to_owned),
server: server.map(ToOwned::to_owned),
});
self.send(request, None).await
self.send(request).await
}

/// Create a room with the given parameters.
Expand Down Expand Up @@ -1473,7 +1473,7 @@ impl Client {
pub async fn create_room(&self, request: create_room::v3::Request) -> Result<Room> {
let invite = request.invite.clone();
let is_direct_room = request.is_direct;
let response = self.send(request, None).await?;
let response = self.send(request).await?;

let base_room = self.base_client().get_or_create_room(&response.room_id, RoomState::Joined);

Expand Down Expand Up @@ -1557,7 +1557,7 @@ impl Client {
&self,
request: get_public_rooms_filtered::v3::Request,
) -> HttpResult<get_public_rooms_filtered::v3::Response> {
self.send(request, None).await
self.send(request).await
}

/// Send an arbitrary request to the server, without updating client state.
Expand Down Expand Up @@ -1592,25 +1592,21 @@ impl Client {
/// let request = profile::get_profile::v3::Request::new(user_id);
///
/// // Start the request using Client::send()
/// let response = client.send(request, None).await?;
/// let response = client.send(request).await?;
///
/// // Check the corresponding Response struct to find out what types are
/// // returned
/// # anyhow::Ok(()) };
/// ```
pub fn send<Request>(
&self,
request: Request,
config: Option<RequestConfig>,
) -> SendRequest<Request>
pub fn send<Request>(&self, request: Request) -> SendRequest<Request>
where
Request: OutgoingRequest + Clone + Debug,
HttpError: From<FromHttpResponseError<Request::EndpointError>>,
{
SendRequest {
client: self.clone(),
request,
config,
config: None,
send_progress: Default::default(),
homeserver_override: None,
}
Expand Down Expand Up @@ -1828,7 +1824,7 @@ impl Client {
pub async fn devices(&self) -> HttpResult<get_devices::v3::Response> {
let request = get_devices::v3::Request::new();

self.send(request, None).await
self.send(request).await
}

/// Delete the given devices from the server.
Expand Down Expand Up @@ -1879,7 +1875,7 @@ impl Client {
let mut request = delete_devices::v3::Request::new(devices.to_owned());
request.auth = auth_data;

self.send(request, None).await
self.send(request).await
}

/// Change the display name of a device owned by the current user.
Expand All @@ -1899,7 +1895,7 @@ impl Client {
let mut request = update_device::v3::Request::new(device_id.to_owned());
request.display_name = Some(display_name.to_owned());

self.send(request, None).await
self.send(request).await
}

/// Synchronize the client's state with the latest state on the server.
Expand Down Expand Up @@ -2023,7 +2019,7 @@ impl Client {
request_config.timeout += timeout;
}

let response = self.send(request, Some(request_config)).await?;
let response = self.send(request).with_request_config(request_config).await?;
let next_batch = response.next_batch.clone();
let response = self.process_sync(response).await?;

Expand Down Expand Up @@ -2340,7 +2336,7 @@ impl Client {
/// Gets information about the owner of a given access token.
pub async fn whoami(&self) -> HttpResult<whoami::v3::Response> {
let request = whoami::v3::Request::new();
self.send(request, None).await
self.send(request).await
}

/// Subscribes a new receiver to client SessionChange broadcasts.
Expand Down Expand Up @@ -2451,7 +2447,7 @@ impl Client {
) -> Result<Room> {
let request =
assign!(knock_room::v3::Request::new(room_id_or_alias), { reason, via: server_names });
let response = self.send(request, None).await?;
let response = self.send(request).await?;
let base_room = self.inner.base_client.room_knocked(&response.room_id).await?;
Ok(Room::new(self.clone(), base_room))
}
Expand Down
Loading

0 comments on commit 5f5e979

Please sign in to comment.