Content negotiation for every web frameworks.
Project description
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)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file nego-0.1.0.zip.
File metadata
- Download URL: nego-0.1.0.zip
- Upload date:
- Size: 7.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
89ca61d571afca6640478478c95ba4cccfbbbcf7c6a501d8f3e3e0b04823c388
|
|
| MD5 |
572960045967b3538339c29606c993eb
|
|
| BLAKE2b-256 |
cfc65458a18f7ab290dd9bcaa80ea2385f4b38521d25b232ef7efae6af6f792e
|