psf / psf/requests

Returning cookies from Adapters

Open
#4,214 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
54.3k
Forks
10.5k
Avg merge
16h 43m
Merged PRs (30d)
3

Description

I've been trying to create a subclass of BaseAdapter for Requests that supports WSGI applications, (preferably based on the requests-wsgi-adapter package, or from scratch if need be).

The sticking point has been cookies and session handling. From reading the Requests code, my understanding is that cookies are automatically extracted from the reponse object returned from the adapter's send method. In order for the cookies to be automatically extracted the response needs to have a r.raw._original_response.msg chain of attributes and this should return an object with an info method that should return an email.message.Message instance (largely because of cookies.py:extract_cookies_to_jar, but also because of how http.cookiejar behaves).

This seems like a needlessly complicated series of steps to go through, particularly as I'm not currently clear on how to correctly convert my response body to an email.message.Message (and a quick scan of the Python documentation makes this look non-trivial).

Have I misunderstood? Are there any helper functions for performing these steps?

Would you consider a patch to extract_cookies_to_jar that also looks, for example, for a _cookies_dict property on the response, at the point that it currently silently returns if there is no _original_response attribute?

Alternatively, would you consider supporting something like https://pypi.python.org/pypi/cookies/ with a friendlier interface (kind of "Requests for cookies")? I haven't dug into it a lot, but it looks relatively easy to add optional support for it (which I'd be happy to contribute).

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with cookies.py:extract_cookies_to_jar and the BaseAdapter send response path, then review how the requests-wsgi-adapter handles cookies. The issue presents several alternatives rather than a settled change, so done would first require an agreed API and acceptance criteria, followed by tests for adapter cookie extraction.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
networking
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.