-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(examples): Provide Todo-Backend impelmentation
- Loading branch information
1 parent
3c46dce
commit 68756f4
Showing
19 changed files
with
789 additions
and
17 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
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,14 @@ | ||
========= | ||
Hobotnica | ||
========= | ||
|
||
OpenAPI Schema for lightweight CI/CD tool, named Hobotnica. | ||
|
||
Usage | ||
===== | ||
|
||
.. code-block:: bash | ||
make EXAMPLE=hobotnica example | ||
**IMPORTANT:** To run from ``rororo`` root directory. |
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,24 @@ | ||
============ | ||
Todo-Backend | ||
============ | ||
|
||
`Todo-Backend <http://todobackend.com>`_ implementation built on top of | ||
``rororo``, which highlights class based views usage. | ||
|
||
Requirements | ||
============ | ||
|
||
- `redis <https://redis.io>`_ 5.0 or later | ||
|
||
Usage | ||
===== | ||
|
||
.. code-block:: bash | ||
make EXAMPLE=todobackend example | ||
**IMPORTANT:** To run from ``rororo`` root directory. | ||
|
||
After, feel free to run Todo-Backend tests by opening | ||
http://www.todobackend.com/specs/index.html?http://localhost:8080/todos in | ||
your browser. |
Empty file.
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,54 @@ | ||
from pathlib import Path | ||
from typing import AsyncIterator, List | ||
|
||
from aiohttp import web | ||
from aioredis import create_redis | ||
|
||
from rororo import setup_openapi, setup_settings | ||
from rororo.settings import APP_SETTINGS_KEY | ||
from . import views | ||
from .constants import APP_REDIS_KEY, APP_STORAGE_KEY | ||
from .settings import Settings | ||
from .storage import Storage | ||
|
||
|
||
def create_app( | ||
argv: List[str] = None, *, settings: Settings = None | ||
) -> web.Application: | ||
if settings is None: | ||
settings = Settings.from_environ() # type: ignore | ||
|
||
app = setup_openapi( | ||
setup_settings( | ||
web.Application(), | ||
settings, | ||
loggers=( | ||
"aiohttp", | ||
"aiohttp_middlewares", | ||
"rororo", | ||
"todobackend", | ||
), | ||
remove_root_handlers=True, | ||
), | ||
Path(__file__).parent / "openapi.yaml", | ||
views.operations, | ||
cors_middleware_kwargs={"origins": ("http://www.todobackend.com",)}, | ||
) | ||
app.cleanup_ctx.append(storage_context) | ||
return app | ||
|
||
|
||
async def storage_context(app: web.Application) -> AsyncIterator[None]: | ||
settings: Settings = app[APP_SETTINGS_KEY] | ||
|
||
redis = app[APP_REDIS_KEY] = await create_redis( | ||
settings.redis_url, encoding="utf-8" | ||
) | ||
app[APP_STORAGE_KEY] = Storage( | ||
redis=redis, data_key=settings.redis_data_key | ||
) | ||
|
||
yield | ||
|
||
redis.close() | ||
await redis.wait_closed() |
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,2 @@ | ||
APP_REDIS_KEY = "redis" | ||
APP_STORAGE_KEY = "storage" |
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,58 @@ | ||
import uuid | ||
from random import choice | ||
from typing import TypedDict | ||
|
||
import attr | ||
from aiohttp import web | ||
from yarl import URL | ||
|
||
from rororo.annotations import DictStrStr | ||
from rororo.utils import to_bool | ||
|
||
|
||
class TodoAPIDict(TypedDict): | ||
uid: str | ||
title: str | ||
order: int | ||
url: str | ||
completed: bool | ||
|
||
|
||
@attr.dataclass | ||
class Todo: | ||
title: str | ||
|
||
uid: uuid.UUID = attr.Factory(uuid.uuid4) | ||
order: int = 0 | ||
completed: bool = False | ||
|
||
@classmethod | ||
def from_storage(cls, data: DictStrStr, *, uid: uuid.UUID) -> "Todo": | ||
return cls( | ||
uid=uid, | ||
title=data["title"], | ||
order=int(data["order"]), | ||
completed=to_bool(data["completed"]), | ||
) | ||
|
||
def get_absolute_url(self, *, request: web.Request) -> URL: | ||
route_name = choice(["retrieve_todo", "update_todo", "delete_todo"]) | ||
return request.url.with_path( # type: ignore | ||
str(request.app.router[route_name].url_for(todo_uid=str(self.uid))) | ||
) | ||
|
||
def to_api_dict(self, *, request: web.Request) -> TodoAPIDict: | ||
return { | ||
"uid": str(self.uid), | ||
"title": self.title, | ||
"order": self.order, | ||
"url": str(self.get_absolute_url(request=request)), | ||
"completed": self.completed, | ||
} | ||
|
||
def to_storage(self) -> DictStrStr: | ||
return { | ||
"title": self.title, | ||
"order": str(self.order), | ||
"completed": str(int(self.completed)), | ||
} |
Oops, something went wrong.