Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add response headers to context #36

Merged
merged 1 commit into from Nov 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions twirp/asgi.py
Expand Up @@ -72,6 +72,7 @@ async def __call__(self, scope, receive, send):
self._hook.response_prepared(ctx=ctx)

body_bytes, headers = encoder(response_data)
headers = dict(ctx.get_response_headers(), **headers)
# Todo: middleware
await self._respond(
send=send,
Expand Down
16 changes: 15 additions & 1 deletion twirp/context.py
Expand Up @@ -19,6 +19,7 @@ def __init__(self, *args, logger = None, headers = None):
if headers is None:
headers = {}
self._headers = headers
self._response_headers = {}

def set(self, key, value):
"""Set a Context value
Expand Down Expand Up @@ -50,7 +51,7 @@ def set_logger(self, logger):
self._logger = logger

def get_headers(self):
"""Get headers that are currently stored."""
"""Get request headers that are currently stored."""
return self._headers

def set_header(self, key, value):
Expand All @@ -61,3 +62,16 @@ def set_header(self, key, value):
value: Value for the header.
"""
self._headers[key] = value

def get_response_headers(self):
"""Get response headers that are currently stored."""
return self._response_headers

def set_response_header(self, key, value):
"""Set a response header

Arguments:
key: Key for the header.
value: Value for the header.
"""
self._response_headers[key] = value