34 lines
967 B
Rust
34 lines
967 B
Rust
use crate::context::GeoffreyContext;
|
|
use crate::error::BotError;
|
|
use geoffrey_models::models::parameters::{CommandRequest, GeoffreyParam};
|
|
use geoffrey_models::models::response::APIResponse;
|
|
use reqwest::Method;
|
|
use serde::de::DeserializeOwned;
|
|
|
|
pub fn build_url(base_str: &str, end_point: &str) -> String {
|
|
let slash = if !base_str.ends_with('/') { "/" } else { "" };
|
|
|
|
format!("{}{}{}", base_str, slash, end_point)
|
|
}
|
|
|
|
pub async fn run_api_query<T: GeoffreyParam, U: DeserializeOwned>(
|
|
ctx: &GeoffreyContext,
|
|
param: &CommandRequest<T>,
|
|
method: Method,
|
|
endpoint: &str,
|
|
) -> Result<U, BotError> {
|
|
let resp: APIResponse<U> = ctx
|
|
.http_client
|
|
.request(method, build_url(&ctx.cfg.api.base_url, endpoint))
|
|
.json(param)
|
|
.send()
|
|
.await?
|
|
.json()
|
|
.await?;
|
|
|
|
match resp {
|
|
APIResponse::Response(resp) => Ok(resp),
|
|
APIResponse::Error { error: e, .. } => Err(e.into()),
|
|
}
|
|
}
|