matrix_sdk: Create HttpSend trait to abstract sending requests

master
Devin Ragotzy 2020-08-11 08:06:43 -04:00
parent 72168ce084
commit fba3298162
3 changed files with 63 additions and 0 deletions

View File

@ -17,6 +17,7 @@ encryption = ["matrix-sdk-base/encryption"]
sqlite-cryptostore = ["matrix-sdk-base/sqlite-cryptostore"]
[dependencies]
async-trait = "0.1.36"
http = "0.2.1"
# FIXME: Revert to regular dependency once 0.10.8 or 0.11.0 is released
reqwest = { git = "https://github.com/seanmonstar/reqwest", rev = "dd8441fd23dae6ffb79b4cea2862e5bca0c59743" }

View File

@ -0,0 +1,60 @@
// Copyright 2020 The Matrix.org Foundation C.I.C.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::fmt::Debug;
use matrix_sdk_common_macros::async_trait;
use reqwest::Response;
use crate::Result;
/// Abstraction around the http layer. The allows implementors to use different
/// http libraries.
#[async_trait]
pub trait HttpSend: Sync + Send + Debug {
/// The method abstracting sending request types and receiving response types.
///
/// This is called by the client every time it wants to send anything to a homeserver.
///
/// # Arguments
///
/// * `request` - The http request that has been converted from a ruma `Request`.
///
/// # Returns
///
/// A `reqwest::Response` that will be converted to a ruma `Response` in the `Client`.
///
/// # Examples
///
/// ```ignore
/// use matrix_sdk::HttpSend;
/// use matrix_sdk_common_macros::async_trait;
/// use reqwest::Response;
///
/// #[derive(Debug)]
/// struct TestSend;
///
/// impl HttpSend for TestSend {
/// async fn send_request(&self, request: http::Request<Vec<u8>>) -> Result<Response>
/// // send the request somehow
/// let response = send(request, method, homeserver).await?;
///
/// // reqwest can convert to and from `http::Response` types.
/// Ok(reqwest::Response::from(response))
/// }
/// }
///
/// ```
async fn send_request(&self, request: http::Request<Vec<u8>>) -> Result<Response>;
}

View File

@ -51,6 +51,7 @@ pub use reqwest::header::InvalidHeaderValue;
mod client;
mod error;
mod http_client;
mod http_send;
mod request_builder;
#[cfg(feature = "encryption")]
@ -58,6 +59,7 @@ mod sas;
pub use client::{Client, ClientConfig, SyncSettings};
pub use error::{Error, Result};
pub use http_send::HttpSend;
pub use request_builder::{
MessagesRequestBuilder, RegistrationBuilder, RoomBuilder, RoomListFilterBuilder,
};