Skip to content

Commit f17bef9

Browse files
bnjbvrpoljar
authored andcommitted
refactor!: Put the RequestConfig argument of Client::send() into a builder method
Instead of `Client::send(request, request_config)`, consumers can now do `Client::send(request).with_request_config(request_config)`.
1 parent bc8c4f5 commit f17bef9

File tree

23 files changed

+178
-157
lines changed

23 files changed

+178
-157
lines changed

bindings/matrix-sdk-ffi/src/room.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -449,15 +449,12 @@ impl Room {
449449
let int_score = score.map(|value| value.into());
450450
self.inner
451451
.client()
452-
.send(
453-
report_content::v3::Request::new(
454-
self.inner.room_id().into(),
455-
event_id,
456-
int_score,
457-
reason,
458-
),
459-
None,
460-
)
452+
.send(report_content::v3::Request::new(
453+
self.inner.room_id().into(),
454+
event_id,
455+
int_score,
456+
reason,
457+
))
461458
.await?;
462459
Ok(())
463460
}

crates/matrix-sdk/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ All notable changes to this project will be documented in this file.
66

77
## [Unreleased] - ReleaseDate
88

9+
### Refactor
10+
11+
- [**breaking**] Move the optional `RequestConfig` argument of the
12+
`Client::send()` method to the `with_request_config()` builder method. You
13+
should call `Client::send(request).with_request_config(request_config).awat`
14+
now instead.
15+
916
## [0.9.0] - 2024-12-18
1017

1118
### Bug Fixes

crates/matrix-sdk/src/account.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ impl Account {
9191
let user_id = self.client.user_id().ok_or(Error::AuthenticationRequired)?;
9292
let request = get_display_name::v3::Request::new(user_id.to_owned());
9393
let request_config = self.client.request_config().force_auth();
94-
let response = self.client.send(request, Some(request_config)).await?;
94+
let response = self.client.send(request).with_request_config(request_config).await?;
9595
Ok(response.displayname)
9696
}
9797

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

@@ -147,7 +147,7 @@ impl Account {
147147

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

150-
let response = self.client.send(request, config).await?;
150+
let response = self.client.send(request).with_request_config(config).await?;
151151
if let Some(url) = response.avatar_url.clone() {
152152
// If an avatar is found cache it.
153153
let _ = self
@@ -181,7 +181,7 @@ impl Account {
181181
let user_id = self.client.user_id().ok_or(Error::AuthenticationRequired)?;
182182
let request =
183183
set_avatar_url::v3::Request::new(user_id.to_owned(), url.map(ToOwned::to_owned));
184-
self.client.send(request, None).await?;
184+
self.client.send(request).await?;
185185
Ok(())
186186
}
187187

@@ -291,7 +291,11 @@ impl Account {
291291
user_id: &UserId,
292292
) -> Result<get_profile::v3::Response> {
293293
let request = get_profile::v3::Request::new(user_id.to_owned());
294-
Ok(self.client.send(request, Some(RequestConfig::short_retry().force_auth())).await?)
294+
Ok(self
295+
.client
296+
.send(request)
297+
.with_request_config(RequestConfig::short_retry().force_auth())
298+
.await?)
295299
}
296300

297301
/// Change the password of the account.
@@ -344,7 +348,7 @@ impl Account {
344348
let request = assign!(change_password::v3::Request::new(new_password.to_owned()), {
345349
auth: auth_data,
346350
});
347-
Ok(self.client.send(request, None).await?)
351+
Ok(self.client.send(request).await?)
348352
}
349353

350354
/// Deactivate this account definitively.
@@ -398,7 +402,7 @@ impl Account {
398402
auth: auth_data,
399403
erase: erase_data,
400404
});
401-
Ok(self.client.send(request, None).await?)
405+
Ok(self.client.send(request).await?)
402406
}
403407

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

434438
/// Request a token to validate an email address as a [Third Party
@@ -499,7 +503,7 @@ impl Account {
499503
email.to_owned(),
500504
send_attempt,
501505
);
502-
Ok(self.client.send(request, None).await?)
506+
Ok(self.client.send(request).await?)
503507
}
504508

505509
/// Request a token to validate a phone number as a [Third Party
@@ -576,7 +580,7 @@ impl Account {
576580
phone_number.to_owned(),
577581
send_attempt,
578582
);
579-
Ok(self.client.send(request, None).await?)
583+
Ok(self.client.send(request).await?)
580584
}
581585

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

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

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

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

