torn_api/request/mod.rs
1use bytes::Bytes;
2use http::StatusCode;
3
4#[cfg(feature = "requests")]
5/// Auto-generated api requests definitions.
6pub mod models;
7
8/// A generic api request description.
9#[derive(Default)]
10pub struct ApiRequest {
11 /// The relative path relative to "<https://api.torn.com/v2>".
12 pub path: String,
13 /// All url parameters.
14 pub parameters: Vec<(&'static str, String)>,
15}
16
17impl ApiRequest {
18 pub fn url(&self) -> String {
19 let mut url = format!("https://api.torn.com/v2{}?", self.path);
20
21 let mut first = true;
22 for (name, value) in &self.parameters {
23 if first {
24 first = false;
25 } else {
26 url.push('&');
27 }
28 url.push_str(&format!("{name}={value}"));
29 }
30
31 url
32 }
33}
34
35/// A generic api response.
36pub struct ApiResponse {
37 /// The response body as binary blob.
38 pub body: Option<Bytes>,
39 /// The HTTP response code.
40 pub status: StatusCode,
41}
42
43/// Trait for typed api requests
44pub trait IntoRequest: Send {
45 /// If used in bulk request, the discriminant is used to distinguish the responses. For
46 /// endpoints which have no path parameters this will be `()`.
47 type Discriminant: Send + 'static;
48 /// The response type which shall be deserialised.
49 type Response: for<'de> serde::Deserialize<'de> + Send;
50 fn into_request(self) -> (Self::Discriminant, ApiRequest);
51}
52
53/* #[cfg(feature = "requests")]
54pub(crate) struct WrappedApiRequest<R>
55where
56 R: IntoRequest,
57{
58 discriminant: R::Discriminant,
59 request: ApiRequest,
60}
61
62#[cfg(feature = "requests")]
63impl<R> IntoRequest for WrappedApiRequest<R>
64where
65 R: IntoRequest,
66{
67 type Discriminant = R::Discriminant;
68 type Response = R::Response;
69 fn into_request(self) -> (Self::Discriminant, ApiRequest) {
70 (self.discriminant, self.request)
71 }
72} */
73
74#[cfg(test)]
75mod test {}