mirror of
https://github.com/Garmelon/PFERD.git
synced 2023-12-21 10:23:01 +01:00
Add some docs to cookie_jar
This commit is contained in:
parent
b2fe7cc064
commit
f3d3d6bb65
@ -1,3 +1,5 @@
|
|||||||
|
"""A helper for requests cookies."""
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
from http.cookiejar import LoadError, LWPCookieJar
|
from http.cookiejar import LoadError, LWPCookieJar
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
@ -7,9 +9,15 @@ import requests
|
|||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class CookieJar:
|
class CookieJar:
|
||||||
|
"""A cookie jar that can be persisted."""
|
||||||
|
|
||||||
def __init__(self, cookie_file: Optional[Path] = None) -> None:
|
def __init__(self, cookie_file: Optional[Path] = None) -> None:
|
||||||
|
"""Create a new cookie jar at the given path.
|
||||||
|
|
||||||
|
If the path is None, the cookies will not be persisted.
|
||||||
|
"""
|
||||||
self._cookies: LWPCookieJar
|
self._cookies: LWPCookieJar
|
||||||
if cookie_file is None:
|
if cookie_file is None:
|
||||||
self._cookies = LWPCookieJar()
|
self._cookies = LWPCookieJar()
|
||||||
@ -18,9 +26,11 @@ class CookieJar:
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def cookies(self) -> LWPCookieJar:
|
def cookies(self) -> LWPCookieJar:
|
||||||
|
"""Return the requests cookie jar."""
|
||||||
return self._cookies
|
return self._cookies
|
||||||
|
|
||||||
def load_cookies(self) -> None:
|
def load_cookies(self) -> None:
|
||||||
|
"""Load all cookies from the file given in the constructor."""
|
||||||
if self._cookies.filename is None:
|
if self._cookies.filename is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
@ -34,6 +44,7 @@ class CookieJar:
|
|||||||
)
|
)
|
||||||
|
|
||||||
def save_cookies(self, reason: Optional[str] = None) -> None:
|
def save_cookies(self, reason: Optional[str] = None) -> None:
|
||||||
|
"""Save the cookies in the file given in the constructor."""
|
||||||
if self._cookies.filename is None:
|
if self._cookies.filename is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
@ -47,11 +58,12 @@ class CookieJar:
|
|||||||
self._cookies.save(ignore_discard=True)
|
self._cookies.save(ignore_discard=True)
|
||||||
|
|
||||||
def create_session(self) -> requests.Session:
|
def create_session(self) -> requests.Session:
|
||||||
|
"""Create a new session using the cookie jar."""
|
||||||
sess = requests.Session()
|
sess = requests.Session()
|
||||||
|
|
||||||
# From the request docs: "All requests code should work out of the box
|
# From the request docs: "All requests code should work out of the box
|
||||||
# with externally provided instances of CookieJar, e.g. LWPCookieJar
|
# with externally provided instances of CookieJar, e.g. LWPCookieJar
|
||||||
# and FileCookieJar."
|
# and FileCookieJar."
|
||||||
sess.cookies = self.cookies # type: ignore
|
sess.cookies = self.cookies # type: ignore
|
||||||
|
|
||||||
return sess
|
return sess
|
||||||
|
Loading…
Reference in New Issue
Block a user