HTTP or IMAP: How To Prevent Headers From Getting Complicated

March 31st 2020
Author profile picture

@OusretAhmed TAHRI

https://github.com/Ousret

LinkedIn social iconTwitter social icongithub social icon

Why something so simple keeps bothering us?

No matters your religion, IMAP4 or HTTP, you should not worry 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 a more important part of the code. In the face of that I say no more!
Do not forget that headers are not OneToOne. One header can be repeated multiple times and attributes can have multiple values within the same header. So representing them by using a
dict()
is not a good idea even if using case insensible dict !
charset = headers.content_type.charset # better now ?

Project Kiss-Headers

Kiss-Headers is a library that allows you to handle with great care 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 clear that they were something missing in the python community. From the project
psf/requests
to
tornado
, each one has its implementation, each one is 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 your beloved IDE. It should dramatically reduce stress and pain when encountering them.

Features

  • A backward compatible syntax using bracket style.
  • Capable to alter headers using simple operator notation + and - like a human would do.
  • It does not care if headers are from IMAP4 or HTTP, do as you need with one library.
  • Ability to parse any object and extract from it recognized headers.
  • Fully type annotated.
  • Provide great auto-completion in python interpreter or any capable IDE.
  • 90% test coverage.
Plus all the features that you would expect from handling headers...
  • Properties syntax for headers and attributes in header.
  • Support headers and attributes OneToOne and OneToMany.
  • Capable of parsing bytes, fp, str, dict and requests.Response.
  • Automatically unquote the value of an attribute when retrieving it.
  • Case insensible on header name and attribute key.
  • Character - equal _ in addition to the above feature.
  • Any syntax you like. We like.
Imagine being capable of doing those things :

Endnote

I am pretty confident that those kinds 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 respects those criteria : (i) repeatably reinventing the wheel (ii) everyone is facing this issue at least once. (iii) break the loop by contributing.
Thank you :)

Comments

Tags

The Noonification banner

Subscribe to get your daily round-up of top tech stories!