Source code for human_requests.abstraction.cookies

from dataclasses import dataclass, field
from datetime import datetime
from typing import Any, Iterable, Iterator, Literal, Mapping
from urllib.parse import urlsplit

from playwright.async_api import StorageStateCookie


@dataclass




@dataclass
[docs] class CookieManager: """Convenient jar-style wrapper + Playwright conversion."""
[docs] storage: list[Cookie] = field(default_factory=list)
# ────── dunder helpers ──────
[docs] def __iter__(self) -> Iterator[Cookie]: return iter(self.storage)
[docs] def __len__(self) -> int: return len(self.storage)
[docs] def __bool__(self) -> bool: return bool(self.storage)
# ────── CRUD ──────
[docs] def get(self, name: str, domain: str | None = None, path: str | None = None) -> Cookie | None: """Get a cookie by name, domain, and path.""" return next( ( c for c in self.storage if c.name == name and (domain is None or c.domain == domain) and (path is None or c.path == path) ), None, )
[docs] def get_for_domain(self, url_or_domain: str) -> list[Cookie]: """Get all cookies available for a domain/URL.""" host = urlsplit(url_or_domain).hostname or url_or_domain.split(":")[0] if not host: return [] def _match(cookie_domain: str, h: str) -> bool: return h == cookie_domain or h.endswith("." + cookie_domain) return [c for c in self.storage if _match(c.domain, host)]
[docs] def add(self, cookie: Cookie | Iterable[Cookie]) -> None: """Add a cookie or cookies.""" def _add_one(c: Cookie) -> None: key = (c.domain, c.path, c.name) for i, old in enumerate(self.storage): if (old.domain, old.path, old.name) == key: self.storage[i] = c break else: self.storage.append(c) if isinstance(cookie, Iterable) and not isinstance(cookie, Cookie): for c in cookie: _add_one(c) else: _add_one(cookie)
[docs] def delete( self, name: str, domain: str | None = None, path: str | None = None ) -> Cookie | None: """Delete a cookie by name, domain, and path.""" for i, c in enumerate(self.storage): if ( c.name == name and (domain is None or c.domain == domain) and (path is None or c.path == path) ): return self.storage.pop(i) return None
# ────── Playwright helpers ──────
[docs] def to_playwright(self) -> list[StorageStateCookie]: """Serialize all cookies into a format understood by Playwright.""" return [c.to_playwright_like_dict() for c in self.storage]
[docs] def add_from_playwright(self, raw_cookies: Iterable[Mapping[str, Any]]) -> None: """Inverse operation — add a list of Playwright cookies/mappings to the jar.""" self.add(Cookie.from_playwright_like_dict(rc) for rc in raw_cookies)