Update http_client and version modules
This commit is contained in:
parent
2d764af8d7
commit
94f333f983
2 changed files with 36 additions and 21 deletions
|
|
@ -1,4 +1,3 @@
|
||||||
|
|
||||||
use crate::error::BotError;
|
use crate::error::BotError;
|
||||||
use log::{debug, error};
|
use log::{debug, error};
|
||||||
use serde::{de::DeserializeOwned, Serialize};
|
use serde::{de::DeserializeOwned, Serialize};
|
||||||
|
|
@ -44,8 +43,10 @@ impl BotServerClient {
|
||||||
&self.base_url
|
&self.base_url
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Perform a GET request to the specified endpoint.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// Returns an error if the request fails or the response cannot be parsed.
|
||||||
pub async fn get<T: DeserializeOwned>(&self, endpoint: &str) -> Result<T, BotError> {
|
pub async fn get<T: DeserializeOwned>(&self, endpoint: &str) -> Result<T, BotError> {
|
||||||
let url = format!("{}{endpoint}", self.base_url);
|
let url = format!("{}{endpoint}", self.base_url);
|
||||||
debug!("GET {url}");
|
debug!("GET {url}");
|
||||||
|
|
@ -54,8 +55,10 @@ impl BotServerClient {
|
||||||
self.handle_response(response).await
|
self.handle_response(response).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Perform a POST request to the specified endpoint.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// Returns an error if the request fails or the response cannot be parsed.
|
||||||
pub async fn post<T: Serialize + Send + Sync, R: DeserializeOwned>(
|
pub async fn post<T: Serialize + Send + Sync, R: DeserializeOwned>(
|
||||||
&self,
|
&self,
|
||||||
endpoint: &str,
|
endpoint: &str,
|
||||||
|
|
@ -68,8 +71,10 @@ impl BotServerClient {
|
||||||
self.handle_response(response).await
|
self.handle_response(response).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Perform a PUT request to the specified endpoint.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// Returns an error if the request fails or the response cannot be parsed.
|
||||||
pub async fn put<T: Serialize + Send + Sync, R: DeserializeOwned>(
|
pub async fn put<T: Serialize + Send + Sync, R: DeserializeOwned>(
|
||||||
&self,
|
&self,
|
||||||
endpoint: &str,
|
endpoint: &str,
|
||||||
|
|
@ -82,8 +87,10 @@ impl BotServerClient {
|
||||||
self.handle_response(response).await
|
self.handle_response(response).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Perform a PATCH request to the specified endpoint.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// Returns an error if the request fails or the response cannot be parsed.
|
||||||
pub async fn patch<T: Serialize + Send + Sync, R: DeserializeOwned>(
|
pub async fn patch<T: Serialize + Send + Sync, R: DeserializeOwned>(
|
||||||
&self,
|
&self,
|
||||||
endpoint: &str,
|
endpoint: &str,
|
||||||
|
|
@ -96,8 +103,10 @@ impl BotServerClient {
|
||||||
self.handle_response(response).await
|
self.handle_response(response).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Perform a DELETE request to the specified endpoint.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// Returns an error if the request fails or the response cannot be parsed.
|
||||||
pub async fn delete<T: DeserializeOwned>(&self, endpoint: &str) -> Result<T, BotError> {
|
pub async fn delete<T: DeserializeOwned>(&self, endpoint: &str) -> Result<T, BotError> {
|
||||||
let url = format!("{}{endpoint}", self.base_url);
|
let url = format!("{}{endpoint}", self.base_url);
|
||||||
debug!("DELETE {url}");
|
debug!("DELETE {url}");
|
||||||
|
|
@ -106,8 +115,10 @@ impl BotServerClient {
|
||||||
self.handle_response(response).await
|
self.handle_response(response).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Perform an authorized GET request with a bearer token.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// Returns an error if the request fails or the response cannot be parsed.
|
||||||
pub async fn get_authorized<T: DeserializeOwned>(
|
pub async fn get_authorized<T: DeserializeOwned>(
|
||||||
&self,
|
&self,
|
||||||
endpoint: &str,
|
endpoint: &str,
|
||||||
|
|
@ -120,8 +131,10 @@ impl BotServerClient {
|
||||||
self.handle_response(response).await
|
self.handle_response(response).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Perform an authorized POST request with a bearer token.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// Returns an error if the request fails or the response cannot be parsed.
|
||||||
pub async fn post_authorized<T: Serialize + Send + Sync, R: DeserializeOwned>(
|
pub async fn post_authorized<T: Serialize + Send + Sync, R: DeserializeOwned>(
|
||||||
&self,
|
&self,
|
||||||
endpoint: &str,
|
endpoint: &str,
|
||||||
|
|
@ -141,8 +154,10 @@ impl BotServerClient {
|
||||||
self.handle_response(response).await
|
self.handle_response(response).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Perform an authorized DELETE request with a bearer token.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// Returns an error if the request fails or the response cannot be parsed.
|
||||||
pub async fn delete_authorized<T: DeserializeOwned>(
|
pub async fn delete_authorized<T: DeserializeOwned>(
|
||||||
&self,
|
&self,
|
||||||
endpoint: &str,
|
endpoint: &str,
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
|
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
use log::debug;
|
use log::debug;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
@ -197,14 +196,15 @@ impl VersionRegistry {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Serialize the registry to a JSON string.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// Returns an error if serialization fails.
|
||||||
pub fn to_json(&self) -> Result<String, serde_json::Error> {
|
pub fn to_json(&self) -> Result<String, serde_json::Error> {
|
||||||
serde_json::to_string_pretty(self)
|
serde_json::to_string_pretty(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn init_version_registry() {
|
pub fn init_version_registry() {
|
||||||
let registry = VersionRegistry::new();
|
let registry = VersionRegistry::new();
|
||||||
if let Ok(mut guard) = VERSION_REGISTRY.write() {
|
if let Ok(mut guard) = VERSION_REGISTRY.write() {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue