diff --git a/src/async_client.rs b/src/async_client.rs index c2aa3eaa..269ab520 100644 --- a/src/async_client.rs +++ b/src/async_client.rs @@ -6,7 +6,7 @@ use std::pin::Pin; use http::Method as HttpMethod; use http::Response as HttpResponse; use js_int::UInt; -use reqwest; +use reqwest::header::{HeaderValue, InvalidHeaderValue}; use url::Url; use ruma_api::Endpoint; @@ -35,7 +35,7 @@ pub struct AsyncClient { pub struct AsyncClientConfig { proxy: Option, use_sys_proxy: bool, - user_agent: Option, + user_agent: Option, disable_ssl_verification: bool, } @@ -68,6 +68,11 @@ impl AsyncClientConfig { self.disable_ssl_verification = true; self } + + pub fn user_agent(mut self, user_agent: &str) -> Result { + self.user_agent = Some(HeaderValue::from_str(user_agent)?); + Ok(self) + } } #[derive(Debug, Default)] @@ -107,23 +112,16 @@ use api::r0::sync::sync_events; impl AsyncClient { /// Creates a new client for making HTTP requests to the given homeserver. - pub fn new(homeserver_url: &str, session: Option) -> Result { - let homeserver = Url::parse(homeserver_url)?; - let http_client = reqwest::Client::new(); - - Ok(Self { - homeserver, - http_client, - base_client: BaseClient::new(session), - event_callbacks: HashMap::new(), - }) + pub fn new(homeserver_url: &str, session: Option) -> Result { + let config = AsyncClientConfig::new(); + AsyncClient::new_with_config(homeserver_url, session, config) } pub fn new_with_config( homeserver_url: &str, session: Option, config: AsyncClientConfig, - ) -> Result { + ) -> Result { let homeserver = Url::parse(homeserver_url)?; let http_client = reqwest::Client::builder(); @@ -146,9 +144,14 @@ impl AsyncClient { let mut headers = reqwest::header::HeaderMap::new(); + let user_agent = match config.user_agent { + Some(a) => a, + None => HeaderValue::from_static("nio-rust"), + }; + headers.insert( reqwest::header::USER_AGENT, - reqwest::header::HeaderValue::from_static("ruma"), + user_agent, ); let http_client = http_client.default_headers(headers).build().unwrap(); diff --git a/src/error.rs b/src/error.rs index 15268732..61a5e80e 100644 --- a/src/error.rs +++ b/src/error.rs @@ -3,7 +3,7 @@ use std::error::Error as StdError; use std::fmt::{Display, Formatter, Result as FmtResult}; -use http::uri::InvalidUri; +use url::ParseError; use reqwest::Error as ReqwestError; use ruma_api::Error as RumaApiError; use serde_json::Error as SerdeJsonError; @@ -41,7 +41,7 @@ pub(crate) enum InnerError { /// An error at the HTTP layer. Reqwest(ReqwestError), /// An error when parsing a string as a URI. - Uri(InvalidUri), + Uri(ParseError), /// An error converting between ruma_client_api types and Hyper types. RumaApi(RumaApiError), /// An error when serializing or deserializing a JSON value. @@ -50,8 +50,8 @@ pub(crate) enum InnerError { SerdeUrlEncodedSerialize(SerdeUrlEncodedSerializeError), } -impl From for Error { - fn from(error: InvalidUri) -> Self { +impl From for Error { + fn from(error: ParseError) -> Self { Self(InnerError::Uri(error)) } } diff --git a/src/lib.rs b/src/lib.rs index 2febee9a..fc4c0e4d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,8 +5,8 @@ pub use crate::{error::Error, session::Session}; pub use ruma_client_api as api; pub use ruma_events as events; +pub use reqwest::header::InvalidHeaderValue; -//pub mod api; mod async_client; mod base_client; mod error;