reqwest-enum 0.4.0

Typed enum HTTP API for reqwest.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
use std::{time::Duration, future::Future};
use serde::de::DeserializeOwned;
use crate::{
    http::{AuthMethod, HTTPBody, HTTPResponse},
    error::Error,
    target::Target,
};

#[cfg(feature = "jsonrpc")]
use crate::{
    jsonrpc::{JsonRpcError, JsonRpcRequest, JsonRpcResult},
    target::JsonRpcTarget,
};
#[cfg(feature = "jsonrpc")]
use futures::future::join_all;

#[cfg(not(feature = "middleware"))]
pub type ProviderRequestBuilder = reqwest::RequestBuilder;
#[cfg(feature = "middleware")]
pub type ProviderRequestBuilder = reqwest_middleware::RequestBuilder;
#[cfg(feature = "middleware")]
use reqwest_middleware::{ClientBuilder as MiddlewareClientBuilder, ClientWithMiddleware};

// Base trait for providers, defining the core request method.
pub trait ProviderType<T: Target>: Send {
    /// request to target and return http response
    fn request(&self, target: T) -> impl Future<Output = Result<HTTPResponse, Error>>;
}

// Trait for providers that can handle JSON responses, deserializing them.
pub trait JsonProviderType<T: Target>: ProviderType<T> {
    /// request and deserialize response to json using serde
    fn request_json<U: DeserializeOwned>(
        &self,
        target: T,
    ) -> impl Future<Output = Result<U, Error>>;
}

#[cfg(feature = "jsonrpc")]
pub trait JsonRpcProviderType<T: Target>: ProviderType<T> {
    /// batch isomorphic JSON-RPC requests
    fn batch<U: DeserializeOwned>(
        &self,
        targets: Vec<T>,
    ) -> impl Future<Output = Result<Vec<JsonRpcResult<U>>, JsonRpcError>>;

    fn batch_chunk_by<U: DeserializeOwned>(
        &self,
        targets: Vec<T>,
        chunk_size: usize,
    ) -> impl Future<Output = Result<Vec<JsonRpcResult<U>>, JsonRpcError>>;
}

pub type EndpointFn<T> = fn(target: &T) -> String;
pub type RequestBuilderFn<T> =
    Box<dyn Fn(&ProviderRequestBuilder, &T) -> ProviderRequestBuilder + Send + Sync>;

/// Generic provider for HTTP requests to a `Target`. Handles construction, auth, and execution.
pub struct Provider<T: Target> {
    /// endpoint closure to customize the endpoint (url / path)
    endpoint_fn: Option<EndpointFn<T>>,
    request_fn: Option<RequestBuilderFn<T>>,
    /// An optional default timeout for all requests made by this provider.
    /// If set, this timeout is applied to each request unless overridden by more specific timeout logic.
    timeout: Option<Duration>,
    #[cfg(not(feature = "middleware"))]
    client: reqwest::Client,
    #[cfg(feature = "middleware")]
    client: ClientWithMiddleware,
}

impl<T: Target> std::fmt::Debug for Provider<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Provider")
            .field("endpoint_fn", &self.endpoint_fn.map(|_| "<function>")) // Print placeholder for fn pointer
            .field("request_fn", &self.request_fn.as_ref().map(|_| "<function>")) // Print placeholder for Box<dyn Fn>
            .field("timeout", &self.timeout)
            .field("client", &self.client) // reqwest::Client and reqwest_middleware::ClientWithMiddleware implement Debug
            .finish()
    }
}

impl<T> ProviderType<T> for Provider<T>
where
    T: Target + Send,
{
    /// Builds and executes a request to `Target`, returning raw `HTTPResponse`.
    async fn request(&self, target: T) -> Result<HTTPResponse, Error> {
        let req = self.request_builder(&target)?.build()?;
        self.client.execute(req).await.map_err(Error::from)
    }
}

