From 9a325a4505998a29c0f2bc24ea2fcf0fb69762f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Tue, 11 Aug 2020 17:25:33 +0200 Subject: [PATCH] matrix-sdk: Move the HttpSend trait into the http_client file. --- matrix_sdk/src/client.rs | 28 ++++++++-------- matrix_sdk/src/http_client.rs | 49 +++++++++++++++++++++++++--- matrix_sdk/src/http_send.rs | 60 ----------------------------------- matrix_sdk/src/lib.rs | 3 +- 4 files changed, 60 insertions(+), 80 deletions(-) delete mode 100644 matrix_sdk/src/http_send.rs diff --git a/matrix_sdk/src/client.rs b/matrix_sdk/src/client.rs index 3a8494eb..d6f43919 100644 --- a/matrix_sdk/src/client.rs +++ b/matrix_sdk/src/client.rs @@ -19,11 +19,22 @@ use std::{ collections::HashMap, convert::{TryFrom, TryInto}, fmt::{self, Debug}, + future::Future, path::Path, result::Result as StdResult, sync::Arc, }; +use futures_timer::Delay as sleep; +use reqwest::header::{HeaderValue, InvalidHeaderValue}; +use url::Url; + +#[cfg(feature = "encryption")] +use tracing::{debug, warn}; +use tracing::{error, info, instrument}; + +use matrix_sdk_base::{BaseClient, BaseClientConfig, Room, Session, StateStore}; + #[cfg(feature = "encryption")] use matrix_sdk_common::api::r0::to_device::send_event_to_device::Request as ToDeviceRequest; use matrix_sdk_common::{ @@ -36,28 +47,17 @@ use matrix_sdk_common::{ FromHttpResponseError, }; -use futures_timer::Delay as sleep; -use std::future::Future; -#[cfg(feature = "encryption")] -use tracing::{debug, warn}; -use tracing::{error, info, instrument}; - -use reqwest::header::{HeaderValue, InvalidHeaderValue}; -use url::Url; - use crate::{ + api, events::{room::message::MessageEventContent, EventType}, - http_client::DefaultHttpClient, + http_client::{DefaultHttpClient, HttpClient, HttpSend}, identifiers::{EventId, RoomId, RoomIdOrAliasId, UserId}, - Endpoint, HttpSend, + Endpoint, EventEmitter, Result, }; #[cfg(feature = "encryption")] use crate::{identifiers::DeviceId, sas::Sas}; -use crate::{api, http_client::HttpClient, EventEmitter, Result}; -use matrix_sdk_base::{BaseClient, BaseClientConfig, Room, Session, StateStore}; - const DEFAULT_SYNC_TIMEOUT: Duration = Duration::from_secs(30); /// An async/await enabled Matrix client. diff --git a/matrix_sdk/src/http_client.rs b/matrix_sdk/src/http_client.rs index 45793a23..2c48a336 100644 --- a/matrix_sdk/src/http_client.rs +++ b/matrix_sdk/src/http_client.rs @@ -12,16 +12,57 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::{convert::TryFrom, sync::Arc}; +use std::{convert::TryFrom, fmt::Debug, sync::Arc}; use http::{HeaderValue, Method as HttpMethod, Response as HttpResponse}; -use matrix_sdk_common::{locks::RwLock, FromHttpResponseError}; -use matrix_sdk_common_macros::async_trait; use reqwest::{Client, Response}; use tracing::trace; use url::Url; -use crate::{ClientConfig, Endpoint, Error, HttpSend, Result, Session}; +use matrix_sdk_common::{locks::RwLock, FromHttpResponseError}; +use matrix_sdk_common_macros::async_trait; + +use crate::{ClientConfig, Endpoint, Error, Result, Session}; + +/// 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>) -> Result + /// // 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>) -> Result; +} #[derive(Clone, Debug)] pub(crate) struct HttpClient { diff --git a/matrix_sdk/src/http_send.rs b/matrix_sdk/src/http_send.rs deleted file mode 100644 index f3ba9fb0..00000000 --- a/matrix_sdk/src/http_send.rs +++ /dev/null @@ -1,60 +0,0 @@ -// 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>) -> Result - /// // 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>) -> Result; -} diff --git a/matrix_sdk/src/lib.rs b/matrix_sdk/src/lib.rs index edd20fc1..4d1fe0ea 100644 --- a/matrix_sdk/src/lib.rs +++ b/matrix_sdk/src/lib.rs @@ -51,7 +51,6 @@ pub use reqwest::header::InvalidHeaderValue; mod client; mod error; mod http_client; -mod http_send; mod request_builder; #[cfg(feature = "encryption")] @@ -59,7 +58,7 @@ mod sas; pub use client::{Client, ClientConfig, SyncSettings}; pub use error::{Error, Result}; -pub use http_send::HttpSend; +pub use http_client::HttpSend; pub use request_builder::{ MessagesRequestBuilder, RegistrationBuilder, RoomBuilder, RoomListFilterBuilder, };