asks
asks copied to clipboard
Switch out cookie handling in favor of stdlib `http.cookies`
Mentioned in #22 (comment).
Decided to create a separate ticket for tracking this refactor. Still wrapping my head around the codebase, but I think @theelous3 should be able to explain a little more about what's going on here.
I'm not even going to try to guess right now given how much of a noob I am around here, ha.
Currently the Cookie
objects returned along with Response
objects are homebrew nonsense. They're not entirely useless but certainly contribute to the NIH factor of the lib as a whole. The goal here is to use python's http.cookies
instead, to offer a more standardised and delicious cookie experience.
I'm not entirely enthused by the stdlib cookies however. They requires some kind of ugly code. For example, when returning a cookie as a client you've to do some wangjangle like SimpleCookie.output(header='Cookie:')
. For sending from serverside you've to send the str of the cookie, like so: str(SimpleCookie)
. The opposing use cases use wildly asymetrical actions to generate their directly complimentary parts. Yuck.
I bring this up just to put out the idea that I'm not at all against using a third party cookie library if it offers a nicer overall api and is reasonably compatible with the expected api of a stdlib / requests cookie.
They're not completely asymetrical. This is probably what you want:
>>> from http.cookies import SimpleCookie
>>> c = SimpleCookie()
>>> c['lol'] = 'wat'
>>> c.output(header='Cookie:')
'Cookie: lol=wat'
>>> c.output(header='Set-Cookie:') # default value btw
'Set-Cookie: lol=wat'
You see this because __str__ = output
:)