Skip to content

Commit 963532d

Browse files
committed
feat(cookie): add optional cookie jar compression strategy
1 parent a221dc4 commit 963532d

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

python/examples/exceptions.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ async def test_connection_error():
4646
print(f"Other error: {type(e).__name__}: {e}")
4747

4848

49-
5049
async def main():
5150
await test_bad_builder()
5251
await test_timeout_error()

python/rnet/cookie.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,30 @@ class Jar:
9696
9797
This cookie jar can be safely shared across multiple threads and is used
9898
to automatically handle cookies during HTTP requests and responses.
99+
100+
By default, cookie compression is enabled to reduce storage overhead.
101+
Use `uncompressed()` to create a variant without compression if needed.
99102
"""
100103

101-
def __init__(self) -> None:
104+
def __init__(self, compression: int | None = None) -> None:
105+
r"""
106+
Create a new cookie jar with compression enabled by default.
107+
108+
Args:
109+
compression: Optional compression level. If not specified, compression
110+
is enabled automatically for better storage efficiency.
111+
"""
112+
...
113+
114+
def compressed(self) -> "Jar":
115+
r"""
116+
Clone this Jar, sharing storage but enabling compression.
117+
"""
118+
...
119+
120+
def uncompressed(self) -> "Jar":
102121
r"""
103-
Create a new cookie jar.
122+
Clone this Jar, sharing storage but disabling compression.
104123
"""
105124
...
106125

src/cookie.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,22 @@ impl CookieStore for Jar {
207207
impl Jar {
208208
/// Create a new [`Jar`] with an empty cookie store.
209209
#[new]
210-
pub fn new() -> Self {
211-
Jar(Arc::new(wreq::cookie::Jar::default()))
210+
#[pyo3(signature = (compression = None))]
211+
pub fn new(compression: Option<bool>) -> Self {
212+
Self(Arc::new(compression.map_or_else(
213+
wreq::cookie::Jar::default,
214+
wreq::cookie::Jar::new,
215+
)))
216+
}
217+
218+
/// Clone this [`Jar`], sharing storage but enabling compression.
219+
pub fn compreessed(&self) -> Self {
220+
Self(self.0.compressed())
221+
}
222+
223+
/// Clone this [`Jar`], sharing storage but disabling compression.
224+
pub fn uncompressed(&self) -> Self {
225+
Self(self.0.uncompressed())
212226
}
213227

214228
/// Get a cookie by name and URL.

0 commit comments

Comments
 (0)