Skip to content

Commit

Permalink
Fixing streaming responses
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbyt3r committed Dec 17, 2024
1 parent 99d006c commit b2b5e38
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
4 changes: 3 additions & 1 deletion backend/tuber/api/emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ def stream_emails():
"Content-Disposition": "attachment; filename=emails.csv",
}
return Response(stream_with_context(stream_emails()), headers=headers)
email_csv.generator = True

@app.route('/api/event/<int:event>/email/<int:email>/trigger', methods=['POST'])
def api_email_trigger(event, email):
Expand Down Expand Up @@ -158,4 +159,5 @@ def stream_emails():
"Content-Type": "application/json",
"Content-Disposition": "attachment; filename=emails.json",
}
return Response(stream_with_context(stream_emails()), headers=headers)
return Response(stream_with_context(stream_emails()), headers=headers)
api_email_trigger.generator = True
25 changes: 25 additions & 0 deletions backend/tuber/backgroundjobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import json
import uuid
import time
from werkzeug.routing import RequestRedirect
from tuber import config
from tuber.database import db, r
from tuber.models import *
Expand Down Expand Up @@ -59,7 +60,31 @@ def __del__(self):
self.pool.close()
self.pool.terminate()

def get_view_function(self, path, method):
adapter = self.application.url_map.bind('localhost')

try:
match = adapter.match(path, method=method)
except RequestRedirect as e:
# recursively match redirects
return self.get_view_function(e.new_url, method)
except:
# no match
return None

try:
# return the view function and arguments
return app.view_functions[match[0]], match[1]
except KeyError:
# no view is associated with the endpoint
return None

def __call__(self, environ, start_response):
# If the function says it's a generator then we just return it immediately
func, args = self.get_view_function(environ['PATH_INFO'], environ['REQUEST_METHOD'])
if func and hasattr(func, "generator") and getattr(func, "generator"):
return self.application(environ, start_response)

if environ['PATH_INFO'].startswith("/api/job/"):
job_id = environ['PATH_INFO'].split("/api/job/")[1]
if r:
Expand Down

0 comments on commit b2b5e38

Please sign in to comment.