impl<T> JsonProviderType<T> for Provider<T>
where
    T: Target + Send,
{
    async fn request_json<U: DeserializeOwned>(&self, target: T) -> Result<U, Error> {
        let response = self.request(target).await?;

        // Check status and get Response or reqwest::Error
        let response = response.error_for_status()?;

        // If error_for_status succeeded, deserialize the JSON.
        let body: U = response.json().await?;

        Ok(body)
    }
}

#[cfg(feature = "jsonrpc")]
impl<T> JsonRpcProviderType<T> for Provider<T>
where
    T: JsonRpcTarget + Send,
{
    async fn batch<U: DeserializeOwned>(
        &self,
        targets: Vec<T>,
    ) -> Result<Vec<JsonRpcResult<U>>, JsonRpcError> {
        if targets.is_empty() {
            return Err(JsonRpcError {
                code: -32600,
                message: "Invalid Request".into(),
            });
        }

        let representative_target = &targets[0];

        let mut builder = self.request_builder(representative_target)?;

        let mut rpc_payload = Vec::new();
        for (k, individual_target) in targets.iter().enumerate() {
            let req = JsonRpcRequest::new(individual_target.method_name(), individual_target.params(), (k + 1) as u64);
            rpc_payload.push(req);
        }
        let body = HTTPBody::from_array(&rpc_payload).map_err(|e| JsonRpcError { code: -32700, message: format!("Failed to serialize batch request: {}", e) })?;

        builder = builder.body(body.inner);

        // Build the final reqwest::Request
        let final_request = builder.build().map_err(|e| JsonRpcError { code: -32603, message: format!("Failed to build batch request: {}", e) })?;

        // Execute the request using self.client
        let response = self.client.execute(final_request).await.map_err(|e| JsonRpcError { code: -32603, message: format!("Batch request execution failed: {}", e) })?;
        
        // Deserialize the response
        let response_body = response.json::<Vec<JsonRpcResult<U>>>().await.map_err(|e| JsonRpcError { code: -32700, message: format!("Failed to parse batch JSON response: {}", e) })?;
        Ok(response_body)
    }

    async fn batch_chunk_by<U: DeserializeOwned>(
        &self,
        targets: Vec<T>,
        chunk_size: usize,
    ) -> Result<Vec<JsonRpcResult<U>>, JsonRpcError> {
        if targets.is_empty() || chunk_size == 0 {
            return Err(JsonRpcError {
                code: -32600,
                message: "Invalid Request".into(),
            });
        }

        let chunk_targets = targets.chunks(chunk_size).collect::<Vec<_>>();
        let mut rpc_requests = Vec::<ProviderRequestBuilder>::new();

        for (chunk_idx, chunk) in chunk_targets.into_iter().enumerate() {
            let target = &chunk[0];
            let mut request = self.request_builder(target);
            let mut requests = Vec::<JsonRpcRequest>::new();
            for (k, v) in chunk.iter().enumerate() {
                let request = JsonRpcRequest::new(
                    v.method_name(),
                    v.params(),
                    (chunk_idx * chunk_size + k + 1) as u64,
                );
                requests.push(request);
            }

            let http_body = HTTPBody::from_array(&requests).map_err(|e| JsonRpcError { code: -32700, message: format!("Failed to serialize batch chunk: {}", e) })?;
            request = Ok(request?.body(http_body.inner));
            rpc_requests.push(request?);
        }
        let bodies = join_all(rpc_requests.into_iter().map(|request| async move {
            #[cfg(feature = "middleware")]
            let response = request.send().await.map_err(crate::Error::ReqwestMiddleware)?;
            #[cfg(not(feature = "middleware"))]
            let response = request.send().await?;
            let body = response.json::<Vec<JsonRpcResult<U>>>().await?;
            Ok(body)
        }))
        .await;

        let mut results = Vec::<JsonRpcResult<U>>::new();
        let mut error: Option<JsonRpcError> = None;

        for result in bodies {
            match result {
                Ok(body) => {
                    results.extend(body);
                }
                Err(err) => {
                    error = Some(err);
                }
            }
        }
        if let Some(err) = error {
            return Err(err);
        }
        Ok(results)
    }
}

