async_client: Change the place where we parse the user agent.

master
Damir Jelić 2019-10-24 22:34:58 +02:00
parent b8eb0489c7
commit 8c681c362a
3 changed files with 22 additions and 19 deletions

View File

@ -6,7 +6,7 @@ use std::pin::Pin;
use http::Method as HttpMethod; use http::Method as HttpMethod;
use http::Response as HttpResponse; use http::Response as HttpResponse;
use js_int::UInt; use js_int::UInt;
use reqwest; use reqwest::header::{HeaderValue, InvalidHeaderValue};
use url::Url; use url::Url;
use ruma_api::Endpoint; use ruma_api::Endpoint;
@ -35,7 +35,7 @@ pub struct AsyncClient {
pub struct AsyncClientConfig { pub struct AsyncClientConfig {
proxy: Option<reqwest::Proxy>, proxy: Option<reqwest::Proxy>,
use_sys_proxy: bool, use_sys_proxy: bool,
user_agent: Option<String>, user_agent: Option<HeaderValue>,
disable_ssl_verification: bool, disable_ssl_verification: bool,
} }
@ -68,6 +68,11 @@ impl AsyncClientConfig {
self.disable_ssl_verification = true; self.disable_ssl_verification = true;
self self
} }
pub fn user_agent(mut self, user_agent: &str) -> Result<Self, InvalidHeaderValue> {
self.user_agent = Some(HeaderValue::from_str(user_agent)?);
Ok(self)
}
} }
#[derive(Debug, Default)] #[derive(Debug, Default)]
@ -107,23 +112,16 @@ use api::r0::sync::sync_events;
impl AsyncClient { impl AsyncClient {
/// Creates a new client for making HTTP requests to the given homeserver. /// Creates a new client for making HTTP requests to the given homeserver.
pub fn new(homeserver_url: &str, session: Option<Session>) -> Result<Self, url::ParseError> { pub fn new(homeserver_url: &str, session: Option<Session>) -> Result<Self, Error> {
let homeserver = Url::parse(homeserver_url)?; let config = AsyncClientConfig::new();
let http_client = reqwest::Client::new(); AsyncClient::new_with_config(homeserver_url, session, config)
Ok(Self {
homeserver,
http_client,
base_client: BaseClient::new(session),
event_callbacks: HashMap::new(),
})
} }
pub fn new_with_config( pub fn new_with_config(
homeserver_url: &str, homeserver_url: &str,
session: Option<Session>, session: Option<Session>,
config: AsyncClientConfig, config: AsyncClientConfig,
) -> Result<Self, url::ParseError> { ) -> Result<Self, Error> {
let homeserver = Url::parse(homeserver_url)?; let homeserver = Url::parse(homeserver_url)?;
let http_client = reqwest::Client::builder(); let http_client = reqwest::Client::builder();
@ -146,9 +144,14 @@ impl AsyncClient {
let mut headers = reqwest::header::HeaderMap::new(); 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( headers.insert(
reqwest::header::USER_AGENT, reqwest::header::USER_AGENT,
reqwest::header::HeaderValue::from_static("ruma"), user_agent,
); );
let http_client = http_client.default_headers(headers).build().unwrap(); let http_client = http_client.default_headers(headers).build().unwrap();

View File

@ -3,7 +3,7 @@
use std::error::Error as StdError; use std::error::Error as StdError;
use std::fmt::{Display, Formatter, Result as FmtResult}; use std::fmt::{Display, Formatter, Result as FmtResult};
use http::uri::InvalidUri; use url::ParseError;
use reqwest::Error as ReqwestError; use reqwest::Error as ReqwestError;
use ruma_api::Error as RumaApiError; use ruma_api::Error as RumaApiError;
use serde_json::Error as SerdeJsonError; use serde_json::Error as SerdeJsonError;
@ -41,7 +41,7 @@ pub(crate) enum InnerError {
/// An error at the HTTP layer. /// An error at the HTTP layer.
Reqwest(ReqwestError), Reqwest(ReqwestError),
/// An error when parsing a string as a URI. /// An error when parsing a string as a URI.
Uri(InvalidUri), Uri(ParseError),
/// An error converting between ruma_client_api types and Hyper types. /// An error converting between ruma_client_api types and Hyper types.
RumaApi(RumaApiError), RumaApi(RumaApiError),
/// An error when serializing or deserializing a JSON value. /// An error when serializing or deserializing a JSON value.
@ -50,8 +50,8 @@ pub(crate) enum InnerError {
SerdeUrlEncodedSerialize(SerdeUrlEncodedSerializeError), SerdeUrlEncodedSerialize(SerdeUrlEncodedSerializeError),
} }
impl From<InvalidUri> for Error { impl From<ParseError> for Error {
fn from(error: InvalidUri) -> Self { fn from(error: ParseError) -> Self {
Self(InnerError::Uri(error)) Self(InnerError::Uri(error))
} }
} }

View File

@ -5,8 +5,8 @@
pub use crate::{error::Error, session::Session}; pub use crate::{error::Error, session::Session};
pub use ruma_client_api as api; pub use ruma_client_api as api;
pub use ruma_events as events; pub use ruma_events as events;
pub use reqwest::header::InvalidHeaderValue;
//pub mod api;
mod async_client; mod async_client;
mod base_client; mod base_client;
mod error; mod error;