Move crates to toplevel directory

This commit is contained in:
Elias Projahn 2021-02-07 12:53:25 +01:00
parent d16961efa8
commit 0ffe68e04f
127 changed files with 15 additions and 13 deletions

11
client/Cargo.toml Normal file
View file

@ -0,0 +1,11 @@
[package]
name = "musicus_client"
version = "0.1.0"
edition = "2018"
[dependencies]
isahc = "1.1.0"
musicus_database = { version = "0.1.0", path = "../database" }
serde = { version = "1.0.117", features = ["derive"] }
serde_json = "1.0.59"
thiserror = "1.0.23"

17
client/src/ensembles.rs Normal file
View file

@ -0,0 +1,17 @@
use crate::{Client, Result};
use musicus_database::Ensemble;
impl Client {
/// Get all available ensembles from the server.
pub async fn get_ensembles(&self) -> Result<Vec<Ensemble>> {
let body = self.get("ensembles").await?;
let ensembles: Vec<Ensemble> = serde_json::from_str(&body)?;
Ok(ensembles)
}
/// Post a new ensemble to the server.
pub async fn post_ensemble(&self, data: &Ensemble) -> Result<()> {
self.post("ensembles", serde_json::to_string(data)?).await?;
Ok(())
}
}

36
client/src/error.rs Normal file
View file

@ -0,0 +1,36 @@
use isahc::http::StatusCode;
/// An error within the client.
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("The users login credentials were wrong.")]
LoginFailed,
#[error("The user has to be logged in to perform this action.")]
Unauthorized,
#[error("The user is not allowed to perform this action.")]
Forbidden,
#[error("The server returned an unexpected status code: {0}.")]
UnexpectedResponse(StatusCode),
#[error("A networking error happened.")]
NetworkError(#[from] isahc::Error),
#[error("A networking error happened.")]
HttpError(#[from] isahc::http::Error),
#[error("An error happened when serializing/deserializing.")]
SerdeError(#[from] serde_json::Error),
#[error("An IO error happened.")]
IoError(#[from] std::io::Error),
#[error("An error happened: {0}")]
Other(&'static str),
}
pub type Result<T> = std::result::Result<T, Error>;

17
client/src/instruments.rs Normal file
View file

@ -0,0 +1,17 @@
use crate::{Client, Result};
use musicus_database::Instrument;
impl Client {
/// Get all available instruments from the server.
pub async fn get_instruments(&self) -> Result<Vec<Instrument>> {
let body = self.get("instruments").await?;
let instruments: Vec<Instrument> = serde_json::from_str(&body)?;
Ok(instruments)
}
/// Post a new instrument to the server.
pub async fn post_instrument(&self, data: &Instrument) -> Result<()> {
self.post("instruments", serde_json::to_string(data)?).await?;
Ok(())
}
}

176
client/src/lib.rs Normal file
View file

@ -0,0 +1,176 @@
use isahc::{AsyncBody, Request, Response};
use isahc::http::StatusCode;
use isahc::prelude::*;
use serde::Serialize;
use std::time::Duration;
use std::cell::RefCell;
pub mod ensembles;
pub use ensembles::*;
pub mod error;
pub use error::*;
pub mod instruments;
pub use instruments::*;
pub mod mediums;
pub use mediums::*;
pub mod persons;
pub use persons::*;
pub mod recordings;
pub use recordings::*;
pub mod register;
pub use register::*;
pub mod works;
pub use works::*;
/// Credentials used for login.
#[derive(Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct LoginData {
pub username: String,
pub password: String,
}
/// A client for accessing the Wolfgang API.
pub struct Client {
server_url: RefCell<Option<String>>,
login_data: RefCell<Option<LoginData>>,
token: RefCell<Option<String>>,
}
impl Client {
/// Create a new client.
pub fn new() -> Self {
Self {
server_url: RefCell::new(None),
login_data: RefCell::new(None),
token: RefCell::new(None),
}
}
/// Set the URL of the Musicus server to connect to.
pub fn set_server_url(&self, url: &str) {
self.server_url.replace(Some(url.to_owned()));
}
/// Get the currently set server URL.
pub fn get_server_url(&self) -> Option<String> {
self.server_url.borrow().clone()
}
/// Set the user credentials to use.
pub fn set_login_data(&self, data: Option<LoginData>) {
self.login_data.replace(data);
self.token.replace(None);
}
/// Get the currently stored login credentials.
pub fn get_login_data(&self) -> Option<LoginData> {
self.login_data.borrow().clone()
}
/// Try to login a user with the provided credentials and return, wether the login suceeded.
pub async fn login(&self) -> Result<bool> {
let server_url = self.server_url()?;
let data = self.login_data()?;
let request = Request::post(format!("{}/login", server_url))
.timeout(Duration::from_secs(10))
.header("Content-Type", "application/json")
.body(serde_json::to_string(&data)?)?;
let mut response = isahc::send_async(request).await?;
let success = match response.status() {
StatusCode::OK => {
let token = response.text().await?;
self.token.replace(Some(token));
true
}
StatusCode::UNAUTHORIZED => false,
status_code => Err(Error::UnexpectedResponse(status_code))?,
};
Ok(success)
}
/// Make an unauthenticated get request to the server.
async fn get(&self, url: &str) -> Result<String> {
let server_url = self.server_url()?;
let mut response = Request::get(format!("{}/{}", server_url, url))
.timeout(Duration::from_secs(10))
.body(())?
.send_async()
.await?;
let body = response.text().await?;
Ok(body)
}
/// Make an authenticated post request to the server.
async fn post(&self, url: &str, body: String) -> Result<String> {
let body = if self.token.borrow().is_some() {
let mut response = self.post_priv(url, body.clone()).await?;
// Try one more time (maybe the token was expired)
if response.status() == StatusCode::UNAUTHORIZED {
if self.login().await? {
response = self.post_priv(url, body).await?;
} else {
Err(Error::LoginFailed)?;
}
}
response.text().await?
} else {
let mut response = if self.login().await? {
self.post_priv(url, body).await?
} else {
Err(Error::LoginFailed)?
};
response.text().await?
};
Ok(body)
}
/// Post something to the server assuming there is a valid login token.
async fn post_priv(&self, url: &str, body: String) -> Result<Response<AsyncBody>> {
let server_url = self.server_url()?;
let token = self.token()?;
let response = Request::post(format!("{}/{}", server_url, url))
.timeout(Duration::from_secs(10))
.header("Authorization", format!("Bearer {}", token))
.header("Content-Type", "application/json")
.body(body)?
.send_async()
.await?;
Ok(response)
}
/// Require the server URL to be set.
fn server_url(&self) -> Result<String> {
self.get_server_url().ok_or(Error::Other("The server URL is not available!"))
}
/// Require the login data to be set.
fn login_data(&self) -> Result<LoginData> {
self.get_login_data().ok_or(Error::Other("The login data is unset!"))
}
/// Require a login token to be set.
fn token(&self) -> Result<String> {
self.token.borrow().clone().ok_or(Error::Other("No login token found!"))
}
}

26
client/src/mediums.rs Normal file
View file

@ -0,0 +1,26 @@
use crate::{Client, Result};
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>> {
let body = self.get(&format!("recordings/{}/mediums", recording_id)).await?;
let mediums: Vec<Medium> = serde_json::from_str(&body)?;
Ok(mediums)
}
/// 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>> {
let body = self.get(&format!("discids/{}/mediums", discid)).await?;
let mediums: Vec<Medium> = serde_json::from_str(&body)?;
Ok(mediums)
}
/// Post a new medium to the server.
pub async fn post_medium(&self, data: &Medium) -> Result<()> {
self.post("mediums", serde_json::to_string(data)?).await?;
Ok(())
}
}

17
client/src/persons.rs Normal file
View file

@ -0,0 +1,17 @@
use crate::{Client, Result};
use musicus_database::Person;
impl Client {
/// Get all available persons from the server.
pub async fn get_persons(&self) -> Result<Vec<Person>> {
let body = self.get("persons").await?;
let persons: Vec<Person> = serde_json::from_str(&body)?;
Ok(persons)
}
/// Post a new person to the server.
pub async fn post_person(&self, data: &Person) -> Result<()> {
self.post("persons", serde_json::to_string(data)?).await?;
Ok(())
}
}

17
client/src/recordings.rs Normal file
View file

@ -0,0 +1,17 @@
use crate::{Client, Result};
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>> {
let body = self.get(&format!("works/{}/recordings", work_id)).await?;
let recordings: Vec<Recording> = serde_json::from_str(&body)?;
Ok(recordings)
}
/// Post a new recording to the server.
pub async fn post_recording(&self, data: &Recording) -> Result<()> {
self.post("recordings", serde_json::to_string(data)?).await?;
Ok(())
}
}

