client: Add extensive logging

This commit is contained in:
Elias Projahn 2021-04-25 23:36:27 +02:00
parent 2b95dbeadd
commit ec47969aa9
9 changed files with 32 additions and 2 deletions

View file

@ -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"

View file

@ -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<Vec<Ensemble>> {
info!("Get ensembles");
let body = self.get("ensembles").await?;
let ensembles: Vec<Ensemble> = 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(())
}

View file

@ -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<Vec<Instrument>> {
info!("Get instruments");
let body = self.get("instruments").await?;
let instruments: Vec<Instrument> = 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(())
}

View file

@ -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<bool> {
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)),
}

View file

@ -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<Vec<Medium>> {
info!("Get mediums for recording {}", recording_id);
let body = self.get(&format!("recordings/{}/mediums", recording_id)).await?;
let mediums: Vec<Medium> = 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<Vec<Medium>> {
info!("Get mediums by discid {}", discid);
let body = self.get(&format!("discids/{}/mediums", discid)).await?;
let mediums: Vec<Medium> = 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(())
}

View file

@ -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<Vec<Person>> {
info!("Get persons");
let body = self.get("persons").await?;
let persons: Vec<Person> = 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(())
}

View file

@ -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<Vec<Recording>> {
info!("Get recordings for work {}", work_id);
let body = self.get(&format!("works/{}/recordings", work_id)).await?;
let recordings: Vec<Recording> = 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(())
}

View file

@ -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<Captcha> {
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<bool> {
// 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))

View file

@ -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<Vec<Work>> {
info!("Get works by composer {}", composer_id);
let body = self.get(&format!("persons/{}/works", composer_id)).await?;
let works: Vec<Work> = 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(())
}