Rust's reqwest: Elegant Persistence
"TLDR: This article introduces the implementation and solutions for cookie persistence in Rust's reqwest library. By analyzing the limitation that the default Jar implementation does not support persistence, and how to combine the external libraries cookie_store::CookieStore and reqwest_cookie_store through the adapter pattern, it extends the CookieStore trait to support persistence functionality."
Actually quite boring, even complicated
flowchart TB
T["trait: reqwest::cookie::CookieStore"]
subgraph Objects implementing the trait
A2["Jar\n(Default implementation, wraps cookie_store::CookieStore, hides persistence)"]
C1["CookieStoreMutex\n(Adapter, wraps cookie_store::CookieStore, exposes persistence capabilities)"]
end
subgraph External library
B1["cookie_store::CookieStore\n(supports persistence)"]
end
subgraph reqwest
A1["When using reqwest, you can inject\nany object implementing the CookieStore trait"]
end
A2 --> T
C1 --> T
A1 --> T
A2 -. internally wraps .-> B1
C1 --> B1
-
reqwest itself does not support cookies
-
Using the default Jar implementation enables Cookie support
-
However, after the program restarts, the cookies are gone, so persistence is needed
-
Persistence requires another library: reqwest_cookie_store, which both implements the trait and wraps cookie_store::CookieStore, serving as an adapter to inject into reqwest
#Technical Notes/rust