Skip to content

Commit

Permalink
Reformat with blue
Browse files Browse the repository at this point in the history
  • Loading branch information
maxking committed Mar 28, 2022
1 parent 98e270f commit 598d3ae
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions examples/03-forum-webapp/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from kubernetes_asyncio.client.api_client import ApiClient as AsyncApiClient
from pathlib import Path


@dataclass
class Post(OpenAPISchemaBase):
__group__ = 'forum.example.com'
Expand Down Expand Up @@ -46,6 +47,7 @@ async def on_get(self, req, resp):
"""Handles GET requests"""
resp.sse = emitter(Post)


class AllPostsResource:
"""List of all posts"""

Expand All @@ -62,29 +64,34 @@ async def on_get(self, req, resp):
<script type="text/javascript">{0}</script>
</body>
</html>
""".format(Path('sse.js').read_text())
""".format(
Path('sse.js').read_text()
)
resp.content_type = 'text/html'
resp.code = 200


async def emitter(resource):
"""emitter emits changes to the resource object as Falcon's SSE events."""
async for happened, obj in watch_changes(resource):
yield SSEvent(json={'happened': happened, 'object': obj.json})


async def watch_changes(resource):
"""Watch changes in resource and yield the values."""
await kubernetes_asyncio.config.load_kube_config()
async_client = AsyncApiClient()
async for happened, obj in resource.async_watch(async_client):
yield happened, obj


# Create an ASGI Falcon App that can be run using something like uvcorn.
app = falcon.asgi.App()
app.add_route('/post-sse', PostStreamResource())
app.add_route('/posts', AllPostsResource())


if __name__ == "__main__":
if __name__ == '__main__':
# Run python app.py to install the CRD.
print(f'Installing CRD {Post!r} to cluster.')
install_crd([Post])

0 comments on commit 598d3ae

Please sign in to comment.