From 8a5a129a878589c408e4fa2d88f487cb1d73dc1f Mon Sep 17 00:00:00 2001 From: CramMK Date: Wed, 15 Jan 2020 14:59:02 +0000 Subject: [PATCH] Clean dependencies Delete useless dependencies in files Sort dependencies --- examples/auth.rs | 13 +--- src/net/protos/wire_websocket.rs | 2 +- src/net/wire_api/auth.rs | 23 +++--- src/net/wire_api/conversations.rs | 125 +++++++++--------------------- src/net/wire_api/members.rs | 5 +- src/net/wire_api/mod.rs | 4 +- src/net/wire_api/self_info.rs | 42 +++++----- src/net/wire_api/wire_client.rs | 48 +++++++----- 8 files changed, 108 insertions(+), 154 deletions(-) diff --git a/examples/auth.rs b/examples/auth.rs index ec52ca3..b79a833 100644 --- a/examples/auth.rs +++ b/examples/auth.rs @@ -1,12 +1,6 @@ use std::env; -use irssi_wire::net::wire_api::{ - wire_client::WireClient, - auth::Config, - conversations::Conversations, - members::Members, - self_info::SelfInfo, -}; +use irssi_wire::net::wire_api::{auth::Config, wire_client::WireClient}; #[tokio::main] async fn main() -> Result<(), Box> { @@ -16,10 +10,7 @@ async fn main() -> Result<(), Box> { Config::Default, ); - let auth_response = client - .authentification() - .await - .unwrap(); + let _auth_response = client.authentification().await.unwrap(); dbg!(client); diff --git a/src/net/protos/wire_websocket.rs b/src/net/protos/wire_websocket.rs index 76294ff..52bd394 100644 --- a/src/net/protos/wire_websocket.rs +++ b/src/net/protos/wire_websocket.rs @@ -24,7 +24,7 @@ use protobuf::ProtobufEnum as ProtobufEnum_imported_for_functions; /// Generated files are compatible only with the same version /// of protobuf runtime. -const _PROTOBUF_VERSION_CHECK: () = ::protobuf::VERSION_2_10_0; +const _PROTOBUF_VERSION_CHECK: () = ::protobuf::VERSION_2_10_1; #[derive(PartialEq,Clone,Default)] pub struct GenericMessage { diff --git a/src/net/wire_api/auth.rs b/src/net/wire_api/auth.rs index 7fc5662..245c7bb 100644 --- a/src/net/wire_api/auth.rs +++ b/src/net/wire_api/auth.rs @@ -1,11 +1,11 @@ -use hyper::body::Buf; -use hyper::client::connect::HttpConnector; -use hyper::{header, Body, Client, Method, Request}; -use hyper_tls::HttpsConnector; +// Dependencies +use hyper::{ + body::Buf, + {header, Body, Method, Request}, +}; use serde::{Deserialize, Serialize}; -use crate::net::wire_api::wire_client::{WireClient}; -use crate::net::wire_api::error::ApiError; +use crate::net::wire_api::{error::ApiError, wire_client::WireClient}; #[derive(Debug, Clone)] pub struct ConnectionUrls { @@ -56,11 +56,11 @@ impl WireClient { pub async fn authentification(&mut self) -> Result<(), ApiError> { let endpoint = [ self.config.fetch().rest_url, - String::from("/login?persist=true") - ].concat(); + String::from("/login?persist=true"), + ] + .concat(); - let json = serde_json::to_string(&self.login_info) - .unwrap(); + let json = serde_json::to_string(&self.login_info).unwrap(); let auth_request = Request::builder() .method(Method::POST) @@ -70,7 +70,8 @@ impl WireClient { .body(Body::from(json)) .unwrap(); - let auth_response = self.client + let auth_response = self + .client .request(auth_request) .await .map_err(|e| ApiError::HttpError(e))?; diff --git a/src/net/wire_api/conversations.rs b/src/net/wire_api/conversations.rs index a74a5c9..17624ba 100644 --- a/src/net/wire_api/conversations.rs +++ b/src/net/wire_api/conversations.rs @@ -1,116 +1,67 @@ -use hyper::body::Buf; -use hyper::client::connect::HttpConnector; -use hyper::{header, Body, Client, Method, Request}; -use hyper_tls::HttpsConnector; +// Dependencies use serde::{Deserialize, Serialize}; -use crate::net::wire_api::{ - wire_client::WireClient, - auth::AuthResponse, - error::ApiError}; - #[derive(Serialize, Deserialize, Debug)] pub struct Conversations { -has_more: Option, -conversations: Option>, + has_more: Option, + conversations: Option>, } #[derive(Serialize, Deserialize, Debug)] pub struct Conversation { -access: Option, -creator: Option, -access_role: Option, -members: Option, -name: Option, -team: Option, -id: Option, -r#type: Option, -receipt_mode: Option, -last_event_time: String, -message_timer: Option, -last_event: Option, + access: Option, + creator: Option, + access_role: Option, + members: Option, + name: Option, + team: Option, + id: Option, + r#type: Option, + receipt_mode: Option, + last_event_time: String, + message_timer: Option, + last_event: Option, } #[derive(Serialize, Deserialize, Debug)] pub struct Access { -#[serde(rename = "0")] -_0: Option, + #[serde(rename = "0")] + _0: Option, } #[derive(Serialize, Deserialize, Debug)] pub struct ConversationMembers { -#[serde(rename = "self")] -self_: Option, -others: Option>, + #[serde(rename = "self")] + self_: Option, + others: Option>, } #[derive(Serialize, Deserialize, Debug)] pub struct Member { -hidden_ref: Option, -stauts: Option, -service: Option, -otr_muted_ref: Option, -conversation_role: Option, -status_time: Option, -hidden: Option, -status_ref: Option, -id: Option, -otr_archived: Option, -otr_muted_status: Option, -otr_muted: Option, -otr_archived_ref: Option, + hidden_ref: Option, + stauts: Option, + service: Option, + otr_muted_ref: Option, + conversation_role: Option, + status_time: Option, + hidden: Option, + status_ref: Option, + id: Option, + otr_archived: Option, + otr_muted_status: Option, + otr_muted: Option, + otr_archived_ref: Option, } #[derive(Serialize, Deserialize, Debug)] pub struct OtherMember { -status: Option, -conversation_role: Option, -id: Option, + status: Option, + conversation_role: Option, + id: Option, } #[derive(Serialize, Deserialize, Debug)] pub struct ServiceRef { -id: String, -provider: String, + id: String, + provider: String, } - -// TODO delete this -/* -impl WireClient { - pub async fn fetch_conversations( - &mut self, - auth_response: &AuthResponse, -) -> Result { - let endpoint = [ - self.config.fetch().rest_url, - String::from("/conversations?size=500"), - ] - .concat(); - let auth_token = auth_response.token_string(); - - let fetch_request = Request::builder() - .method(Method::GET) - .uri(endpoint) - .header(header::CONTENT_TYPE, "application/json") - .header(header::USER_AGENT, self.user_agent) - .header(header::AUTHORIZATION, auth_token) - .body(Body::empty()) - .unwrap(); - - let fetch_result = self - .client - .request(fetch_request) - .await - .map_err(|e| ApiError::HttpError(e))?; - - let fetch_body = hyper::body::aggregate(fetch_result) - .await - .map_err(|e| ApiError::HttpError(e))?; - - let fetch_response = serde_json::from_reader(fetch_body.bytes()) - .map_err(|e| ApiError::JsonParseError(Box::new(e)))?; - - Ok(fetch_response) - } -} -*/ diff --git a/src/net/wire_api/members.rs b/src/net/wire_api/members.rs index d1c734b..c393785 100644 --- a/src/net/wire_api/members.rs +++ b/src/net/wire_api/members.rs @@ -1,6 +1,5 @@ -use serde::{Serialize, Deserialize}; - -use crate::net::wire_api::{error::ApiError}; +// Dependencies +use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize, Debug)] pub struct Members { diff --git a/src/net/wire_api/mod.rs b/src/net/wire_api/mod.rs index e3db7ca..30542fc 100644 --- a/src/net/wire_api/mod.rs +++ b/src/net/wire_api/mod.rs @@ -1,7 +1,7 @@ pub mod error; -pub mod wire_client; pub mod auth; -pub mod self_info; pub mod conversations; pub mod members; +pub mod self_info; +pub mod wire_client; diff --git a/src/net/wire_api/self_info.rs b/src/net/wire_api/self_info.rs index 354d65c..b010566 100644 --- a/src/net/wire_api/self_info.rs +++ b/src/net/wire_api/self_info.rs @@ -1,7 +1,7 @@ -use serde::{Serialize, Deserialize}; +// Dependencies +use serde::{Deserialize, Serialize}; -use crate::net::wire_api::wire_client::WireClient; -use crate::net::wire_api::error::ApiError; +use crate::net::wire_api::{error::ApiError, wire_client::WireClient}; #[derive(Serialize, Deserialize, Debug, Clone)] pub struct SelfInfo { @@ -26,32 +26,34 @@ pub struct UserAssets { impl WireClient { pub async fn fetch_self(&mut self) -> Result<(), ApiError> { - self.self_info = Some(self - .api_get(String::from("/self")) - .await - .unwrap()); + self.self_info = Some(self.api_get(String::from("/self")).await.unwrap()); Ok(()) } pub async fn fetch_conversations(&mut self) -> Result<(), ApiError> { - self.conversations = Some(self - .api_get(String::from("/conversations?size=500")) - .await - .unwrap()); + self.conversations = Some( + self.api_get(String::from("/conversations?size=500")) + .await + .unwrap(), + ); Ok(()) } - pub async fn fetch_members(&mut self) -> Result<(), ApiError>{ - self.members = Some(self - .api_get(String::from( - [String::from("/teams/"), - self.self_info.clone().unwrap().team.unwrap(), - String::from("/members"),].concat() - )) - .await - .unwrap()); + pub async fn fetch_members(&mut self) -> Result<(), ApiError> { + self.members = Some( + self.api_get(String::from( + [ + String::from("/teams/"), + self.self_info.clone().unwrap().team.unwrap(), + String::from("/members"), + ] + .concat(), + )) + .await + .unwrap(), + ); Ok(()) } diff --git a/src/net/wire_api/wire_client.rs b/src/net/wire_api/wire_client.rs index e16933b..307c548 100644 --- a/src/net/wire_api/wire_client.rs +++ b/src/net/wire_api/wire_client.rs @@ -1,15 +1,13 @@ -use hyper::body::Buf; -use hyper::{ - client::connect::HttpConnector, - {header, Body, Client, Method, Request}}; +// Dependencies +use hyper::{body::Buf, client::connect::HttpConnector, header, Body, Client, Method, Request}; use hyper_tls::HttpsConnector; -use crate::net::wire_api::error::ApiError; -use crate::net::wire_api::auth::{LoginInfo, AuthResponse, Config}; use crate::net::wire_api::{ - self_info::SelfInfo, + auth::{AuthResponse, Config, LoginInfo}, conversations::Conversations, + error::ApiError, members::Members, + self_info::SelfInfo, }; #[derive(Debug)] @@ -21,7 +19,7 @@ pub struct WireClient { pub config: Config, pub self_info: Option, pub conversations: Option, - pub members: Option + pub members: Option, } impl WireClient { @@ -42,18 +40,25 @@ impl WireClient { pub async fn api_post( &mut self, endpoint: String, - body_content: String) -> Result - where T: serde::de::DeserializeOwned { + body_content: String, + ) -> Result + where + T: serde::de::DeserializeOwned, + { let api_request = Request::builder() .method(Method::POST) .uri(endpoint) .header(header::CONTENT_TYPE, "application/json") .header(header::USER_AGENT, &self.user_agent) - .header(header::AUTHORIZATION, self.auth_token.as_ref().unwrap().token_string()) + .header( + header::AUTHORIZATION, + self.auth_token.as_ref().unwrap().token_string(), + ) .body(Body::from(body_content)) .unwrap(); - let api_response = self.client + let api_response = self + .client .request(api_request) .await .map_err(|e| ApiError::HttpError(e))?; @@ -68,26 +73,31 @@ impl WireClient { Ok(parsed_json) } - pub async fn api_get( - &mut self, - endpoint: String) -> Result - where T: serde::de::DeserializeOwned { + pub async fn api_get(&mut self, endpoint: String) -> Result + where + T: serde::de::DeserializeOwned, + { let api_request = Request::builder() .method(Method::GET) .uri([self.config.fetch().rest_url, endpoint].concat()) .header(header::CONTENT_TYPE, "application/json") .header(header::USER_AGENT, &self.user_agent) - .header(header::AUTHORIZATION, self.auth_token.as_ref().unwrap().token_string()) + .header( + header::AUTHORIZATION, + self.auth_token.as_ref().unwrap().token_string(), + ) .body(Body::empty()) .unwrap(); - let api_response = self.client + let api_response = self + .client .request(api_request) .await .map_err(|e| ApiError::HttpError(e))?; let body = hyper::body::aggregate(api_response) - .await.map_err(|e| ApiError::HttpError(e))?; + .await + .map_err(|e| ApiError::HttpError(e))?; let parsed_json = serde_json::from_reader(body.bytes()) .map_err(|e| ApiError::JsonParseError(Box::new(e)))?;