Content negotiation utilities for every web framework.
HTTP Media Type
Nego Provides abstracted HTTP media type.
Main Type & Sub Type
MediaType class parses raw media type string and split it into main/sub type.
>>> from nego import MediaType
>>> MediaType('text/html')
nego.MediaType('text/html')
>>> MediaType('text/html').main_type
'text'
>>> MediaType('text/html').sub_type
'html'
>>> MediaType('application', 'json')
nego.MediaType('application/json')
Parameters
MediaType class also parses parameters.
>>> MediaType('application/json; charset=utf-8')
nego.MediaType('application/json; charset=utf-8')
>>> MediaType('application/json; charset=utf-8').params
{'charset': 'utf-8'}
>>> MediaType('application', 'json', dict(charset='utf-8'))
nego.MediaType('application/json; charset=utf-8')
>>> MediaType('text/html; q=0.8').quality
0.8
Wildcard & Matching
You can use wildcard for media type.
>>> MediaType('text/html') in MediaType('text/*')
True
>>> MediaType('text/*') in MediaType('text/html')
False
>>> MediaType('text/html') in MediaType('text/html')
True
>>> MediaType('text/html') in MediaType('application/*')
False
>>> MediaType('text/*') in MediaType('*/*')
True
Content Negotiation
Nego provides content negotiation by function nego.media_type.choose_acceptable_media_type.
>>> from nego.media_type import choose_acceptable_media_type
>>> jpeg_type = MediaType('image/jpeg')
>>> png_type = MediaType('image/png')
>>> html_type = MediaType('text/html; q=0.9')
>>> json_type = MediaType('application/json; q=0.8')
>>> choose_acceptable_media_type(
... [png_type, jpeg_type],
... [MediaType('text/html'), MediaType('application/*'),
... MediaType('image/*')]
... )
nego.MediaType('image/png')
>>> choose_acceptable_media_type(
... [json_type, html_type],
... [MediaType('text/html'), MediaType('application/*')]
... )
nego.MediaType('text/html; q=0.9')
Renderer
Renderer renders a data into specific type of data. For example, you can make a renderer that renders a data into json like
import json
from nego import Renderer
class JSONRenderer(Renderer):
__media_types__ = ['application/json']
def render(self, data):
return json.dumps(data)
And also plain text renderer like
class TextRenderer(Renderer):
__media_types__ = ['text/plain']
def render(self, data):
return str(data)
Content Negotiation
Nego also provides content negotiation using renderer by function nego.renderer.best_renderer.
>>> json_renderer = JSONRenderer()
>>> text_renderer = TextRenderer())
>>> renderers = [json_renderer, text_renderer]
>>> accept = 'application/json;q=0.9, text/plain;q=0.7'
>>> acceptable_types = acceptable_media_types(accept)
>>> renderer, media_type = best_renderer(acceptable_types, renderers)
>>> renderer
json_renderer
>>> media_type
nego.MediaType('application/json')
>>> renderer.render(dict(foo='bar'))
'{"foo": "bar"}'
Using with Web Frameworks
Tornado
You can use Nego with tornado by making mixin.
class NegoMixin(object):
renderers = [JSONRenderer(), TextRenderer()]
def nego(self, obj):
accept = self.request.headers.get('Accept', '*/*')
acceptable_types = acceptable_media_types(accept)
renderer, media_type = best_renderer(acceptable_types, renderers)
body = renderer.render(obj)
self.set_header('Content-Type', str(media_type))
self.write(body)
Metadata
Release files for nego 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| nego-0.1.0.zip | 7.8 kB | Without provenance | Details for nego-0.1.0.zip |
Release files / nego-0.1.0.zip
| Download URL | nego-0.1.0.zip |
|---|---|
| Size | 7.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
89ca61d571afca6640478478c95ba4cccfbbbcf7c6a501d8f3e3e0b04823c388
|
|
BLAKE2b-256 checksum How to use checksums |
cfc65458a18f7ab290dd9bcaa80ea2385f4b38521d25b232ef7efae6af6f792e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for nego 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.