Why ? Indeed Why ?!
No matters your religion, IMAP4 or HTTP, you should not worries about accessing easily header and associated attributes, adjectives or values.
But even with modern language like Python we still have those :
charset = headers['Content-Type'].split(';')[-1].split('=')[-1]
I have seen so much chunk of code like this, trying to deal with them, often in a risky way to take care of more important part of the code. In face of that I say no more !
Do not forget that headers are not 1 TO 1. One header can be repeated multiple time and attribute can have multiple value within the same header. So representing them by using a ´dict()´ is not a good idea even if using case insensible dict !
Project Kiss-Headers
That is why I created a project that would remove the pain of dealing with them no matter where they came from.
It was absolutely clear that they was something missing in the python community. From the project ´psf/requests´ to ´tornado´, each one have it own implementation, each one are also incomplete. The best so far I have seen is the one from ´httpx´ project.
Mine is not perfect, but has its amount of sweetness. Beginning from auto-completion capability in python interpreter or in your beloved IDE. It should dramatically reduce stress and pain when encountering them.
Ousret / kiss-headers
Python package for HTTP/1.1 style headers. Parse headers to objects. Most advanced available structure for http headers.
HTTP Headers, the Complete Toolkit 🧰
Object-oriented headers. Kind of structured headers.
❓ Why
No matter if you are currently dealing with code using HTTP or IMAP (message, email), you should not worry about easily accessing and exploiting headers.
I have seen so many chunks of code trying to deal with these headers; often I saw this implementation:
# No more of that!
charset = headers['Content-Type'].split(';')[-1].split('=')[-1].replace('"', '')
# That too..
response = get(
"https://httpbin.org/headers",
headers={
"user-agent": "custom/2.22",
"referer": "https://www.google.com",
"accept": "text/html",
"accept-language": "en-US",
"custom-header-xyz": "hello; charset=utf-8"
}
)
Scroll down and see how you could do it from now on.
🔪 Features
kiss-headers
is a basic library that allow you to handle headers as…
Imaging being capable of doing those things :
End note
I am pretty confident that those kind of libraries that serve reducing time spent on the little things would actually help make the life of a programmer easier. And spent a lot more time on something else.
So, do not hesitate, when you are encountering a case that respect those criteria : (i) stupid (ii) repeatably reinventing the wheel (iii) everyone is facing this issue at least once. (iv) break the loop by contributing.
Thank you :)
Top comments (0)