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

Ensure ASGI State is added to all subsequent ASGI requests #240

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions azure/functions/_http_asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from typing import Dict, List, Tuple, Optional, Any, Union
import logging
from copy import copy
import asyncio
from asyncio import Event, Queue
from urllib.parse import ParseResult, urlparse
Expand Down Expand Up @@ -36,7 +37,7 @@ def _get_server_address(self):
return (self.server_name, int(self.server_port))
return None

def to_asgi_http_scope(self):
def to_asgi_http_scope(self, state: Optional[Dict] = None):
if self.path_info is not None:
_raw_path = self.path_info.encode("utf-8")
else:
Expand All @@ -60,6 +61,7 @@ def to_asgi_http_scope(self):
"headers": self._get_encoded_http_headers(),
"server": self._get_server_address(),
"client": None,
"state": state,
"azure_functions.function_directory": self.af_function_directory,
"azure_functions.function_name": self.af_function_name,
"azure_functions.invocation_id": self.af_invocation_id,
Expand Down Expand Up @@ -210,7 +212,8 @@ async def main(req, context):

async def _handle_async(self, req, context):
asgi_request = AsgiRequest(req, context)
scope = asgi_request.to_asgi_http_scope()
# Shallow copy the state as-per the ASGI spec
scope = asgi_request.to_asgi_http_scope(state=copy(self.state))
asgi_response = await AsgiResponse.from_app(self._app,
scope,
req.get_body())
Expand Down