752-
match self.client.send(request, None).await {
756+
match self.client.send(request).await {
753757
Ok(r) => Ok(Some(r.account_data)),
754758
Err(e) => {
755759
if let Some(kind) = e.client_api_error_kind() {
@@ -802,7 +806,7 @@ impl Account {
802806

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

805-
Ok(self.client.send(request, None).await?)
809+
Ok(self.client.send(request).await?)
806810
}
807811

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

819-
Ok(self.client.send(request, None).await?)
823+
Ok(self.client.send(request).await?)
820824
}
821825

822826
/// Marks the room identified by `room_id` as a "direct chat" with each

crates/matrix-sdk/src/client/futures.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ impl<R> SendRequest<R> {
7777
self
7878
}
7979

80+
/// Use the given [`RequestConfig`] for this send request, instead of the
81+
/// one provided by default.
82+
pub fn with_request_config(mut self, request_config: impl Into<Option<RequestConfig>>) -> Self {
83+
self.config = request_config.into();
84+
self
85+
}
86+
8087
/// Get a subscriber to observe the progress of sending the request
8188
/// body.
8289
#[cfg(not(target_arch = "wasm32"))]

crates/matrix-sdk/src/client/mod.rs

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ impl Client {
480480
/// # anyhow::Ok(()) };
481481
/// ```
482482
pub async fn get_capabilities(&self) -> HttpResult<Capabilities> {
483-
let res = self.send(get_capabilities::v3::Request::new(), None).await?;
483+
let res = self.send(get_capabilities::v3::Request::new()).await?;
484484
Ok(res.capabilities)
485485
}
486486

@@ -563,7 +563,7 @@ impl Client {
563563
request.limit = limit;
564564
}
565565

566-
self.send(request, None).await
566+
self.send(request).await
567567
}
568568

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

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

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

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

@@ -1369,7 +1369,7 @@ impl Client {
13691369
/// * `room_id` - The `RoomId` of the room to be joined.
13701370
pub async fn join_room_by_id(&self, room_id: &RoomId) -> Result<Room> {
13711371
let request = join_room_by_id::v3::Request::new(room_id.to_owned());
1372-
let response = self.send(request, None).await?;
1372+
let response = self.send(request).await?;
13731373
let base_room = self.base_client().room_joined(&response.room_id).await?;
13741374
Ok(Room::new(self.clone(), base_room))
13751375
}
@@ -1391,7 +1391,7 @@ impl Client {
13911391
let request = assign!(join_room_by_id_or_alias::v3::Request::new(alias.to_owned()), {
13921392
via: server_names.to_owned(),
13931393
});
1394-
let response = self.send(request, None).await?;
1394+
let response = self.send(request).await?;
13951395
let base_room = self.base_client().room_joined(&response.room_id).await?;
13961396
Ok(Room::new(self.clone(), base_room))
13971397
}
@@ -1438,7 +1438,7 @@ impl Client {
14381438
since: since.map(ToOwned::to_owned),
14391439
server: server.map(ToOwned::to_owned),
14401440
});
1441-
self.send(request, None).await
1441+
self.send(request).await
14421442
}
14431443

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

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

@@ -1557,7 +1557,7 @@ impl Client {
15571557
&self,
15581558
request: get_public_rooms_filtered::v3::Request,
15591559
) -> HttpResult<get_public_rooms_filtered::v3::Response> {
1560-
self.send(request, None).await
1560+
self.send(request).await
15611561
}
15621562

15631563
/// Send an arbitrary request to the server, without updating client state.
@@ -1592,25 +1592,21 @@ impl Client {
15921592
/// let request = profile::get_profile::v3::Request::new(user_id);
15931593
///
15941594
/// // Start the request using Client::send()
1595-
/// let response = client.send(request, None).await?;
1595+
/// let response = client.send(request).await?;
15961596
///
15971597
/// // Check the corresponding Response struct to find out what types are
15981598
/// // returned
15991599
/// # anyhow::Ok(()) };
16001600
/// ```
1601-
pub fn send<Request>(
1602-
&self,
1603-
request: Request,
1604-
config: Option<RequestConfig>,
1605-
) -> SendRequest<Request>
1601+
pub fn send<Request>(&self, request: Request) -> SendRequest<Request>
16061602
where
16071603
Request: OutgoingRequest + Clone + Debug,
16081604
HttpError: From<FromHttpResponseError<Request::EndpointError>>,
16091605
{
16101606
SendRequest {
16111607
client: self.clone(),
16121608
request,
1613-
config,
1609+
config: None,
16141610
send_progress: Default::default(),
16151611
homeserver_override: None,
16161612
}
@@ -1828,7 +1824,7 @@ impl Client {
18281824
pub async fn devices(&self) -> HttpResult<get_devices::v3::Response> {
18291825
let request = get_devices::v3::Request::new();
18301826

1831-
self.send(request, None).await
1827+
self.send(request).await
18321828
}
18331829

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

1882-
self.send(request, None).await
1878+
self.send(request).await
18831879
}
18841880

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

1902-
self.send(request, None).await
1898+
self.send(request).await
19031899
}
19041900

19051901
/// Synchronize the client's state with the latest state on the server.
@@ -2023,7 +2019,7 @@ impl Client {
20232019
request_config.timeout += timeout;
20242020
}
20252021

2026-
let response = self.send(request, Some(request_config)).await?;
2022+
let response = self.send(request).with_request_config(request_config).await?;
20272023
let next_batch = response.next_batch.clone();
20282024
let response = self.process_sync(response).await?;
20292025

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

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

0 commit comments

Comments
 (0)