49
client/src/register.rs Normal file
View file

@ -0,0 +1,49 @@
use crate::{Client, Result};
use isahc::Request;
use isahc::http::StatusCode;
use isahc::prelude::*;
use serde::{Deserialize, Serialize};
use std::time::Duration;
/// Response body data for captcha requests.
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Captcha {
pub id: String,
pub question: String,
}
/// Request body data for user registration.
#[derive(Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct UserRegistration {
pub username: String,
pub password: String,
pub email: Option<String>,
pub captcha_id: String,
pub answer: String,
}
impl Client {
/// Request a new captcha for registration.
pub async fn get_captcha(&self) -> Result<Captcha> {
let body = self.get("captcha").await?;
let captcha = serde_json::from_str(&body)?;
Ok(captcha)
}
/// 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> {
let server_url = self.server_url()?;
let response = Request::post(format!("{}/users", server_url))
.timeout(Duration::from_secs(10))
.header("Content-Type", "application/json")
.body(serde_json::to_string(&data)?)?
.send_async()
.await?;
Ok(response.status() == StatusCode::OK)
}
}

17
client/src/works.rs Normal file
View file

@ -0,0 +1,17 @@
use crate::{Client, Result};
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>> {
let body = self.get(&format!("persons/{}/works", composer_id)).await?;
let works: Vec<Work> = serde_json::from_str(&body)?;
Ok(works)
}
/// Post a new work to the server.
pub async fn post_work(&self, data: &Work) -> Result<()> {
self.post("works", serde_json::to_string(data)?).await?;
Ok(())
}
}