-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
94 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
""" | ||
This module contains a base HTTP API Provider implementation. This provider | ||
is only used to merge different HTTP-based API providers under the same domain | ||
""" | ||
import asyncio | ||
|
||
from aiohttp import web | ||
|
||
|
||
class HttpApiProvider(object): | ||
""" | ||
This class contains a base HTTP provider to be used | ||
""" | ||
|
||
def __init__(self, loop: asyncio.AbstractEventLoop = None): | ||
if loop is None: | ||
self._loop = asyncio.get_event_loop() | ||
else: | ||
self._loop = loop | ||
|
||
self._handler = None | ||
self._server = None | ||
|
||
self._app = web.Application() | ||
|
||
def add_child_provider(self, provider, provider_root: str) -> None: | ||
""" | ||
Assigns the specified address to the specified API provider | ||
:param provider: an child API provider to be registered | ||
:param provider_root: an address where this provider will be installed | ||
:return: None | ||
""" | ||
self._app.add_subapp( | ||
provider_root, provider.app | ||
) | ||
|
||
async def create_server(self, host: str, port: int) -> None: | ||
""" | ||
Factory function that creates fully-functional aiohttp server | ||
:param host: a server hostname or address | ||
:param port: a server port | ||
:return: None | ||
""" | ||
self._handler = self._app.make_handler(loop=self._loop) | ||
self._server = await self._loop.create_server( | ||
self._handler, host, port | ||
) | ||
|
||
async def shutdown_server(self) -> None: | ||
""" | ||
Stop (shutdown) REST server gracefully. | ||
More info is available here: https://goo.gl/eNviyZ | ||
:return: None | ||
""" | ||
self._server.close() | ||
await self._server.wait_closed() | ||
# fires on_shutdown signal (so does nothing now) | ||
await self._app.shutdown() | ||
await self._handler.shutdown(60.0) | ||
# fires on_cleanup signal (so does nothing now) | ||
await self._app.cleanup() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters