use async_trait::async_trait;
use gitlab::AsyncGitlab;
use gitlab::api::{AsyncQuery, Endpoint, raw};
use ironflow_core::error::OperationError;
use ironflow_core::operation::{Operation, OperationContext};
use serde_json::Value;
pub struct GitLabOp<E> {
client: AsyncGitlab,
endpoint: E,
}
impl<E> GitLabOp<E> {
pub(crate) fn new(client: AsyncGitlab, endpoint: E) -> Self {
Self { client, endpoint }
}
}
#[async_trait]
impl<E> Operation for GitLabOp<E>
where
E: Endpoint + Sync + Send,
{
fn kind(&self) -> &str {
"gitlab"
}
async fn execute(&self, _ctx: &OperationContext) -> Result<Value, OperationError> {
let body: Vec<u8> = raw(&self.endpoint)
.query_async(&self.client)
.await
.map_err(|e| OperationError::Http {
status: None,
message: e.to_string(),
})?;
if body.iter().all(u8::is_ascii_whitespace) {
return Ok(Value::Null);
}
serde_json::from_slice(&body).map_err(|e| OperationError::Http {
status: None,
message: format!("could not parse the GitLab response: {e}"),
})
}
fn input(&self) -> Option<Value> {
Some(Value::Object(serde_json::Map::from_iter([(
"endpoint".to_string(),
Value::String(self.endpoint.endpoint().into_owned()),
)])))
}
}