Skip to main content

Headers: Keep-It=Simple; And=Stupid.

Project description

Welcome to Headers for Human 👋

Temporary logo
Imagine you could combine advantages of many representations, with auto-completion!
Download Count /Month License: MIT PyPi Publish Action Code style: black Checked with mypy Download Count Total

❓ Why

No matter if you are currently building software using HTTP or IMAP, you should not worry about easily accessing header and associated attributes, adjectives or values.

using kiss-headers from python interpreter

I have seen so many chunks of code trying to deal with these headers; often I saw this implementation :

charset = headers['Content-Type'].split(';')[-1].split('=')[-1].replace('"', '')

No more of that ! 🤮

🔪 Features

kiss-headers is a library on steroids that allow you to handle headers with great care.

  • A backwards-compatible syntax using bracket style.
  • Capability to alter headers using simple, human-readable operator notation + and -.
  • Flexibility if headers are from IMAP4 or HTTP, use as you need with one library.
  • Ability to parse any object and extract recognized headers from it.
  • 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 attribute in header.
  • Supports headers and attributes OneToOne and OneToMany.
  • Capable of parsing bytes, fp, str, dict, requests.Response and httpx._models.Response.
  • Automatically unquote value of an attribute when retrieving it.
  • Case insensitive with header name and attribute key.
  • Character - equal _ in addition of above feature.
  • Any syntax you like, we like.

Your support

Please 🌟 this repository if this project helped you! ✨ That would be very much appreciated. ✨

✨ Installation

Whatever you like, use pipenv or pip, it simply works. Requires Python 3.6+ installed.

pip install kiss-headers

🍰 Usage

parse_it() method takes bytes, str, fp, dict or even requests.Response or httpx._models.Response itself and returns a Headers object.

from requests import get
from kiss_headers import parse_it

response = get('https://www.google.fr')
headers = parse_it(response)

headers.content_type.charset  # output: ISO-8859-1

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.

from kiss_headers import parse_it

my_cookies = """set-cookie: 1P_JAR=2020-03-16-21; expires=Wed, 15-Apr-2020 21:27:31 GMT; path=/; domain=.google.fr; Secure; SameSite=none
set-cookie: CONSENT=WP.284b10; expires=Fri, 01-Jan-2038 00:00:00 GMT; path=/; domain=.google.fr"""

headers = parse_it(my_cookies)

type(headers.set_cookie)  # output: list
headers.set_cookie[0].expires # output Wed, 15-Apr-2020 21:27:31 GMT

If this behaviour does bother you, you can lock output to always be a list. Just call lock_output_type() method.

Just a note: Accessing a header that has the same name as a reserved keyword must be done this way :

headers = parse_it('From: Ousret; origin=www.github.com\nIS: 1\nWhile: Not-True')

# this flavour
headers.from_ # to access From, just add a single underscore to it
# or..
headers['from']

📜 Documentation

See the full documentation for advanced usages : www.kiss-headers.tech

👤 Contributing

Contributions, issues and feature requests are very much welcome.
Feel free to check issues page if you want to contribute.

📝 License

Copyright © 2020 Ahmed TAHRI @Ousret.
This project is MIT licensed.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kiss-headers-1.1.1.tar.gz (15.1 kB view hashes)

Uploaded Source

Built Distribution

kiss_headers-1.1.1-py3-none-any.whl (13.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page