impl<T> Provider<T>
where
    T: Target,
{
    /// Creates a new `Provider` with optional URL, request builder customization, and timeout.
    pub fn new(
        endpoint_fn: Option<EndpointFn<T>>,
        request_fn: Option<RequestBuilderFn<T>>,
        timeout: Option<Duration>,
    ) -> Self {
        #[cfg(not(feature = "middleware"))]
        let client = reqwest::Client::new();
        #[cfg(feature = "middleware")]
        let client = {
            MiddlewareClientBuilder::new(reqwest::Client::new()).build()
        };
        Self {
            client,
            endpoint_fn,
            request_fn,
            timeout,
        }
    }

    #[cfg(not(feature = "middleware"))]
    pub fn with_client(
        client: reqwest::Client,
        endpoint_fn: Option<EndpointFn<T>>,
        request_fn: Option<RequestBuilderFn<T>>,
    ) -> Self {
        Self {
            endpoint_fn,
            request_fn,
            client,
            timeout: None,
        }
    }

    #[cfg(feature = "middleware")]
    pub fn with_client(
        client: ClientWithMiddleware,
        endpoint_fn: Option<EndpointFn<T>>,
        request_fn: Option<RequestBuilderFn<T>>,
    ) -> Self {
        Self {
            endpoint_fn,
            request_fn,
            client,
            timeout: None,
        }
    }

    pub fn request_url(&self, target: &T) -> String {
        let mut url = format!("{}{}", target.base_url(), target.path());
        if let Some(func) = &self.endpoint_fn {
            url = func(target);
        }
        url
    }

    /// Constructs a `reqwest::RequestBuilder` for the `Target`, applying URL, method, query, headers, auth, body, timeout, and custom `request_fn`.
    pub(crate) fn request_builder(&self, target: &T) -> Result<ProviderRequestBuilder, Error> {
        let url = self.request_url(target);
        let mut request_builder = self.client.request(target.method().into(), url.as_str());

        // apply query params
        request_builder = request_builder.query(&target.query());

        // apply headers
        for (key, value) in target.headers() {
            request_builder = request_builder.header(key, value);
        }

        // apply authentication
        if let Some(auth) = target.authentication() {
            request_builder = match auth {
                AuthMethod::Bearer(token) => request_builder.bearer_auth(token),
                AuthMethod::Basic(username, password) => request_builder.basic_auth(username, password),
                AuthMethod::Custom(auth_fn) => auth_fn(request_builder),
            };
        }

        // apply body
        let body = target.body()?;
        request_builder = request_builder.body(body.inner);

        // apply provider timeout
        if let Some(provider_timeout) = self.timeout {
            request_builder = request_builder.timeout(provider_timeout);
        }

        // apply request_fn closure
        if let Some(r_fn) = &self.request_fn {
            request_builder = r_fn(&request_builder, target);
        }

        Ok(request_builder)
    }
}

