allow to assign automatically current user on new work package

This commit is contained in:
2024-03-15 23:10:21 +01:00
parent 8da5d6ed87
commit 34f6eac006
17 changed files with 310 additions and 92 deletions

View File

@@ -1,41 +1,80 @@
use crate::config::OpenProjectConfig;
use crate::error::GeneralError;
use crate::openproject::work::{WorkPackageWriter, WorkPackage};
use crate::openproject::work::{WorkPackage, WorkPackageWriter};
use reqwest::Response;
use serde::Deserialize;
#[derive(Deserialize, Debug)]
pub(crate) struct Error {
description: String,
pub(crate) description: String,
}
impl From<reqwest::Error> for Error {
fn from(value: reqwest::Error) -> Self {
Error {
description: format!("Error while connecting to openproject instance: {}", value)
description: format!("Error while connecting to openproject instance: {}", value),
}
}
}
impl From<Error> for GeneralError {
fn from(value: Error) -> GeneralError {
GeneralError{description: value.description}
GeneralError {
description: value.description,
}
}
}
pub(crate) struct Client {
base_url: String,
token: String,
pub(crate) base_url: String,
pub(crate) token: String,
}
pub async fn handle_response_status<T: for<'de> serde::Deserialize<'de>>(
response: Response,
error_message: &str,
) -> Result<T, Error> {
if !response.status().is_success() {
let status = response.status().to_string().clone();
let content = response
.text()
.await
.unwrap_or_else(|_| "Impossible to decode".into())
.clone();
return Err(Error {
description: format!(
"{}, status: {}, content: {}",
error_message, status, content
),
});
}
let t = response.json().await?;
Ok(t)
}
impl Client {
pub fn from_config(config: &OpenProjectConfig) -> Client {
Client{base_url: config.base_url.clone(), token: config.token.clone()}
Client {
base_url: config.base_url.clone(),
token: config.token.clone(),
}
}
pub async fn create_work_package(&self, work_package: &WorkPackageWriter, project_id: &String) -> Result<WorkPackage, Error> {
pub async fn create_work_package(
&self,
work_package: &WorkPackageWriter,
project_id: &String,
) -> Result<WorkPackage, Error> {
let client = reqwest::Client::new();
let work_package: WorkPackage = client
.post(format!("{}/api/v3/projects/{}/work_packages", self.base_url, project_id))
.post(format!(
"{}/api/v3/projects/{}/work_packages",
self.base_url, project_id
))
.basic_auth("apikey", Some(&self.token))
.json(&work_package)
.send()