openapi_rs/common/
define.rs1use bytes::Bytes;
2use futures::Stream;
3use reqwest::header::{HeaderMap, HeaderValue};
4use serde::{Deserialize, Serialize};
5use std::collections::HashMap;
6use std::pin::Pin;
7
8pub type BytesStream = Pin<Box<dyn Stream<Item = Result<Bytes, reqwest::Error>> + Send>>;
9
10pub trait HttpBuilder {
11 type Response;
12 fn builder(self) -> HttpFn<Self::Response>;
13}
14
15pub trait HttpStreamBuilder {
16 type Response;
17 fn stream_builder(self) -> HttpFn<Self::Response>;
18}
19
20pub type HttpFn<T> = Box<dyn FnOnce() -> (RequestFn, AsyncResponseFn<T>) + Send>;
21
22pub type RequestFn = Box<dyn FnOnce() -> BaseRequest + Send>;
23
24pub type AsyncResponseFn<T> = Box<
25 dyn FnOnce(reqwest::Response) -> Pin<Box<dyn Future<Output = anyhow::Result<T>> + Send>> + Send,
26>;
27
28#[derive(derive_more::Debug, Default)]
29pub struct BaseRequest {
30 pub method: reqwest::Method,
31 pub uri: String,
32
33 pub headers: HeaderMap<HeaderValue>,
34 pub content_type: Option<String>,
35
36 pub queries: Option<HashMap<String, String>>,
37 pub form: Option<HashMap<String, String>>,
38 pub body: Bytes,
39
40 #[debug(skip)]
41 pub stream: Option<BytesStream>,
42}
43
44#[derive(Debug, Default, Clone, Serialize, Deserialize)]
45#[serde(default)]
46pub struct BaseResponse<T> {
47 #[serde(rename = "ErrorCode")]
48 pub error_code: String,
49 #[serde(rename = "ErrorMessage")]
50 pub error_msg: String,
51 #[serde(rename = "RequestID")]
52 pub request_id: String,
53 #[serde(rename = "Data")]
54 pub data: Option<T>,
55}