Skip to main content

browser_protocol/cachestorage/
mod.rs

1use serde::{Serialize, Deserialize};
2use serde_json::Value as JsonValue;
3use std::borrow::Cow;
4
5/// Unique identifier of the Cache object.
6
7pub type CacheId<'a> = Cow<'a, str>;
8
9/// type of HTTP response cached
10
11#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
12pub enum CachedResponseType {
13    #[default]
14    #[serde(rename = "basic")]
15    Basic,
16    #[serde(rename = "cors")]
17    Cors,
18    #[serde(rename = "default")]
19    Default,
20    #[serde(rename = "error")]
21    Error,
22    #[serde(rename = "opaqueResponse")]
23    OpaqueResponse,
24    #[serde(rename = "opaqueRedirect")]
25    OpaqueRedirect,
26}
27
28/// Data entry.
29
30#[derive(Debug, Clone, Serialize, Deserialize, Default)]
31#[serde(rename_all = "camelCase")]
32pub struct DataEntry<'a> {
33    /// Request URL.
34    requestURL: Cow<'a, str>,
35    /// Request method.
36    requestMethod: Cow<'a, str>,
37    /// Request headers
38    requestHeaders: Vec<Header<'a>>,
39    /// Number of seconds since epoch.
40    responseTime: f64,
41    /// HTTP response status code.
42    responseStatus: i64,
43    /// HTTP response status text.
44    responseStatusText: Cow<'a, str>,
45    /// HTTP response type
46    responseType: CachedResponseType,
47    /// Response headers
48    responseHeaders: Vec<Header<'a>>,
49}
50
51impl<'a> DataEntry<'a> {
52    pub fn builder(requestURL: impl Into<Cow<'a, str>>, requestMethod: impl Into<Cow<'a, str>>, requestHeaders: Vec<Header<'a>>, responseTime: f64, responseStatus: i64, responseStatusText: impl Into<Cow<'a, str>>, responseType: CachedResponseType, responseHeaders: Vec<Header<'a>>) -> DataEntryBuilder<'a> {
53        DataEntryBuilder {
54            requestURL: requestURL.into(),
55            requestMethod: requestMethod.into(),
56            requestHeaders: requestHeaders,
57            responseTime: responseTime,
58            responseStatus: responseStatus,
59            responseStatusText: responseStatusText.into(),
60            responseType: responseType,
61            responseHeaders: responseHeaders,
62        }
63    }
64    pub fn requestURL(&self) -> &str { self.requestURL.as_ref() }
65    pub fn requestMethod(&self) -> &str { self.requestMethod.as_ref() }
66    pub fn requestHeaders(&self) -> &[Header<'a>] { &self.requestHeaders }
67    pub fn responseTime(&self) -> f64 { self.responseTime }
68    pub fn responseStatus(&self) -> i64 { self.responseStatus }
69    pub fn responseStatusText(&self) -> &str { self.responseStatusText.as_ref() }
70    pub fn responseType(&self) -> &CachedResponseType { &self.responseType }
71    pub fn responseHeaders(&self) -> &[Header<'a>] { &self.responseHeaders }
72}
73
74
75pub struct DataEntryBuilder<'a> {
76    requestURL: Cow<'a, str>,
77    requestMethod: Cow<'a, str>,
78    requestHeaders: Vec<Header<'a>>,
79    responseTime: f64,
80    responseStatus: i64,
81    responseStatusText: Cow<'a, str>,
82    responseType: CachedResponseType,
83    responseHeaders: Vec<Header<'a>>,
84}
85
86impl<'a> DataEntryBuilder<'a> {
87    pub fn build(self) -> DataEntry<'a> {
88        DataEntry {
89            requestURL: self.requestURL,
90            requestMethod: self.requestMethod,
91            requestHeaders: self.requestHeaders,
92            responseTime: self.responseTime,
93            responseStatus: self.responseStatus,
94            responseStatusText: self.responseStatusText,
95            responseType: self.responseType,
96            responseHeaders: self.responseHeaders,
97        }
98    }
99}
100
101/// Cache identifier.
102
103#[derive(Debug, Clone, Serialize, Deserialize, Default)]
104#[serde(rename_all = "camelCase")]
105pub struct Cache<'a> {
106    /// An opaque unique id of the cache.
107    cacheId: CacheId<'a>,
108    /// Security origin of the cache.
109    securityOrigin: Cow<'a, str>,
110    /// Storage key of the cache.
111    storageKey: Cow<'a, str>,
112    /// Storage bucket of the cache.
113    #[serde(skip_serializing_if = "Option::is_none")]
114    storageBucket: Option<crate::storage::StorageBucket<'a>>,
115    /// The name of the cache.
116    cacheName: Cow<'a, str>,
117}
118
119impl<'a> Cache<'a> {
120    pub fn builder(cacheId: CacheId<'a>, securityOrigin: impl Into<Cow<'a, str>>, storageKey: impl Into<Cow<'a, str>>, cacheName: impl Into<Cow<'a, str>>) -> CacheBuilder<'a> {
121        CacheBuilder {
122            cacheId: cacheId,
123            securityOrigin: securityOrigin.into(),
124            storageKey: storageKey.into(),
125            storageBucket: None,
126            cacheName: cacheName.into(),
127        }
128    }
129    pub fn cacheId(&self) -> &CacheId<'a> { &self.cacheId }
130    pub fn securityOrigin(&self) -> &str { self.securityOrigin.as_ref() }
131    pub fn storageKey(&self) -> &str { self.storageKey.as_ref() }
132    pub fn storageBucket(&self) -> Option<&crate::storage::StorageBucket<'a>> { self.storageBucket.as_ref() }
133    pub fn cacheName(&self) -> &str { self.cacheName.as_ref() }
134}
135
136
137pub struct CacheBuilder<'a> {
138    cacheId: CacheId<'a>,
139    securityOrigin: Cow<'a, str>,
140    storageKey: Cow<'a, str>,
141    storageBucket: Option<crate::storage::StorageBucket<'a>>,
142    cacheName: Cow<'a, str>,
143}
144
145impl<'a> CacheBuilder<'a> {
146    /// Storage bucket of the cache.
147    pub fn storageBucket(mut self, storageBucket: crate::storage::StorageBucket<'a>) -> Self { self.storageBucket = Some(storageBucket); self }
148    pub fn build(self) -> Cache<'a> {
149        Cache {
150            cacheId: self.cacheId,
151            securityOrigin: self.securityOrigin,
152            storageKey: self.storageKey,
153            storageBucket: self.storageBucket,
154            cacheName: self.cacheName,
155        }
156    }
157}
158
159
160#[derive(Debug, Clone, Serialize, Deserialize, Default)]
161#[serde(rename_all = "camelCase")]
162pub struct Header<'a> {
163    name: Cow<'a, str>,
164    value: Cow<'a, str>,
165}
166
167impl<'a> Header<'a> {
168    pub fn builder(name: impl Into<Cow<'a, str>>, value: impl Into<Cow<'a, str>>) -> HeaderBuilder<'a> {
169        HeaderBuilder {
170            name: name.into(),
171            value: value.into(),
172        }
173    }
174    pub fn name(&self) -> &str { self.name.as_ref() }
175    pub fn value(&self) -> &str { self.value.as_ref() }
176}
177
178
179pub struct HeaderBuilder<'a> {
180    name: Cow<'a, str>,
181    value: Cow<'a, str>,
182}
183
184impl<'a> HeaderBuilder<'a> {
185    pub fn build(self) -> Header<'a> {
186        Header {
187            name: self.name,
188            value: self.value,
189        }
190    }
191}
192
193/// Cached response
194
195#[derive(Debug, Clone, Serialize, Deserialize, Default)]
196#[serde(rename_all = "camelCase")]
197pub struct CachedResponse<'a> {
198    /// Entry content, base64-encoded. (Encoded as a base64 string when passed over JSON)
199    body: Cow<'a, str>,
200}
201
202impl<'a> CachedResponse<'a> {
203    pub fn builder(body: impl Into<Cow<'a, str>>) -> CachedResponseBuilder<'a> {
204        CachedResponseBuilder {
205            body: body.into(),
206        }
207    }
208    pub fn body(&self) -> &str { self.body.as_ref() }
209}
210
211
212pub struct CachedResponseBuilder<'a> {
213    body: Cow<'a, str>,
214}
215
216impl<'a> CachedResponseBuilder<'a> {
217    pub fn build(self) -> CachedResponse<'a> {
218        CachedResponse {
219            body: self.body,
220        }
221    }
222}
223
224/// Deletes a cache.
225
226#[derive(Debug, Clone, Serialize, Deserialize, Default)]
227#[serde(rename_all = "camelCase")]
228pub struct DeleteCacheParams<'a> {
229    /// Id of cache for deletion.
230    cacheId: CacheId<'a>,
231}
232
233impl<'a> DeleteCacheParams<'a> {
234    pub fn builder(cacheId: CacheId<'a>) -> DeleteCacheParamsBuilder<'a> {
235        DeleteCacheParamsBuilder {
236            cacheId: cacheId,
237        }
238    }
239    pub fn cacheId(&self) -> &CacheId<'a> { &self.cacheId }
240}
241
242
243pub struct DeleteCacheParamsBuilder<'a> {
244    cacheId: CacheId<'a>,
245}
246
247impl<'a> DeleteCacheParamsBuilder<'a> {
248    pub fn build(self) -> DeleteCacheParams<'a> {
249        DeleteCacheParams {
250            cacheId: self.cacheId,
251        }
252    }
253}
254
255impl<'a> DeleteCacheParams<'a> { pub const METHOD: &'static str = "CacheStorage.deleteCache"; }
256
257impl<'a> crate::CdpCommand<'a> for DeleteCacheParams<'a> {
258    const METHOD: &'static str = "CacheStorage.deleteCache";
259    type Response = crate::EmptyReturns;
260}
261
262/// Deletes a cache entry.
263
264#[derive(Debug, Clone, Serialize, Deserialize, Default)]
265#[serde(rename_all = "camelCase")]
266pub struct DeleteEntryParams<'a> {
267    /// Id of cache where the entry will be deleted.
268    cacheId: CacheId<'a>,
269    /// URL spec of the request.
270    request: Cow<'a, str>,
271}
272
273impl<'a> DeleteEntryParams<'a> {
274    pub fn builder(cacheId: CacheId<'a>, request: impl Into<Cow<'a, str>>) -> DeleteEntryParamsBuilder<'a> {
275        DeleteEntryParamsBuilder {
276            cacheId: cacheId,
277            request: request.into(),
278        }
279    }
280    pub fn cacheId(&self) -> &CacheId<'a> { &self.cacheId }
281    pub fn request(&self) -> &str { self.request.as_ref() }
282}
283
284
285pub struct DeleteEntryParamsBuilder<'a> {
286    cacheId: CacheId<'a>,
287    request: Cow<'a, str>,
288}
289
290impl<'a> DeleteEntryParamsBuilder<'a> {
291    pub fn build(self) -> DeleteEntryParams<'a> {
292        DeleteEntryParams {
293            cacheId: self.cacheId,
294            request: self.request,
295        }
296    }
297}
298
299impl<'a> DeleteEntryParams<'a> { pub const METHOD: &'static str = "CacheStorage.deleteEntry"; }
300
301impl<'a> crate::CdpCommand<'a> for DeleteEntryParams<'a> {
302    const METHOD: &'static str = "CacheStorage.deleteEntry";
303    type Response = crate::EmptyReturns;
304}
305
306/// Requests cache names.
307
308#[derive(Debug, Clone, Serialize, Deserialize, Default)]
309#[serde(rename_all = "camelCase")]
310pub struct RequestCacheNamesParams<'a> {
311    /// At least and at most one of securityOrigin, storageKey, storageBucket must be specified.
312    /// Security origin.
313    #[serde(skip_serializing_if = "Option::is_none")]
314    securityOrigin: Option<Cow<'a, str>>,
315    /// Storage key.
316    #[serde(skip_serializing_if = "Option::is_none")]
317    storageKey: Option<Cow<'a, str>>,
318    /// Storage bucket. If not specified, it uses the default bucket.
319    #[serde(skip_serializing_if = "Option::is_none")]
320    storageBucket: Option<crate::storage::StorageBucket<'a>>,
321}
322
323impl<'a> RequestCacheNamesParams<'a> {
324    pub fn builder() -> RequestCacheNamesParamsBuilder<'a> {
325        RequestCacheNamesParamsBuilder {
326            securityOrigin: None,
327            storageKey: None,
328            storageBucket: None,
329        }
330    }
331    pub fn securityOrigin(&self) -> Option<&str> { self.securityOrigin.as_deref() }
332    pub fn storageKey(&self) -> Option<&str> { self.storageKey.as_deref() }
333    pub fn storageBucket(&self) -> Option<&crate::storage::StorageBucket<'a>> { self.storageBucket.as_ref() }
334}
335
336#[derive(Default)]
337pub struct RequestCacheNamesParamsBuilder<'a> {
338    securityOrigin: Option<Cow<'a, str>>,
339    storageKey: Option<Cow<'a, str>>,
340    storageBucket: Option<crate::storage::StorageBucket<'a>>,
341}
342
343impl<'a> RequestCacheNamesParamsBuilder<'a> {
344    /// At least and at most one of securityOrigin, storageKey, storageBucket must be specified.
345    /// Security origin.
346    pub fn securityOrigin(mut self, securityOrigin: impl Into<Cow<'a, str>>) -> Self { self.securityOrigin = Some(securityOrigin.into()); self }
347    /// Storage key.
348    pub fn storageKey(mut self, storageKey: impl Into<Cow<'a, str>>) -> Self { self.storageKey = Some(storageKey.into()); self }
349    /// Storage bucket. If not specified, it uses the default bucket.
350    pub fn storageBucket(mut self, storageBucket: crate::storage::StorageBucket<'a>) -> Self { self.storageBucket = Some(storageBucket); self }
351    pub fn build(self) -> RequestCacheNamesParams<'a> {
352        RequestCacheNamesParams {
353            securityOrigin: self.securityOrigin,
354            storageKey: self.storageKey,
355            storageBucket: self.storageBucket,
356        }
357    }
358}
359
360/// Requests cache names.
361
362#[derive(Debug, Clone, Serialize, Deserialize, Default)]
363#[serde(rename_all = "camelCase")]
364pub struct RequestCacheNamesReturns<'a> {
365    /// Caches for the security origin.
366    caches: Vec<Cache<'a>>,
367}
368
369impl<'a> RequestCacheNamesReturns<'a> {
370    pub fn builder(caches: Vec<Cache<'a>>) -> RequestCacheNamesReturnsBuilder<'a> {
371        RequestCacheNamesReturnsBuilder {
372            caches: caches,
373        }
374    }
375    pub fn caches(&self) -> &[Cache<'a>] { &self.caches }
376}
377
378
379pub struct RequestCacheNamesReturnsBuilder<'a> {
380    caches: Vec<Cache<'a>>,
381}
382
383impl<'a> RequestCacheNamesReturnsBuilder<'a> {
384    pub fn build(self) -> RequestCacheNamesReturns<'a> {
385        RequestCacheNamesReturns {
386            caches: self.caches,
387        }
388    }
389}
390
391impl<'a> RequestCacheNamesParams<'a> { pub const METHOD: &'static str = "CacheStorage.requestCacheNames"; }
392
393impl<'a> crate::CdpCommand<'a> for RequestCacheNamesParams<'a> {
394    const METHOD: &'static str = "CacheStorage.requestCacheNames";
395    type Response = RequestCacheNamesReturns<'a>;
396}
397
398/// Fetches cache entry.
399
400#[derive(Debug, Clone, Serialize, Deserialize, Default)]
401#[serde(rename_all = "camelCase")]
402pub struct RequestCachedResponseParams<'a> {
403    /// Id of cache that contains the entry.
404    cacheId: CacheId<'a>,
405    /// URL spec of the request.
406    requestURL: Cow<'a, str>,
407    /// headers of the request.
408    requestHeaders: Vec<Header<'a>>,
409}
410
411impl<'a> RequestCachedResponseParams<'a> {
412    pub fn builder(cacheId: CacheId<'a>, requestURL: impl Into<Cow<'a, str>>, requestHeaders: Vec<Header<'a>>) -> RequestCachedResponseParamsBuilder<'a> {
413        RequestCachedResponseParamsBuilder {
414            cacheId: cacheId,
415            requestURL: requestURL.into(),
416            requestHeaders: requestHeaders,
417        }
418    }
419    pub fn cacheId(&self) -> &CacheId<'a> { &self.cacheId }
420    pub fn requestURL(&self) -> &str { self.requestURL.as_ref() }
421    pub fn requestHeaders(&self) -> &[Header<'a>] { &self.requestHeaders }
422}
423
424
425pub struct RequestCachedResponseParamsBuilder<'a> {
426    cacheId: CacheId<'a>,
427    requestURL: Cow<'a, str>,
428    requestHeaders: Vec<Header<'a>>,
429}
430
431impl<'a> RequestCachedResponseParamsBuilder<'a> {
432    pub fn build(self) -> RequestCachedResponseParams<'a> {
433        RequestCachedResponseParams {
434            cacheId: self.cacheId,
435            requestURL: self.requestURL,
436            requestHeaders: self.requestHeaders,
437        }
438    }
439}
440
441/// Fetches cache entry.
442
443#[derive(Debug, Clone, Serialize, Deserialize, Default)]
444#[serde(rename_all = "camelCase")]
445pub struct RequestCachedResponseReturns<'a> {
446    /// Response read from the cache.
447    response: CachedResponse<'a>,
448}
449
450impl<'a> RequestCachedResponseReturns<'a> {
451    pub fn builder(response: CachedResponse<'a>) -> RequestCachedResponseReturnsBuilder<'a> {
452        RequestCachedResponseReturnsBuilder {
453            response: response,
454        }
455    }
456    pub fn response(&self) -> &CachedResponse<'a> { &self.response }
457}
458
459
460pub struct RequestCachedResponseReturnsBuilder<'a> {
461    response: CachedResponse<'a>,
462}
463
464impl<'a> RequestCachedResponseReturnsBuilder<'a> {
465    pub fn build(self) -> RequestCachedResponseReturns<'a> {
466        RequestCachedResponseReturns {
467            response: self.response,
468        }
469    }
470}
471
472impl<'a> RequestCachedResponseParams<'a> { pub const METHOD: &'static str = "CacheStorage.requestCachedResponse"; }
473
474impl<'a> crate::CdpCommand<'a> for RequestCachedResponseParams<'a> {
475    const METHOD: &'static str = "CacheStorage.requestCachedResponse";
476    type Response = RequestCachedResponseReturns<'a>;
477}
478
479/// Requests data from cache.
480
481#[derive(Debug, Clone, Serialize, Deserialize, Default)]
482#[serde(rename_all = "camelCase")]
483pub struct RequestEntriesParams<'a> {
484    /// ID of cache to get entries from.
485    cacheId: CacheId<'a>,
486    /// Number of records to skip.
487    #[serde(skip_serializing_if = "Option::is_none")]
488    skipCount: Option<u64>,
489    /// Number of records to fetch.
490    #[serde(skip_serializing_if = "Option::is_none")]
491    pageSize: Option<u64>,
492    /// If present, only return the entries containing this substring in the path
493    #[serde(skip_serializing_if = "Option::is_none")]
494    pathFilter: Option<Cow<'a, str>>,
495}
496
497impl<'a> RequestEntriesParams<'a> {
498    pub fn builder(cacheId: CacheId<'a>) -> RequestEntriesParamsBuilder<'a> {
499        RequestEntriesParamsBuilder {
500            cacheId: cacheId,
501            skipCount: None,
502            pageSize: None,
503            pathFilter: None,
504        }
505    }
506    pub fn cacheId(&self) -> &CacheId<'a> { &self.cacheId }
507    pub fn skipCount(&self) -> Option<u64> { self.skipCount }
508    pub fn pageSize(&self) -> Option<u64> { self.pageSize }
509    pub fn pathFilter(&self) -> Option<&str> { self.pathFilter.as_deref() }
510}
511
512
513pub struct RequestEntriesParamsBuilder<'a> {
514    cacheId: CacheId<'a>,
515    skipCount: Option<u64>,
516    pageSize: Option<u64>,
517    pathFilter: Option<Cow<'a, str>>,
518}
519
520impl<'a> RequestEntriesParamsBuilder<'a> {
521    /// Number of records to skip.
522    pub fn skipCount(mut self, skipCount: u64) -> Self { self.skipCount = Some(skipCount); self }
523    /// Number of records to fetch.
524    pub fn pageSize(mut self, pageSize: u64) -> Self { self.pageSize = Some(pageSize); self }
525    /// If present, only return the entries containing this substring in the path
526    pub fn pathFilter(mut self, pathFilter: impl Into<Cow<'a, str>>) -> Self { self.pathFilter = Some(pathFilter.into()); self }
527    pub fn build(self) -> RequestEntriesParams<'a> {
528        RequestEntriesParams {
529            cacheId: self.cacheId,
530            skipCount: self.skipCount,
531            pageSize: self.pageSize,
532            pathFilter: self.pathFilter,
533        }
534    }
535}
536
537/// Requests data from cache.
538
539#[derive(Debug, Clone, Serialize, Deserialize, Default)]
540#[serde(rename_all = "camelCase")]
541pub struct RequestEntriesReturns<'a> {
542    /// Array of object store data entries.
543    cacheDataEntries: Vec<DataEntry<'a>>,
544    /// Count of returned entries from this storage. If pathFilter is empty, it
545    /// is the count of all entries from this storage.
546    returnCount: f64,
547}
548
549impl<'a> RequestEntriesReturns<'a> {
550    pub fn builder(cacheDataEntries: Vec<DataEntry<'a>>, returnCount: f64) -> RequestEntriesReturnsBuilder<'a> {
551        RequestEntriesReturnsBuilder {
552            cacheDataEntries: cacheDataEntries,
553            returnCount: returnCount,
554        }
555    }
556    pub fn cacheDataEntries(&self) -> &[DataEntry<'a>] { &self.cacheDataEntries }
557    pub fn returnCount(&self) -> f64 { self.returnCount }
558}
559
560
561pub struct RequestEntriesReturnsBuilder<'a> {
562    cacheDataEntries: Vec<DataEntry<'a>>,
563    returnCount: f64,
564}
565
566impl<'a> RequestEntriesReturnsBuilder<'a> {
567    pub fn build(self) -> RequestEntriesReturns<'a> {
568        RequestEntriesReturns {
569            cacheDataEntries: self.cacheDataEntries,
570            returnCount: self.returnCount,
571        }
572    }
573}
574
575impl<'a> RequestEntriesParams<'a> { pub const METHOD: &'static str = "CacheStorage.requestEntries"; }
576
577impl<'a> crate::CdpCommand<'a> for RequestEntriesParams<'a> {
578    const METHOD: &'static str = "CacheStorage.requestEntries";
579    type Response = RequestEntriesReturns<'a>;
580}