torn-api.rs/torn-api/src/request/mod.rs

75 lines
1.9 KiB
Rust

use bytes::Bytes;
use http::StatusCode;
#[cfg(feature = "requests")]
/// Auto-generated api requests definitions.
pub mod models;
/// A generic api request description.
#[derive(Default)]
pub struct ApiRequest {
/// The relative path relative to "<https://api.torn.com/v2>".
pub path: String,
/// All url parameters.
pub parameters: Vec<(&'static str, String)>,
}
impl ApiRequest {
pub fn url(&self) -> String {
let mut url = format!("https://api.torn.com/v2{}?", self.path);
let mut first = true;
for (name, value) in &self.parameters {
if first {
first = false;
} else {
url.push('&');
}
url.push_str(&format!("{name}={value}"));
}
url
}
}
/// A generic api response.
pub struct ApiResponse {
/// The response body as binary blob.
pub body: Option<Bytes>,
/// The HTTP response code.
pub status: StatusCode,
}
/// Trait for typed api requests
pub trait IntoRequest: Send {
/// If used in bulk request, the discriminant is used to distinguish the responses. For
/// endpoints which have no path parameters this will be `()`.
type Discriminant: Send + 'static;
/// The response type which shall be deserialised.
type Response: for<'de> serde::Deserialize<'de> + Send;
fn into_request(self) -> (Self::Discriminant, ApiRequest);
}
/* #[cfg(feature = "requests")]
pub(crate) struct WrappedApiRequest<R>
where
R: IntoRequest,
{
discriminant: R::Discriminant,
request: ApiRequest,
}
#[cfg(feature = "requests")]
impl<R> IntoRequest for WrappedApiRequest<R>
where
R: IntoRequest,
{
type Discriminant = R::Discriminant;
type Response = R::Response;
fn into_request(self) -> (Self::Discriminant, ApiRequest) {
(self.discriminant, self.request)
}
} */
#[cfg(test)]
mod test {}