Add a command client to libjirac
This commit is contained in:
parent
6102233bc5
commit
8a7c989f48
16 changed files with 253 additions and 60 deletions
|
@ -5,4 +5,8 @@ edition = "2021"
|
|||
|
||||
[dependencies]
|
||||
serde = { workspace = true }
|
||||
chrono = { workspace = true }
|
||||
chrono = { workspace = true }
|
||||
reqwest = { workspace = true }
|
||||
http = { workspace = true }
|
||||
url = { workspace = true }
|
||||
thiserror = { workspace = true }
|
118
crates/libjirac/src/client.rs
Normal file
118
crates/libjirac/src/client.rs
Normal file
|
@ -0,0 +1,118 @@
|
|||
pub mod commands;
|
||||
|
||||
use http::header::CONTENT_TYPE;
|
||||
use http::{HeaderMap, HeaderValue};
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::Serialize;
|
||||
use std::fmt::Debug;
|
||||
use thiserror::Error;
|
||||
use url::{ParseError, Url};
|
||||
|
||||
pub struct JiraClient {
|
||||
pub url: String,
|
||||
pub email: String,
|
||||
pub api_token: String,
|
||||
}
|
||||
|
||||
pub trait JiraCommand {
|
||||
type TResponse: DeserializeOwned + Clone;
|
||||
type TPayload: Serialize;
|
||||
const REQUEST_TYPE: JiraRequestType;
|
||||
fn endpoint(&self) -> String;
|
||||
fn request_body(&self) -> Option<&Self::TPayload>;
|
||||
fn query_params(&self) -> Option<Vec<(String, String)>>;
|
||||
}
|
||||
|
||||
pub enum JiraRequestType {
|
||||
Read,
|
||||
Create,
|
||||
Update,
|
||||
Delete,
|
||||
}
|
||||
|
||||
impl From<JiraRequestType> for http::Method {
|
||||
fn from(value: JiraRequestType) -> Self {
|
||||
match value {
|
||||
JiraRequestType::Read => Self::GET,
|
||||
JiraRequestType::Create => Self::POST,
|
||||
JiraRequestType::Update => Self::PATCH,
|
||||
JiraRequestType::Delete => Self::DELETE,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum JiraClientError {
|
||||
#[error(transparent)]
|
||||
UrlParseError(#[from] ParseError),
|
||||
#[error(transparent)]
|
||||
ReqwestError(#[from] reqwest::Error),
|
||||
#[error("API Error: {0}")]
|
||||
ApiError(String),
|
||||
}
|
||||
|
||||
impl JiraClient {
|
||||
pub fn new(url: &str, email: &str, api_token: &str) -> Self {
|
||||
Self {
|
||||
url: url.to_string(),
|
||||
email: email.to_string(),
|
||||
api_token: api_token.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn exec<TCommand>(
|
||||
&self,
|
||||
cmd: TCommand,
|
||||
) -> Result<TCommand::TResponse, JiraClientError>
|
||||
where
|
||||
TCommand: JiraCommand + Debug,
|
||||
{
|
||||
let client = reqwest::Client::new();
|
||||
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
|
||||
|
||||
let url = self.construct_url(&cmd.endpoint())?;
|
||||
|
||||
let mut request_builder = client
|
||||
.request(TCommand::REQUEST_TYPE.into(), url)
|
||||
.basic_auth(&self.email, Some(&self.api_token))
|
||||
.headers(headers);
|
||||
|
||||
if let Some(params) = cmd.query_params() {
|
||||
request_builder = request_builder.query(¶ms);
|
||||
}
|
||||
|
||||
if let Some(body) = cmd.request_body() {
|
||||
request_builder = request_builder.json(body);
|
||||
}
|
||||
|
||||
let response = match request_builder.send().await {
|
||||
Ok(x) => x,
|
||||
Err(reason) => return Err(JiraClientError::ReqwestError(reason)),
|
||||
};
|
||||
|
||||
if !response.status().is_success() {
|
||||
let error_text = response.text().await?;
|
||||
return Err(JiraClientError::ApiError(error_text));
|
||||
}
|
||||
|
||||
match response.json::<TCommand::TResponse>().await {
|
||||
Ok(x) => Ok(x),
|
||||
Err(reason) => Err(JiraClientError::ReqwestError(reason)),
|
||||
}
|
||||
}
|
||||
|
||||
fn construct_url(&self, endpoint: &str) -> Result<String, JiraClientError> {
|
||||
let mut url = match Url::parse(&self.url) {
|
||||
Ok(x) => x,
|
||||
Err(reason) => {
|
||||
return Err(JiraClientError::UrlParseError(reason));
|
||||
}
|
||||
};
|
||||
|
||||
url.set_path(endpoint);
|
||||
|
||||
Ok(url.to_string())
|
||||
}
|
||||
}
|
5
crates/libjirac/src/client/commands.rs
Normal file
5
crates/libjirac/src/client/commands.rs
Normal file
|
@ -0,0 +1,5 @@
|
|||
mod search_command;
|
||||
mod self_command;
|
||||
|
||||
pub use search_command::SearchCommand;
|
||||
pub use self_command::SelfCommand;
|
35
crates/libjirac/src/client/commands/search_command.rs
Normal file
35
crates/libjirac/src/client/commands/search_command.rs
Normal file
|
@ -0,0 +1,35 @@
|
|||
use crate::client::{JiraCommand, JiraRequestType};
|
||||
use crate::entities::search::JiraSearchResponse;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct SearchCommand {
|
||||
jql: String,
|
||||
}
|
||||
|
||||
impl SearchCommand {
|
||||
pub fn new(jql: &str) -> Self {
|
||||
Self {
|
||||
jql: jql.to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl JiraCommand for SearchCommand {
|
||||
type TResponse = JiraSearchResponse;
|
||||
type TPayload = ();
|
||||
const REQUEST_TYPE: JiraRequestType = JiraRequestType::Read;
|
||||
|
||||
fn endpoint(&self) -> String {
|
||||
"/rest/api/2/search".to_string()
|
||||
}
|
||||
|
||||
fn request_body(&self) -> Option<&Self::TPayload> {
|
||||
None
|
||||
}
|
||||
|
||||
fn query_params(&self) -> Option<Vec<(String, String)>> {
|
||||
let params = vec![("jql".to_string(), self.jql.clone())];
|
||||
|
||||
Some(params)
|
||||
}
|
||||
}
|
46
crates/libjirac/src/client/commands/self_command.rs
Normal file
46
crates/libjirac/src/client/commands/self_command.rs
Normal file
|
@ -0,0 +1,46 @@
|
|||
use crate::client::{JiraCommand, JiraRequestType};
|
||||
use serde::de::DeserializeOwned;
|
||||
use url::Url;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct SelfCommand<T>
|
||||
where
|
||||
T: DeserializeOwned + Clone,
|
||||
{
|
||||
href: String,
|
||||
_marker: std::marker::PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<T> SelfCommand<T>
|
||||
where
|
||||
T: DeserializeOwned + Clone,
|
||||
{
|
||||
pub fn new(href: &str) -> Self {
|
||||
Self {
|
||||
href: href.to_owned(),
|
||||
_marker: std::marker::PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> JiraCommand for SelfCommand<T>
|
||||
where
|
||||
T: DeserializeOwned + Clone,
|
||||
{
|
||||
type TResponse = T;
|
||||
type TPayload = ();
|
||||
const REQUEST_TYPE: JiraRequestType = JiraRequestType::Read;
|
||||
|
||||
fn endpoint(&self) -> String {
|
||||
let url = Url::parse(&self.href).unwrap();
|
||||
url.path().to_string()
|
||||
}
|
||||
|
||||
fn request_body(&self) -> Option<&Self::TPayload> {
|
||||
None
|
||||
}
|
||||
|
||||
fn query_params(&self) -> Option<Vec<(String, String)>> {
|
||||
None
|
||||
}
|
||||
}
|
|
@ -1 +1,2 @@
|
|||
pub mod issue;
|
||||
pub mod search;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
pub struct JiraIssue {
|
||||
pub key: String,
|
||||
#[serde(rename = "self")]
|
||||
|
@ -8,7 +8,7 @@ pub struct JiraIssue {
|
|||
pub fields: JiraIssueResponseFields,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
pub struct JiraIssueResponseFields {
|
||||
pub summary: String,
|
||||
pub description: Option<String>,
|
||||
|
@ -24,12 +24,12 @@ pub struct JiraIssueResponseFields {
|
|||
pub votes: Votes,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
pub struct Status {
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
pub struct Priority {
|
||||
pub name: String,
|
||||
pub id: String,
|
||||
|
@ -69,7 +69,7 @@ pub struct Comment {
|
|||
pub created: chrono::DateTime<chrono::Utc>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
pub struct Votes {
|
||||
#[serde(rename = "self")]
|
||||
pub href: String,
|
||||
|
|
8
crates/libjirac/src/entities/search.rs
Normal file
8
crates/libjirac/src/entities/search.rs
Normal file
|
@ -0,0 +1,8 @@
|
|||
use crate::entities::issue::JiraIssue;
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct JiraSearchResponse {
|
||||
pub issues: Vec<JiraIssue>,
|
||||
pub total: u32,
|
||||
}
|
|
@ -1 +1,2 @@
|
|||
pub mod client;
|
||||
pub mod entities;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue