django-eventstream icon indicating copy to clipboard operation
django-eventstream copied to clipboard

Adding support for Django RestFramework

Open enzofrnt opened this issue 11 months ago • 26 comments

Related to: #140

Hi, I've just upgraded the plugin to fully support the Django Rest Framework.

Modifications include:

  • The old code has been retained, with minor upgrades.
    • Adding to django_eventstream.utils a get_cors_from_settings method
  • Rearranged the static folder organization.
  • Added a viewset for Server-Sent Events (SSE) that is capable of redirecting to the correct renderer with the appropriate response.
  • Introduced a renderer for SSE streams. This renderer is equipped to handle SSE streams with the correct response headers and the right content type.
  • Implemented a renderer for the SSE browsable API:
    • Added browsable-api-eventstream.html, a template file that is an edited version of the base.html file from the Django Rest Framework.
    • Added browsable-api-eventstream.js, a JavaScript script that provides all the necessary functionality for the browsable-api-eventstream.html template to correctly display the SSE stream, using reconnecting-eventsource.js.
    • Added browsable-api-eventstream.css to supply the CSS for the browsable-api-eventstream.html template.
  • Updated DOCs
  • Adding possibility to install with or without Django Rest Framework. Check __init__.py files.
  • Adding a fully functional example chat-restframework

The Browsable API is fully usable, as shown here:

Preview Image

image

The system will automatically return an SSE Stream when called by a request header that contains Accept: text/event-stream.

enzofrnt avatar Mar 24 '24 12:03 enzofrnt