Skip to content

Commit da946e5

Browse files
authored
ffi: Update Client::get_url to return raw data and to throw on HTTP errors
Client::get_url is there for SDK consumers to be able to use the existing HTTP stack (configuration and all) however in practice it was a bit weird: - Responses came back as strings limiting the types of resource that could be fetched (as well as requiring the string to be converted back to data before handed to a JSON decoder). - HTTP errors weren't being raised and instead you would find the (e.g. 404) error response in the Ok case. This patch fixes both of these issues.
1 parent cba711d commit da946e5

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

bindings/matrix-sdk-ffi/CHANGELOG.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ All notable changes to this project will be documented in this file.
2525

2626
### Breaking changes:
2727

28+
- `Client::get_url` now returns a `Vec<u8>` instead of a `String`. It also throws an error when the
29+
response isn't status code 200 OK, instead of providing the error in the response body.
30+
([#5438](https://github.com/matrix-org/matrix-rust-sdk/pull/5438))
2831
- `RoomPreview::info()` doesn't return a result anymore. All unknown join rules are handled in the
2932
`JoinRule::Custom` variant.
3033
([#5337](https://github.com/matrix-org/matrix-rust-sdk/pull/5337))
@@ -48,7 +51,6 @@ All notable changes to this project will be documented in this file.
4851
- The MSRV has been bumped to Rust 1.88.
4952
([#5431](https://github.com/matrix-org/matrix-rust-sdk/pull/5431))
5053

51-
5254
## [0.13.0] - 2025-07-10
5355

5456
### Features
@@ -115,7 +117,8 @@ Additions:
115117
we can automatically update the UI.
116118
- `Client::get_max_media_upload_size` to get the max size of a request sent to the homeserver so we can tweak our media
117119
uploads by compressing/transcoding the media.
118-
- Add `ClientBuilder::enable_share_history_on_invite` to enable experimental support for sharing encrypted room history on invite, per [MSC4268](https://github.com/matrix-org/matrix-spec-proposals/pull/4268).
120+
- Add `ClientBuilder::enable_share_history_on_invite` to enable experimental support for sharing encrypted room history
121+
on invite, per [MSC4268](https://github.com/matrix-org/matrix-spec-proposals/pull/4268).
119122
([#5141](https://github.com/matrix-org/matrix-rust-sdk/pull/5141))
120123
- Support for adding a Sentry layer to the FFI bindings has been added. Only `tracing` statements with
121124
the field `sentry=true` will be forwarded to Sentry, in addition to default Sentry filters.
@@ -208,7 +211,8 @@ Breaking changes:
208211
- The `dynamic_registrations_file` field of `OidcConfiguration` was removed.
209212
Clients are supposed to re-register with the homeserver for every login.
210213

211-
- `RoomPreview::own_membership_details` is now `RoomPreview::member_with_sender_info`, takes any user id and returns an `Option<RoomMemberWithSenderInfo>`.
214+
- `RoomPreview::own_membership_details` is now `RoomPreview::member_with_sender_info`, takes any user id and returns an
215+
`Option<RoomMemberWithSenderInfo>`.
212216

213217
Additions:
214218

@@ -223,9 +227,11 @@ Additions:
223227
- Add `Timeline::send_thread_reply` for clients that need to start threads
224228
themselves.
225229
([4819](https://github.com/matrix-org/matrix-rust-sdk/pull/4819))
226-
- Add `ClientBuilder::session_pool_max_size`, `::session_cache_size` and `::session_journal_size_limit` to control the stores configuration, especially their memory consumption
230+
- Add `ClientBuilder::session_pool_max_size`, `::session_cache_size` and `::session_journal_size_limit` to control the
231+
stores configuration, especially their memory consumption
227232
([#4870](https://github.com/matrix-org/matrix-rust-sdk/pull/4870/))
228233
- Add `ClientBuilder::system_is_memory_constrained` to indicate that the system
229234
has less memory available than the current standard
230235
([#4894](https://github.com/matrix-org/matrix-rust-sdk/pull/4894))
231-
- Add `Room::member_with_sender_info` to get both a room member's info and for the user who sent the `m.room.member` event the `RoomMember` is based on.
236+
- Add `Room::member_with_sender_info` to get both a room member's info and for the user who sent the `m.room.member`
237+
event the `RoomMember` is based on.

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -775,11 +775,24 @@ impl Client {
775775
}
776776
}
777777

778-
/// Allows generic GET requests to be made through the SDKs internal HTTP
779-
/// client
780-
pub async fn get_url(&self, url: String) -> Result<String, ClientError> {
781-
let http_client = self.inner.http_client();
782-
Ok(http_client.get(url).send().await?.text().await?)
778+
/// Allows generic GET requests to be made through the SDK's internal HTTP
779+
/// client. This is useful when the caller's native HTTP client wouldn't
780+
/// have the same configuration (such as certificates, proxies, etc.) This
781+
/// method returns the raw bytes of the response, so that any kind of
782+
/// resource can be fetched including images, files, etc.
783+
///
784+
/// Note: When an HTTP error occurs, the error response can be found in the
785+
/// `ClientError::Generic`'s `details` field.
786+
pub async fn get_url(&self, url: String) -> Result<Vec<u8>, ClientError> {
787+
let response = self.inner.http_client().get(url).send().await?;
788+
if response.status().is_success() {
789+
Ok(response.bytes().await?.into())
790+
} else {
791+
Err(ClientError::Generic {
792+
msg: response.status().to_string(),
793+
details: response.text().await.ok(),
794+
})
795+
}
783796
}
784797

785798
/// Empty the server version and unstable features cache.

0 commit comments

Comments
 (0)