From b889a9f8410e16af15cf7f3b4502c4784f21b12e Mon Sep 17 00:00:00 2001 From: jesopo Date: Tue, 3 Dec 2019 13:00:43 +0000 Subject: [PATCH] add utils.http.Session object, to preserve cookies across requests --- src/utils/http.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/utils/http.py b/src/utils/http.py index 83d6523b..eda5b947 100644 --- a/src/utils/http.py +++ b/src/utils/http.py @@ -205,6 +205,20 @@ def _request(request_obj: Request) -> Response: return response +class Session(object): + def __init__(self): + self._cookies: typing.Dict[str, str] = {} + def __enter__(self): + pass + def __exit__(self): + self._cookies.clear() + + def request(self, request: Request) -> Response: + request.cookies.update(self._cookies) + response = _request(request) + self._cookies.update(response.cookies) + return response + class RequestManyException(Exception): pass def request_many(requests: typing.List[Request]) -> typing.Dict[str, Response]: