cl-cli/src/openproject/client.rs

49 lines
1.3 KiB
Rust

use crate::config::OpenProjectConfig;
use crate::error::GeneralError;
use crate::openproject::work::{WorkPackageWriter, WorkPackage};
pub(crate) struct Error {
description: String,
}
impl From<reqwest::Error> for Error {
fn from(value: reqwest::Error) -> Self {
Error {
description: format!("Error while connecting to openproject instance: {}", value)
}
}
}
impl From<Error> for GeneralError {
fn from(value: Error) -> GeneralError {
GeneralError{description: value.description}
}
}
pub(crate) struct Client {
base_url: String,
token: String,
}
impl Client {
pub fn from_config(config: &OpenProjectConfig) -> Client {
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> {
let client = reqwest::Client::new();
let work_package: WorkPackage = client
.post(format!("{}/api/v3/projects/{}/work_packages", self.base_url, project_id))
.basic_auth("apikey", Some(&self.token))
.json(&work_package)
.send()
.await?
.json()
.await?;
Ok(work_package)
}
}