Skip to content

Commit

Permalink
Better initial support for ASGI in Appier
Browse files Browse the repository at this point in the history
  • Loading branch information
joamag committed Nov 11, 2019
1 parent 459aea6 commit 496a21c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .pydevproject
Expand Up @@ -8,5 +8,5 @@
<path>/${PROJECT_DIR_NAME}/examples/http</path>
</pydev_pathproperty>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python 2.6</pydev_property>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python 3.5</pydev_property>
</pydev_project>
44 changes: 39 additions & 5 deletions src/appier/base.py
Expand Up @@ -452,6 +452,14 @@ def __getattr__(self, name):

return getattr(self.request, name)

@classmethod
async def asgi_entry(cls, scope, receive, send):
if hasattr(cls, "_asgi") and cls._asgi:
return await cls._asgi.app_asgi(scope, receive, send)
cls._asgi = cls()
cls._asgi.start()
return await cls._asgi.app_asgi(scope, receive, send)

@property
def request(self):
if not self.safe: return self._request
Expand Down Expand Up @@ -844,10 +852,6 @@ def loop(self, callable = lambda: time.sleep(60)):
# back to the "original"/expected state
self.stop()

def get_app(self):
self.start()
return self.app

def serve(
self,
server = "legacy",
Expand Down Expand Up @@ -1317,7 +1321,37 @@ def application_wsgi(self, environ, start_response):
finally: self.restore()

async def application_asgi(self, scope, receive, send):
print(scope)
"""
ASGI version of the application entrypoint, should define
the proper asynchronous workflow for an HTTP request handling.
:type scope: Dictionary
:param scope: The connection scope, a dictionary that contains
at least a type key specifying the protocol that is incoming.
:type receive: Coroutine
:param receive: An awaitable callable that will yield a new
event dictionary when one is available.
:type send: Coroutine
:param send: an awaitable callable taking a single event dictionary
as a positional argument that will return once the send has been
completed or the connection has been closed.
"""

self.prepare()
try:
await send({
"type": "http.response.start",
"status" : 200,
"headers" : [
[b"content-type", b"text/plain"],
]
})
await send({
"type" : "http.response.body",
"body" : b"Hello, world!",
})
finally:
self.restore()

def prepare(self):
"""
Expand Down

0 comments on commit 496a21c

Please sign in to comment.