use crate::error::{ApiError, Result};
use reqwest::Client;
use serde_json::Value;
use url::Url;
pub struct GitSettingsClient {
base_url: String,
client: Client,
}
impl GitSettingsClient {
pub fn new(base_url: String, client: Client) -> Self {
Self { base_url, client }
}
pub fn with_auth(self, token: &str) -> Self {
self
}
pub async fn get_repo_settings_cloud_native_build(
&self,
repo: String,
) -> Result<Value> {
let path = format!("/{}/-/settings/cloud-native-build", repo);
let url = Url::parse(&format!("{}{}", self.base_url, path))?;
let request = self.client.request(
reqwest::Method::GET,
url
);
let response = request.send().await?;
if response.status().is_success() {
let json: Value = response.json().await?;
Ok(json)
} else {
Err(ApiError::HttpError(response.status().as_u16()))
}
}
pub async fn put_repo_settings_cloud_native_build(
&self,
repo: String,
pipeline_form: serde_json::Value,
) -> Result<Value> {
let path = format!("/{}/-/settings/cloud-native-build", repo);
let url = Url::parse(&format!("{}{}", self.base_url, path))?;
let mut request = self.client.request(
reqwest::Method::PUT,
url
);
request = request.json(&pipeline_form);
let response = request.send().await?;
if response.status().is_success() {
let json: Value = response.json().await?;
Ok(json)
} else {
Err(ApiError::HttpError(response.status().as_u16()))
}
}
pub async fn get_repo_settings_branch_protections(
&self,
repo: String,
) -> Result<Value> {
let path = format!("/{}/-/settings/branch-protections", repo);
let url = Url::parse(&format!("{}{}", self.base_url, path))?;
let request = self.client.request(
reqwest::Method::GET,
url
);
let response = request.send().await?;
if response.status().is_success() {
let json: Value = response.json().await?;
Ok(json)
} else {
Err(ApiError::HttpError(response.status().as_u16()))
}
}
pub async fn post_repo_settings_branch_protections(
&self,
repo: String,
branch_protection_form: serde_json::Value,
) -> Result<Value> {
let path = format!("/{}/-/settings/branch-protections", repo);
let url = Url::parse(&format!("{}{}", self.base_url, path))?;
let mut request = self.client.request(
reqwest::Method::POST,
url
);
request = request.json(&branch_protection_form);
let response = request.send().await?;
if response.status().is_success() {
let json: Value = response.json().await?;
Ok(json)
} else {
Err(ApiError::HttpError(response.status().as_u16()))
}
}
pub async fn get_repo_settings_pull_request(
&self,
repo: String,
) -> Result<Value> {
let path = format!("/{}/-/settings/pull-request", repo);
let url = Url::parse(&format!("{}{}", self.base_url, path))?;
let request = self.client.request(
reqwest::Method::GET,
url
);
let response = request.send().await?;
if response.status().is_success() {
let json: Value = response.json().await?;
Ok(json)
} else {
Err(ApiError::HttpError(response.status().as_u16()))
}
}
pub async fn put_repo_settings_pull_request(
&self,
repo: String,
pull_request_form: serde_json::Value,
) -> Result<Value> {
let path = format!("/{}/-/settings/pull-request", repo);
let url = Url::parse(&format!("{}{}", self.base_url, path))?;
let mut request = self.client.request(
reqwest::Method::PUT,
url
);
request = request.json(&pull_request_form);
let response = request.send().await?;
if response.status().is_success() {
let json: Value = response.json().await?;
Ok(json)
} else {
Err(ApiError::HttpError(response.status().as_u16()))
}
}
pub async fn get_repo_settings_branch_protections_id(
&self,
repo: String,
id: String,
) -> Result<Value> {
let path = format!("/{}/-/settings/branch-protections/{}", repo, id);
let url = Url::parse(&format!("{}{}", self.base_url, path))?;
let request = self.client.request(
reqwest::Method::GET,
url
);
let response = request.send().await?;
if response.status().is_success() {
let json: Value = response.json().await?;
Ok(json)
} else {
Err(ApiError::HttpError(response.status().as_u16()))
}
}
pub async fn delete_repo_settings_branch_protections_id(
&self,
repo: String,
id: String,
) -> Result<Value> {
let path = format!("/{}/-/settings/branch-protections/{}", repo, id);
let url = Url::parse(&format!("{}{}", self.base_url, path))?;
let request = self.client.request(
reqwest::Method::DELETE,
url
);
let response = request.send().await?;
if response.status().is_success() {
let json: Value = response.json().await?;
Ok(json)
} else {
Err(ApiError::HttpError(response.status().as_u16()))
}
}
pub async fn patch_repo_settings_branch_protections_id(
&self,
repo: String,
id: String,
branch_protection_form: serde_json::Value,
) -> Result<Value> {
let path = format!("/{}/-/settings/branch-protections/{}", repo, id);
let url = Url::parse(&format!("{}{}", self.base_url, path))?;
let mut request = self.client.request(
reqwest::Method::PATCH,
url
);
request = request.json(&branch_protection_form);
let response = request.send().await?;
if response.status().is_success() {
let json: Value = response.json().await?;
Ok(json)
} else {
Err(ApiError::HttpError(response.status().as_u16()))
}
}
pub async fn get_repo_settings_push_limit(
&self,
repo: String,
) -> Result<Value> {
let path = format!("/{}/-/settings/push-limit", repo);
let url = Url::parse(&format!("{}{}", self.base_url, path))?;
let request = self.client.request(
reqwest::Method::GET,
url
);
let response = request.send().await?;
if response.status().is_success() {
let json: Value = response.json().await?;
Ok(json)
} else {
Err(ApiError::HttpError(response.status().as_u16()))
}
}
pub async fn put_repo_settings_push_limit(
&self,
repo: String,
push_limit_form: serde_json::Value,
) -> Result<Value> {
let path = format!("/{}/-/settings/push-limit", repo);
let url = Url::parse(&format!("{}{}", self.base_url, path))?;
let mut request = self.client.request(
reqwest::Method::PUT,
url
);
request = request.json(&push_limit_form);
let response = request.send().await?;
if response.status().is_success() {
let json: Value = response.json().await?;
Ok(json)
} else {
Err(ApiError::HttpError(response.status().as_u16()))
}
}
}