firebase_rs_sdk/storage/request/
info.rs1use std::collections::HashMap;
2use std::sync::Arc;
3use std::time::Duration;
4
5use reqwest::Method;
6
7use crate::storage::error::{StorageError, StorageResult};
8
9use super::transport::ResponsePayload;
10
11pub type ResponseHandler<O> = Arc<dyn Fn(ResponsePayload) -> StorageResult<O> + Send + Sync>;
12pub type ErrorHandler = Arc<dyn Fn(ResponsePayload, StorageError) -> StorageError + Send + Sync>;
13
14#[derive(Clone, Debug)]
15pub enum RequestBody {
16 Bytes(Vec<u8>),
17 Text(String),
18 Empty,
19}
20
21impl RequestBody {
22 pub fn is_empty(&self) -> bool {
23 matches!(self, RequestBody::Empty)
24 }
25}
26
27pub struct RequestInfo<O> {
28 pub url: String,
29 pub method: Method,
30 pub headers: HashMap<String, String>,
31 pub body: RequestBody,
32 pub success_codes: Vec<u16>,
33 pub additional_retry_codes: Vec<u16>,
34 pub timeout: Duration,
35 pub query_params: HashMap<String, String>,
36 pub response_handler: ResponseHandler<O>,
37 pub error_handler: Option<ErrorHandler>,
38}
39
40impl<O> Clone for RequestInfo<O> {
41 fn clone(&self) -> Self {
42 Self {
43 url: self.url.clone(),
44 method: self.method.clone(),
45 headers: self.headers.clone(),
46 body: self.body.clone(),
47 success_codes: self.success_codes.clone(),
48 additional_retry_codes: self.additional_retry_codes.clone(),
49 timeout: self.timeout,
50 query_params: self.query_params.clone(),
51 response_handler: Arc::clone(&self.response_handler),
52 error_handler: self.error_handler.as_ref().map(Arc::clone),
53 }
54 }
55}
56
57impl<O> RequestInfo<O> {
58 pub fn new(
59 url: impl Into<String>,
60 method: Method,
61 timeout: Duration,
62 response_handler: ResponseHandler<O>,
63 ) -> Self {
64 Self {
65 url: url.into(),
66 method,
67 headers: HashMap::new(),
68 body: RequestBody::Empty,
69 success_codes: vec![200],
70 additional_retry_codes: Vec::new(),
71 timeout,
72 query_params: HashMap::new(),
73 response_handler,
74 error_handler: None,
75 }
76 }
77
78 pub fn with_body(mut self, body: RequestBody) -> Self {
79 self.body = body;
80 self
81 }
82
83 pub fn with_headers(mut self, headers: HashMap<String, String>) -> Self {
84 self.headers = headers;
85 self
86 }
87
88 pub fn with_query_param(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
89 self.query_params.insert(key.into(), value.into());
90 self
91 }
92
93 pub fn with_success_codes(mut self, codes: Vec<u16>) -> Self {
94 self.success_codes = codes;
95 self
96 }
97
98 pub fn with_additional_retry_codes(mut self, codes: Vec<u16>) -> Self {
99 self.additional_retry_codes = codes;
100 self
101 }
102
103 pub fn with_error_handler(mut self, handler: ErrorHandler) -> Self {
104 self.error_handler = Some(handler);
105 self
106 }
107}