async_client: Change the place where we parse the user agent.
parent
b8eb0489c7
commit
8c681c362a
|
@ -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<reqwest::Proxy>,
|
||||
use_sys_proxy: bool,
|
||||
user_agent: Option<String>,
|
||||
user_agent: Option<HeaderValue>,
|
||||
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, InvalidHeaderValue> {
|
||||
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<Session>) -> Result<Self, url::ParseError> {
|
||||
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<Session>) -> Result<Self, Error> {
|
||||
let config = AsyncClientConfig::new();
|
||||
AsyncClient::new_with_config(homeserver_url, session, config)
|
||||
}
|
||||
|
||||
pub fn new_with_config(
|
||||
homeserver_url: &str,
|
||||
session: Option<Session>,
|
||||
config: AsyncClientConfig,
|
||||
) -> Result<Self, url::ParseError> {
|
||||
) -> Result<Self, Error> {
|
||||
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();
|
||||
|
|
|
@ -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<InvalidUri> for Error {
|
||||
fn from(error: InvalidUri) -> Self {
|
||||
impl From<ParseError> for Error {
|
||||
fn from(error: ParseError) -> Self {
|
||||
Self(InnerError::Uri(error))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue