Skip to content

Commit

Permalink
Ensure ASGI State is added to all subsequent ASGI requests
Browse files Browse the repository at this point in the history
  • Loading branch information
ashleysommer authored Aug 22, 2024
1 parent 3873bab commit 4b61071
Showing 1 changed file with 5 additions and 2 deletions.
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

0 comments on commit 4b61071

Please sign in to comment.