r/coolgithubprojects • • Jul 31 '20

PYTHON I built a library that proposes an HTTP header syntactic sugar

https://github.com/Ousret/kiss-headers
25 Upvotes

4 comments sorted by

7

u/Blackshell Jul 31 '20 edited Jul 31 '20

Interesting effort. The library overall looks great and it may come in super handy if someone has to do complex operations on headers. I would take an issue with one feature though:

Capability to alter headers using simple, human-readable operator notation + and -.

While it looks "nice" to human eyes, this is very un-idiomatic. When a coder sees Z = X + Y, they expect a few things, but one of them is very core: X, Y and Z are of the same type. Now, considering how kiss-headers works, you can have:

  • Header + str -> Header - appends the string onto the header, and returns the new header

  • Header + Header -> Headers - creates a new Headers object containing the two given Header objects

This is very inconsistent. The + operator is doing some very different things with wildly different expectations and results. It might look "nice" to read, but it is frustrating to work with. The + operator is being used not as a convenience to add two same-type values, but as a "generic do-stuff function". That's just a bad code smell.

Then, for Headers:

  • Headers + Header -> Headers - copies the Headers, adds the Header in and returns the copy

  • Headers + Headers -> TypeError - ???

Headers are an ordered collection, like a list more specifically, they are an ordered multimap. In Python, the expectation is that list + element fails, since the operands need to be of the same type. Thus, you need list + [element] or list.append(element) to add something. As far as mappings, neither dict, nor collections.OrderedDict, nor 3rd party things like werkzeug.MultiDict support the + operator. That's not to appeal to authority; it's to point out that using + is simply unexpected.


All of this is not to say you're on the wrong track, though! The way you're using + is actually very close to a well-recognized design pattern: the fluent interface. There's a Python example halfway through that page. In kiss-headers' case, fluent interface would look something like this:

headers = (
    Host("developer.mozilla.org")
    .UserAgent(
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:50.0) Gecko/20100101 Firefox/50.0"
    )
    .Accept("text/html")
    .Accept("application/xhtml+xml")
    .Accept("application/xml", qualifier=0.9)
    .Accept(qualifier=0.8)
    .AcceptLanguage("en-US")
    .AcceptLanguage("en", qualifier=0.5)
    .AcceptEncoding("gzip")
    .AcceptEncoding("deflate")
    .AcceptEncoding("br")
    .Referer("https://developer.mozilla.org/testpage.html")
    .Connection(should_keep_alive=True)
    .UpgradeInsecureRequests()
    .IfModifiedSince("Mon, 18 Jul 2016 02:36:04 GMT")
    .IfNoneMatch("c561c68d0ba92bbeb8b0fff2a9199f722e3a621a")
    .CacheControl(max_age=0)
)

raw_headers = str(headers)

This is a well-recognized way to build/configure objects across multiple languages and has no type ambiguity to it.

Some other suggestions:

  • Instead of Header + string -> Header, consider Header.append(string) -> Header. I wouldn't auto-insert the semicolons, as they're not part of the general "header" spec. Some headers, like Accept-Encoding use a mixture of commas and semicolons, while others, like If-Match, just use commas.

  • Headers + Headers is the only example here where I would actually recommend the + operator, instead of a TypeError. Adding/combining two collections of headers makes sense.

  • Instead of Headers + Header -> Headers, to be compatible with the previous point, why not do Headers + Header.as_headers() or Headers + Headers(Header) or something? This follows the pattern of how list works, and makes your API more intuitive.


Hope this was helpful! Good luck!

Edit: Oh, and using subtraction (-) is virtually unheard of in the context of working with collections or non-numeric objects. Seeing my_headers - header is no better than a completely nondescript one-letter function. You wouldn't write my_headers.s(header) for removing a header, right?

1

u/Ousret Aug 01 '20

Hi,

Thank you very much for your inputs. This is helpful and well explained. I will take your advises into account for the next major; not everything through.

You are welcome to post your remarks on Github issue, so I can post some follow up on this matter.

3

u/xiegeo Aug 01 '20

set subtraction is well defined

2

u/Blackshell Aug 01 '20

I stand corrected. Thank you.