allow multiple instances of gitlab to be configured

This commit is contained in:
2024-03-17 21:34:03 +01:00
parent 34f6eac006
commit df71e1073c
8 changed files with 80 additions and 39 deletions

View File

@@ -3,22 +3,23 @@ use crate::error::GeneralError;
use crate::openproject::work::{WorkPackage, WorkPackageWriter};
use reqwest::Response;
use serde::Deserialize;
use std::error::Error;
#[derive(Deserialize, Debug)]
pub(crate) struct Error {
pub(crate) struct OpenProjectError {
pub(crate) description: String,
}
impl From<reqwest::Error> for Error {
impl From<reqwest::Error> for OpenProjectError {
fn from(value: reqwest::Error) -> Self {
Error {
OpenProjectError {
description: format!("Error while connecting to openproject instance: {}", value),
}
}
}
impl From<Error> for GeneralError {
fn from(value: Error) -> GeneralError {
impl From<OpenProjectError> for GeneralError {
fn from(value: OpenProjectError) -> GeneralError {
GeneralError {
description: value.description,
}
@@ -33,7 +34,7 @@ pub(crate) struct Client {
pub async fn handle_response_status<T: for<'de> serde::Deserialize<'de>>(
response: Response,
error_message: &str,
) -> Result<T, Error> {
) -> Result<T, OpenProjectError> {
if !response.status().is_success() {
let status = response.status().to_string().clone();
let content = response
@@ -42,7 +43,7 @@ pub async fn handle_response_status<T: for<'de> serde::Deserialize<'de>>(
.unwrap_or_else(|_| "Impossible to decode".into())
.clone();
return Err(Error {
return Err(OpenProjectError {
description: format!(
"{}, status: {}, content: {}",
error_message, status, content
@@ -50,9 +51,18 @@ pub async fn handle_response_status<T: for<'de> serde::Deserialize<'de>>(
});
}
let t = response.json().await?;
let t = response.json::<T>().await;
Ok(t)
match t {
Ok(t) => Ok(t),
Err(e) => Err(OpenProjectError {
description: format!(
"Error while decoding json: {}, source: {:?}",
e.to_string(),
e.source()
),
}),
}
}
impl Client {
@@ -67,21 +77,22 @@ impl Client {
&self,
work_package: &WorkPackageWriter,
project_id: &String,
) -> Result<WorkPackage, Error> {
) -> Result<WorkPackage, OpenProjectError> {
let client = reqwest::Client::new();
let work_package: WorkPackage = client
let response = client
.post(format!(
"{}/api/v3/projects/{}/work_packages",
self.base_url, project_id
))
.basic_auth("apikey", Some(&self.token))
.json(&work_package)
.json(work_package)
.send()
.await?
.json()
.await?;
let work_package =
handle_response_status(response, "error while retrieving work package").await?;
Ok(work_package)
}
}