From ec47969aa9aabdbc0bc0650f17623d64d6ba8991 Mon Sep 17 00:00:00 2001 From: Elias Projahn Date: Sun, 25 Apr 2021 23:36:27 +0200 Subject: [PATCH] client: Add extensive logging --- client/Cargo.toml | 1 + client/src/ensembles.rs | 3 +++ client/src/instruments.rs | 3 +++ client/src/lib.rs | 5 ++++- client/src/mediums.rs | 4 ++++ client/src/persons.rs | 3 +++ client/src/recordings.rs | 3 +++ client/src/register.rs | 9 ++++++++- client/src/works.rs | 3 +++ 9 files changed, 32 insertions(+), 2 deletions(-) diff --git a/client/Cargo.toml b/client/Cargo.toml index bb552db..d0c7109 100644 --- a/client/Cargo.toml +++ b/client/Cargo.toml @@ -5,6 +5,7 @@ edition = "2018" [dependencies] isahc = "1.1.0" +log = "0.4.14" musicus_database = { version = "0.1.0", path = "../database" } serde = { version = "1.0.117", features = ["derive"] } serde_json = "1.0.59" diff --git a/client/src/ensembles.rs b/client/src/ensembles.rs index 7068bd7..7ff3980 100644 --- a/client/src/ensembles.rs +++ b/client/src/ensembles.rs @@ -1,9 +1,11 @@ use crate::{Client, Result}; +use log::info; use musicus_database::Ensemble; impl Client { /// Get all available ensembles from the server. pub async fn get_ensembles(&self) -> Result> { + info!("Get ensembles"); let body = self.get("ensembles").await?; let ensembles: Vec = serde_json::from_str(&body)?; Ok(ensembles) @@ -11,6 +13,7 @@ impl Client { /// Post a new ensemble to the server. pub async fn post_ensemble(&self, data: &Ensemble) -> Result<()> { + info!("Post ensemble {:?}", data); self.post("ensembles", serde_json::to_string(data)?).await?; Ok(()) } diff --git a/client/src/instruments.rs b/client/src/instruments.rs index 6607b9d..dfb33b1 100644 --- a/client/src/instruments.rs +++ b/client/src/instruments.rs @@ -1,9 +1,11 @@ use crate::{Client, Result}; +use log::info; use musicus_database::Instrument; impl Client { /// Get all available instruments from the server. pub async fn get_instruments(&self) -> Result> { + info!("Get instruments"); let body = self.get("instruments").await?; let instruments: Vec = serde_json::from_str(&body)?; Ok(instruments) @@ -11,6 +13,7 @@ impl Client { /// Post a new instrument to the server. pub async fn post_instrument(&self, data: &Instrument) -> Result<()> { + info!("Post instrument {:?}", data); self.post("instruments", serde_json::to_string(data)?).await?; Ok(()) } diff --git a/client/src/lib.rs b/client/src/lib.rs index 6eab7c6..e7cf3f8 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -1,6 +1,7 @@ use isahc::{AsyncBody, Request, Response}; use isahc::http::StatusCode; use isahc::prelude::*; +use log::info; use serde::Serialize; use std::time::Duration; use std::cell::RefCell; @@ -77,6 +78,8 @@ impl Client { /// Try to login a user with the provided credentials and return, wether the login suceeded. pub async fn login(&self) -> Result { + info!("Login"); + let server_url = self.server_url()?; let data = self.login_data()?; @@ -124,7 +127,7 @@ impl Client { // If authorization failed, try again below. Else, return early. match response.status() { - StatusCode::UNAUTHORIZED => (), + StatusCode::UNAUTHORIZED => info!("Token may be expired"), StatusCode::OK => return Ok(response.text().await?), status_code => return Err(Error::UnexpectedResponse(status_code)), } diff --git a/client/src/mediums.rs b/client/src/mediums.rs index ad55434..255c12d 100644 --- a/client/src/mediums.rs +++ b/client/src/mediums.rs @@ -1,10 +1,12 @@ use crate::{Client, Result}; +use log::info; use musicus_database::Medium; impl Client { /// Get all available mediums from the server, that contain the specified /// recording. pub async fn get_mediums_for_recording(&self, recording_id: &str) -> Result> { + info!("Get mediums for recording {}", recording_id); let body = self.get(&format!("recordings/{}/mediums", recording_id)).await?; let mediums: Vec = serde_json::from_str(&body)?; Ok(mediums) @@ -13,6 +15,7 @@ impl Client { /// Get all available mediums from the server, that match the specified /// DiscID. pub async fn get_mediums_by_discid(&self, discid: &str) -> Result> { + info!("Get mediums by discid {}", discid); let body = self.get(&format!("discids/{}/mediums", discid)).await?; let mediums: Vec = serde_json::from_str(&body)?; Ok(mediums) @@ -20,6 +23,7 @@ impl Client { /// Post a new medium to the server. pub async fn post_medium(&self, data: &Medium) -> Result<()> { + info!("Post medium {:?}", data); self.post("mediums", serde_json::to_string(data)?).await?; Ok(()) } diff --git a/client/src/persons.rs b/client/src/persons.rs index bc14350..4e1ab84 100644 --- a/client/src/persons.rs +++ b/client/src/persons.rs @@ -1,9 +1,11 @@ use crate::{Client, Result}; +use log::info; use musicus_database::Person; impl Client { /// Get all available persons from the server. pub async fn get_persons(&self) -> Result> { + info!("Get persons"); let body = self.get("persons").await?; let persons: Vec = serde_json::from_str(&body)?; Ok(persons) @@ -11,6 +13,7 @@ impl Client { /// Post a new person to the server. pub async fn post_person(&self, data: &Person) -> Result<()> { + info!("Post person {:?}", data); self.post("persons", serde_json::to_string(data)?).await?; Ok(()) } diff --git a/client/src/recordings.rs b/client/src/recordings.rs index 07862a9..21c15e6 100644 --- a/client/src/recordings.rs +++ b/client/src/recordings.rs @@ -1,9 +1,11 @@ use crate::{Client, Result}; +use log::info; use musicus_database::Recording; impl Client { /// Get all available recordings from the server. pub async fn get_recordings_for_work(&self, work_id: &str) -> Result> { + info!("Get recordings for work {}", work_id); let body = self.get(&format!("works/{}/recordings", work_id)).await?; let recordings: Vec = serde_json::from_str(&body)?; Ok(recordings) @@ -11,6 +13,7 @@ impl Client { /// Post a new recording to the server. pub async fn post_recording(&self, data: &Recording) -> Result<()> { + info!("Post recording {:?}", data); self.post("recordings", serde_json::to_string(data)?).await?; Ok(()) } diff --git a/client/src/register.rs b/client/src/register.rs index efba962..a6ab725 100644 --- a/client/src/register.rs +++ b/client/src/register.rs @@ -2,6 +2,7 @@ use crate::{Client, Result}; use isahc::Request; use isahc::http::StatusCode; use isahc::prelude::*; +use log::info; use serde::{Deserialize, Serialize}; use std::time::Duration; @@ -14,7 +15,7 @@ pub struct Captcha { } /// Request body data for user registration. -#[derive(Serialize, Debug, Clone)] +#[derive(Serialize, Clone)] #[serde(rename_all = "camelCase")] pub struct UserRegistration { pub username: String, @@ -27,6 +28,7 @@ pub struct UserRegistration { impl Client { /// Request a new captcha for registration. pub async fn get_captcha(&self) -> Result { + info!("Get captcha"); let body = self.get("captcha").await?; let captcha = serde_json::from_str(&body)?; Ok(captcha) @@ -35,6 +37,11 @@ impl Client { /// Register a new user and return whether the process suceeded. This will /// not store the new login credentials. pub async fn register(&self, data: UserRegistration) -> Result { + // Make sure to not log the password accidentally! + info!("Register user '{}'", data.username); + info!("Captcha ID: {}", data.captcha_id); + info!("Captcha answer: {}", data.answer); + let server_url = self.server_url()?; let response = Request::post(format!("{}/users", server_url)) diff --git a/client/src/works.rs b/client/src/works.rs index d4e5bcd..b59bbc1 100644 --- a/client/src/works.rs +++ b/client/src/works.rs @@ -1,9 +1,11 @@ use crate::{Client, Result}; +use log::info; use musicus_database::Work; impl Client { /// Get all available works from the server. pub async fn get_works(&self, composer_id: &str) -> Result> { + info!("Get works by composer {}", composer_id); let body = self.get(&format!("persons/{}/works", composer_id)).await?; let works: Vec = serde_json::from_str(&body)?; Ok(works) @@ -11,6 +13,7 @@ impl Client { /// Post a new work to the server. pub async fn post_work(&self, data: &Work) -> Result<()> { + info!("Post work {:?}", data); self.post("works", serde_json::to_string(data)?).await?; Ok(()) }