impl<T> Default for Provider<T>
where
    T: Target,
{
    fn default() -> Self {
        #[cfg(not(feature = "middleware"))]
        let client = reqwest::Client::new();
        #[cfg(feature = "middleware")]
        let client = {
            MiddlewareClientBuilder::new(reqwest::Client::new()).build()
        };
        Self {
            client,
            endpoint_fn: None,
            request_fn: None,
            timeout: None,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        http::{AuthMethod, HTTPBody, HTTPMethod},
        provider::{JsonProviderType, Provider},
        target::Target,
    };
    use serde::{Deserialize, Serialize};
    use std::{borrow::Cow, collections::hash_map::DefaultHasher, collections::HashMap, hash::{Hash, Hasher}, time::{Duration, UNIX_EPOCH}};

    #[derive(Serialize, Deserialize)]
    struct Person {
        name: String,
        age: u8,
        phones: Vec<String>,
    }

    enum HttpBin {
        Get,
        Post,
        Bearer,
        HeaderAuth,
    }

    impl Target for HttpBin {
        fn base_url(&self) -> Cow<'_, str> {
            Cow::Borrowed("https://httpbin.org")
        }

        fn method(&self) -> HTTPMethod {
            match self {
                HttpBin::Get => HTTPMethod::GET,
                HttpBin::Post => HTTPMethod::POST,
                HttpBin::Bearer => HTTPMethod::GET,
                HttpBin::HeaderAuth => HTTPMethod::GET,
            }
        }

        fn path(&self) -> String {
            let ts = UNIX_EPOCH + Duration::from_secs(1728044812);
            match self {
                HttpBin::Get => format!(
                    "/get?ts={}",
                    ts.duration_since(UNIX_EPOCH).unwrap().as_secs(),
                ),
                HttpBin::Post => "/post".into(),
                HttpBin::Bearer => "/bearer".into(),
                HttpBin::HeaderAuth => "/headers".into(),
            }
        }

        fn query(&self) -> HashMap<String, String> {
            HashMap::from([("foo".to_string(), "bar".to_string())])
        }

        fn headers(&self) -> HashMap<String, String> {
            HashMap::default()
        }

        fn authentication(&self) -> Option<AuthMethod> {
            match self {
                HttpBin::Bearer => Some(AuthMethod::Bearer("token".to_string())),
                HttpBin::HeaderAuth => Some(AuthMethod::header_api_key(
                    "X-Test-Api-Key".to_string(),
                    "my-secret-key".to_string(),
                )),
                _ => None,
            }
        }

        fn body(&self) -> Result<HTTPBody, crate::Error> {
            match self {
                HttpBin::Get | HttpBin::Bearer | HttpBin::HeaderAuth => Ok(HTTPBody::default()),
                HttpBin::Post => {
                    let person = Person {
                        name: "test".to_string(),
                        age: 20,
                        phones: vec!["1234567890".to_string()],
                    };
                    Ok(HTTPBody::from(&person)?)
                }
            }
        }
    }

    #[test]
    fn test_test_endpoint_closure() {
        let provider = Provider::<HttpBin>::default();
        assert_eq!(
            provider.request_url(&HttpBin::Get),
            "https://httpbin.org/get?ts=1728044812"
        );

        let provider =
            Provider::<HttpBin>::new(Some(|_: &HttpBin| "http://httpbin.org".to_string()), None, None);
        assert_eq!(provider.request_url(&HttpBin::Post), "http://httpbin.org");
    }

    #[test]
    fn test_request_fn() {
        let provider = Provider::<HttpBin>::new(
            None,
            Some(Box::new(|builder: &ProviderRequestBuilder, target: &HttpBin| {
                let mut hasher = DefaultHasher::new();
                target.query_string().hash(&mut hasher);
                let hash = hasher.finish();

                let mut req = builder.try_clone().expect("trying to clone request");
                req = req.header("X-test", "test");
                req = req.header("X-hash", format!("{}", hash));
                req
            })),
            None,
        );

        let request = provider.request_builder(&HttpBin::Get).unwrap().build().unwrap();
        let headers = request.headers();

        assert_eq!(request.method().to_string(), "GET");
        assert_eq!(headers.get("X-test").unwrap(), "test");
        assert_eq!(headers.get("X-hash").unwrap(), "3270317559611782182");
    }

    #[tokio::test]
    async fn test_authentication() {
        let provider = Provider::<HttpBin>::default();
        let response: serde_json::Value = provider
            .request_json(HttpBin::Bearer)
            .await
            .expect("request error");

        assert!(response["authenticated"].as_bool().unwrap());
    }

    #[tokio::test]
    async fn test_header_api_key_auth() {
        let provider = Provider::<HttpBin>::default();
        let response: serde_json::Value = provider
            .request_json(HttpBin::HeaderAuth)
            .await
            .expect("request error");

        // httpbin /headers returns a JSON object like: {"headers": {"Header-Name": "Header-Value", ...}}
        let headers_map = response.get("headers").unwrap().as_object().unwrap();
        assert_eq!(
            headers_map.get("X-Test-Api-Key").unwrap().as_str().unwrap(),
            "my-secret-key"
        );
    }
}