zrb 1.0.0a2__py3-none-any.whl → 1.0.0a4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (174) hide show
  1. zrb/__init__.py +49 -40
  2. zrb/__main__.py +5 -3
  3. zrb/attr/type.py +2 -1
  4. zrb/builtin/__init__.py +42 -2
  5. zrb/builtin/base64.py +34 -0
  6. zrb/builtin/git.py +156 -0
  7. zrb/builtin/git_subtree.py +88 -0
  8. zrb/builtin/group.py +34 -0
  9. zrb/builtin/llm/llm_chat.py +47 -0
  10. zrb/builtin/llm/tool/cli.py +9 -0
  11. zrb/builtin/llm/tool/rag.py +189 -0
  12. zrb/builtin/llm/tool/web.py +74 -0
  13. zrb/builtin/md5.py +36 -0
  14. zrb/builtin/project/add/fastapp.py +72 -0
  15. zrb/builtin/project/add/fastapp_template/.gitignore +4 -0
  16. zrb/builtin/project/add/fastapp_template/README.md +7 -0
  17. zrb/builtin/project/add/fastapp_template/_zrb/config.py +17 -0
  18. zrb/builtin/project/add/fastapp_template/_zrb/group.py +16 -0
  19. zrb/builtin/project/add/fastapp_template/_zrb/helper.py +97 -0
  20. zrb/builtin/project/add/fastapp_template/_zrb/main.py +132 -0
  21. zrb/builtin/project/add/fastapp_template/_zrb/venv_task.py +22 -0
  22. zrb/builtin/project/add/fastapp_template/common/app.py +18 -0
  23. zrb/builtin/project/add/fastapp_template/common/db_engine.py +5 -0
  24. zrb/builtin/project/add/fastapp_template/common/db_repository.py +134 -0
  25. zrb/builtin/project/add/fastapp_template/common/error.py +8 -0
  26. zrb/builtin/project/add/fastapp_template/common/schema.py +5 -0
  27. zrb/builtin/project/add/fastapp_template/common/usecase.py +232 -0
  28. zrb/builtin/project/add/fastapp_template/config.py +29 -0
  29. zrb/builtin/project/add/fastapp_template/main.py +7 -0
  30. zrb/builtin/project/add/fastapp_template/migrate.py +3 -0
  31. zrb/builtin/project/add/fastapp_template/module/__init__.py +0 -0
  32. zrb/builtin/project/add/fastapp_template/module/auth/alembic.ini +117 -0
  33. zrb/builtin/project/add/fastapp_template/module/auth/client/api_client.py +7 -0
  34. zrb/builtin/project/add/fastapp_template/module/auth/client/base_client.py +27 -0
  35. zrb/builtin/project/add/fastapp_template/module/auth/client/direct_client.py +6 -0
  36. zrb/builtin/project/add/fastapp_template/module/auth/client/factory.py +9 -0
  37. zrb/builtin/project/add/fastapp_template/module/auth/migration/README +1 -0
  38. zrb/builtin/project/add/fastapp_template/module/auth/migration/env.py +108 -0
  39. zrb/builtin/project/add/fastapp_template/module/auth/migration/script.py.mako +26 -0
  40. zrb/builtin/project/add/fastapp_template/module/auth/migration/versions/3093c7336477_add_user_table.py +37 -0
  41. zrb/builtin/project/add/fastapp_template/module/auth/migration_metadata.py +6 -0
  42. zrb/builtin/project/add/fastapp_template/module/auth/route.py +22 -0
  43. zrb/builtin/project/add/fastapp_template/module/auth/service/__init__.py +0 -0
  44. zrb/builtin/project/add/fastapp_template/module/auth/service/user/__init__.py +0 -0
  45. zrb/builtin/project/add/fastapp_template/module/auth/service/user/repository/__init__.py +0 -0
  46. zrb/builtin/project/add/fastapp_template/module/auth/service/user/repository/db_repository.py +39 -0
  47. zrb/builtin/project/add/fastapp_template/module/auth/service/user/repository/factory.py +13 -0
  48. zrb/builtin/project/add/fastapp_template/module/auth/service/user/repository/repository.py +34 -0
  49. zrb/builtin/project/add/fastapp_template/module/auth/service/user/usecase.py +45 -0
  50. zrb/builtin/project/add/fastapp_template/module/gateway/alembic.ini +117 -0
  51. zrb/builtin/project/add/fastapp_template/module/gateway/migration/README +1 -0
  52. zrb/builtin/project/add/fastapp_template/module/gateway/migration/env.py +108 -0
  53. zrb/builtin/project/add/fastapp_template/module/gateway/migration/script.py.mako +26 -0
  54. zrb/builtin/project/add/fastapp_template/module/gateway/migration/versions/.gitkeep +0 -0
  55. zrb/builtin/project/add/fastapp_template/module/gateway/migration_metadata.py +3 -0
  56. zrb/builtin/project/add/fastapp_template/module/gateway/route.py +27 -0
  57. zrb/builtin/project/add/fastapp_template/requirements.txt +6 -0
  58. zrb/builtin/project/add/fastapp_template/schema/__init__.py +0 -0
  59. zrb/builtin/project/add/fastapp_template/schema/role.py +31 -0
  60. zrb/builtin/project/add/fastapp_template/schema/user.py +31 -0
  61. zrb/builtin/project/add/fastapp_template/template.env +2 -0
  62. zrb/builtin/project/create/__init__.py +0 -0
  63. zrb/builtin/project/create/create.py +41 -0
  64. zrb/builtin/project/create/project-template/README.md +3 -0
  65. zrb/builtin/project/create/project-template/zrb_init.py +7 -0
  66. zrb/builtin/python.py +11 -0
  67. zrb/builtin/shell/__init__.py +0 -5
  68. zrb/builtin/shell/autocomplete/__init__.py +0 -9
  69. zrb/builtin/shell/autocomplete/bash.py +5 -6
  70. zrb/builtin/shell/autocomplete/subcmd.py +7 -8
  71. zrb/builtin/shell/autocomplete/zsh.py +5 -6
  72. zrb/builtin/todo.py +219 -0
  73. zrb/callback/any_callback.py +1 -1
  74. zrb/callback/callback.py +5 -5
  75. zrb/cmd/cmd_val.py +2 -2
  76. zrb/config.py +16 -3
  77. zrb/content_transformer/any_content_transformer.py +1 -1
  78. zrb/content_transformer/content_transformer.py +2 -2
  79. zrb/context/any_context.py +1 -1
  80. zrb/context/any_shared_context.py +3 -3
  81. zrb/context/context.py +10 -8
  82. zrb/context/shared_context.py +9 -8
  83. zrb/env/__init__.py +0 -3
  84. zrb/env/any_env.py +1 -1
  85. zrb/env/env.py +3 -4
  86. zrb/env/env_file.py +4 -4
  87. zrb/env/env_map.py +2 -2
  88. zrb/group/__init__.py +0 -3
  89. zrb/group/any_group.py +3 -3
  90. zrb/group/group.py +7 -6
  91. zrb/input/any_input.py +1 -1
  92. zrb/input/base_input.py +4 -4
  93. zrb/input/bool_input.py +5 -5
  94. zrb/input/float_input.py +3 -3
  95. zrb/input/int_input.py +3 -3
  96. zrb/input/option_input.py +51 -0
  97. zrb/input/password_input.py +2 -2
  98. zrb/input/str_input.py +1 -1
  99. zrb/input/text_input.py +12 -10
  100. zrb/runner/cli.py +80 -45
  101. zrb/runner/web_app.py +150 -0
  102. zrb/runner/web_controller/__init__.py +0 -0
  103. zrb/runner/web_controller/group_info_ui/__init__.py +0 -0
  104. zrb/runner/{web_app → web_controller}/group_info_ui/controller.py +7 -8
  105. zrb/runner/{web_app → web_controller}/group_info_ui/view.html +2 -2
  106. zrb/runner/web_controller/home_page/__init__.py +0 -0
  107. zrb/runner/{web_app → web_controller}/home_page/controller.py +7 -6
  108. zrb/runner/{web_app → web_controller}/home_page/view.html +2 -2
  109. zrb/runner/web_controller/task_ui/__init__.py +0 -0
  110. zrb/runner/{web_app → web_controller}/task_ui/controller.py +8 -12
  111. zrb/runner/{web_app → web_controller}/task_ui/view.html +2 -2
  112. zrb/runner/web_util.py +5 -35
  113. zrb/session/any_session.py +13 -7
  114. zrb/session/session.py +78 -40
  115. zrb/session_state_log/session_state_log.py +7 -5
  116. zrb/session_state_logger/any_session_state_logger.py +1 -1
  117. zrb/session_state_logger/default_session_state_logger.py +2 -2
  118. zrb/session_state_logger/file_session_state_logger.py +19 -27
  119. zrb/task/any_task.py +4 -4
  120. zrb/task/base_task.py +33 -23
  121. zrb/task/base_trigger.py +11 -12
  122. zrb/task/cmd_task.py +72 -65
  123. zrb/task/http_check.py +13 -13
  124. zrb/task/llm_task.py +215 -0
  125. zrb/task/make_task.py +9 -9
  126. zrb/task/rsync_task.py +25 -25
  127. zrb/task/scaffolder.py +18 -15
  128. zrb/task/scheduler.py +6 -7
  129. zrb/task/task.py +1 -1
  130. zrb/task/tcp_check.py +11 -13
  131. zrb/util/attr.py +19 -3
  132. zrb/util/cli/style.py +71 -2
  133. zrb/util/cli/subcommand.py +2 -2
  134. zrb/util/codemod/__init__.py +0 -0
  135. zrb/util/codemod/add_code_to_class.py +35 -0
  136. zrb/util/codemod/add_code_to_function.py +36 -0
  137. zrb/util/codemod/add_code_to_method.py +55 -0
  138. zrb/util/codemod/add_key_to_dict.py +51 -0
  139. zrb/util/codemod/add_param_to_function_call.py +39 -0
  140. zrb/util/codemod/add_property_to_class.py +55 -0
  141. zrb/util/git.py +156 -0
  142. zrb/util/git_subtree.py +94 -0
  143. zrb/util/group.py +2 -2
  144. zrb/util/llm/tool.py +63 -0
  145. zrb/util/string/conversion.py +7 -0
  146. zrb/util/todo.py +259 -0
  147. {zrb-1.0.0a2.dist-info → zrb-1.0.0a4.dist-info}/METADATA +13 -5
  148. zrb-1.0.0a4.dist-info/RECORD +197 -0
  149. zrb/builtin/shell/_group.py +0 -9
  150. zrb/builtin/shell/autocomplete/_group.py +0 -6
  151. zrb/runner/web_app/any_request_handler.py +0 -24
  152. zrb/runner/web_server.py +0 -224
  153. zrb-1.0.0a2.dist-info/RECORD +0 -120
  154. /zrb/{runner/web_app → builtin/project}/__init__.py +0 -0
  155. /zrb/{runner/web_app/group_info_ui → builtin/project/add}/__init__.py +0 -0
  156. /zrb/{runner/web_app/home_page → builtin/project/add/fastapp_template}/__init__.py +0 -0
  157. /zrb/{runner/web_app/task_ui → builtin/project/add/fastapp_template/common}/__init__.py +0 -0
  158. /zrb/runner/{web_app → web_controller}/group_info_ui/partial/group_info.html +0 -0
  159. /zrb/runner/{web_app → web_controller}/group_info_ui/partial/group_li.html +0 -0
  160. /zrb/runner/{web_app → web_controller}/group_info_ui/partial/task_info.html +0 -0
  161. /zrb/runner/{web_app → web_controller}/group_info_ui/partial/task_li.html +0 -0
  162. /zrb/runner/{web_app → web_controller}/home_page/partial/group_info.html +0 -0
  163. /zrb/runner/{web_app → web_controller}/home_page/partial/group_li.html +0 -0
  164. /zrb/runner/{web_app → web_controller}/home_page/partial/task_info.html +0 -0
  165. /zrb/runner/{web_app → web_controller}/home_page/partial/task_li.html +0 -0
  166. /zrb/runner/{web_app → web_controller}/static/favicon-32x32.png +0 -0
  167. /zrb/runner/{web_app → web_controller}/static/pico.min.css +0 -0
  168. /zrb/runner/{web_app → web_controller}/task_ui/partial/common-util.js +0 -0
  169. /zrb/runner/{web_app → web_controller}/task_ui/partial/input.html +0 -0
  170. /zrb/runner/{web_app → web_controller}/task_ui/partial/main.js +0 -0
  171. /zrb/runner/{web_app → web_controller}/task_ui/partial/show-existing-session.js +0 -0
  172. /zrb/runner/{web_app → web_controller}/task_ui/partial/visualize-history.js +0 -0
  173. {zrb-1.0.0a2.dist-info → zrb-1.0.0a4.dist-info}/WHEEL +0 -0
  174. {zrb-1.0.0a2.dist-info → zrb-1.0.0a4.dist-info}/entry_points.txt +0 -0
@@ -0,0 +1,5 @@
1
+ from fastapp_template.config import APP_DB_URL
2
+ from sqlmodel import create_engine
3
+
4
+ connect_args = {"check_same_thread": False}
5
+ engine = create_engine(APP_DB_URL, connect_args=connect_args)
@@ -0,0 +1,134 @@
1
+ from typing import Any, Callable, Generic, Type, TypeVar
2
+
3
+ from fastapp_template.common.error import NotFoundError
4
+ from sqlalchemy import Engine
5
+ from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession
6
+ from sqlmodel import Session, SQLModel, select
7
+
8
+ DBModel = TypeVar("DBModel", bound=SQLModel)
9
+ ResponseModel = TypeVar("Model", bound=SQLModel)
10
+ CreateModel = TypeVar("CreateModel", bound=SQLModel)
11
+ UpdateModel = TypeVar("UpdateModel", bound=SQLModel)
12
+
13
+
14
+ class BaseDBRepository(Generic[DBModel, ResponseModel, CreateModel, UpdateModel]):
15
+ db_model: Type[DBModel]
16
+ response_model: Type[ResponseModel]
17
+ create_model: Type[CreateModel]
18
+ update_model: Type[UpdateModel]
19
+ entity_name: str = "entity"
20
+ column_preprocessors: dict[str, Callable[[Any], Any]] = {}
21
+
22
+ def __init__(self, engine: Engine | AsyncEngine):
23
+ self.engine = engine
24
+ self.is_async = isinstance(engine, AsyncEngine)
25
+
26
+ def _to_response(self, db_instance: DBModel) -> ResponseModel:
27
+ return self.response_model(**db_instance.model_dump())
28
+
29
+ async def create(self, data: CreateModel) -> ResponseModel:
30
+ data_dict = data.model_dump(exclude_unset=True)
31
+ for key, preprocessor in self.column_preprocessors.items():
32
+ if key in data_dict:
33
+ data_dict[key] = preprocessor(data_dict[key])
34
+ db_instance = self.db_model(**data_dict)
35
+ if self.is_async:
36
+ async with AsyncSession(self.engine) as session:
37
+ session.add(db_instance)
38
+ await session.commit()
39
+ await session.refresh(db_instance)
40
+ else:
41
+ with Session(self.engine) as session:
42
+ session.add(db_instance)
43
+ session.commit()
44
+ session.refresh(db_instance)
45
+ return self._to_response(db_instance)
46
+
47
+ async def get_by_id(self, item_id: str) -> ResponseModel:
48
+ if self.is_async:
49
+ async with AsyncSession(self.engine) as session:
50
+ db_instance = await session.get(self.db_model, item_id)
51
+ else:
52
+ with Session(self.engine) as session:
53
+ db_instance = session.get(self.db_model, item_id)
54
+ if not db_instance:
55
+ raise NotFoundError(f"{self.entity_name} not found")
56
+ return self._to_response(db_instance)
57
+
58
+ async def get_all(self, page: int = 1, page_size: int = 10) -> list[ResponseModel]:
59
+ offset = (page - 1) * page_size
60
+ statement = select(self.db_model).offset(offset).limit(page_size)
61
+ if self.is_async:
62
+ async with AsyncSession(self.engine) as session:
63
+ result = await session.execute(statement)
64
+ results = result.scalars().all()
65
+ else:
66
+ with Session(self.engine) as session:
67
+ results = session.exec(statement).all()
68
+ return [self._to_response(instance) for instance in results]
69
+
70
+ async def update(self, item_id: str, data: UpdateModel) -> ResponseModel:
71
+ update_data = data.model_dump(exclude_unset=True)
72
+ for key, value in update_data.items():
73
+ if key in self.column_preprocessors:
74
+ update_data[key] = self.column_preprocessors[key](value)
75
+ if self.is_async:
76
+ async with AsyncSession(self.engine) as session:
77
+ db_instance = await session.get(self.db_model, item_id)
78
+ if not db_instance:
79
+ raise NotFoundError(f"{self.entity_name} not found")
80
+ for key, value in update_data.items():
81
+ setattr(db_instance, key, value)
82
+ session.add(db_instance)
83
+ await session.commit()
84
+ await session.refresh(db_instance)
85
+ else:
86
+ with Session(self.engine) as session:
87
+ db_instance = session.get(self.db_model, item_id)
88
+ if not db_instance:
89
+ raise NotFoundError(f"{self.entity_name} not found")
90
+ for key, value in update_data.items():
91
+ setattr(db_instance, key, value)
92
+ session.add(db_instance)
93
+ session.commit()
94
+ session.refresh(db_instance)
95
+ return self._to_response(db_instance)
96
+
97
+ async def delete(self, item_id: str) -> ResponseModel:
98
+ if self.is_async:
99
+ async with AsyncSession(self.engine) as session:
100
+ db_instance = await session.get(self.db_model, item_id)
101
+ if not db_instance:
102
+ raise NotFoundError(f"{self.entity_name} not found")
103
+ await session.delete(db_instance)
104
+ await session.commit()
105
+ else:
106
+ with Session(self.engine) as session:
107
+ db_instance = session.get(self.db_model, item_id)
108
+ if not db_instance:
109
+ raise NotFoundError(f"{self.entity_name} not found")
110
+ session.delete(db_instance)
111
+ session.commit()
112
+ return self._to_response(db_instance)
113
+
114
+ async def create_bulk(self, data_list: list[CreateModel]) -> list[ResponseModel]:
115
+ db_instances = []
116
+ for data in data_list:
117
+ data_dict = data.model_dump(exclude_unset=True)
118
+ for key, preprocessor in self.column_preprocessors.items():
119
+ if key in data_dict:
120
+ data_dict[key] = preprocessor(data_dict[key])
121
+ db_instances.append(self.db_model(**data_dict))
122
+ if self.is_async:
123
+ async with AsyncSession(self.engine) as session:
124
+ session.add_all(db_instances)
125
+ await session.commit()
126
+ for instance in db_instances:
127
+ await session.refresh(instance)
128
+ else:
129
+ with Session(self.engine) as session:
130
+ session.add_all(db_instances)
131
+ session.commit()
132
+ for instance in db_instances:
133
+ session.refresh(instance)
134
+ return [self._to_response(instance) for instance in db_instances]
@@ -0,0 +1,8 @@
1
+ from typing import Dict
2
+
3
+ from fastapi import HTTPException
4
+
5
+
6
+ class NotFoundError(HTTPException):
7
+ def __init__(self, message: str, headers: Dict[str, str] | None = None) -> None:
8
+ super().__init__(404, {"message": message}, headers)
@@ -0,0 +1,5 @@
1
+ from pydantic import BaseModel
2
+
3
+
4
+ class BasicResponse(BaseModel):
5
+ message: str
@@ -0,0 +1,232 @@
1
+ from enum import Enum
2
+ from functools import partial, wraps
3
+ from typing import Any, Callable, Sequence
4
+
5
+ import httpx
6
+ from fastapi import APIRouter, params
7
+ from fastapi.routing import APIRoute
8
+ from fastapi.types import IncEx
9
+ from fastapi.utils import generate_unique_id
10
+ from starlette.responses import JSONResponse, Response
11
+
12
+
13
+ class RouteParam:
14
+ def __init__(
15
+ self,
16
+ path: str,
17
+ response_model: Any,
18
+ status_code: int | None = None,
19
+ tags: list[str | Enum] | None = None,
20
+ dependencies: Sequence[params.Depends] | None = None,
21
+ summary: str | None = None,
22
+ description: str = "",
23
+ response_description: str = "",
24
+ responses: dict[int | str, dict[str, Any]] | None = None,
25
+ deprecated: bool | None = None,
26
+ methods: set[str] | list[str] | None = None,
27
+ operation_id: str | None = None,
28
+ response_model_include: IncEx | None = None,
29
+ response_model_exclude: IncEx | None = None,
30
+ response_model_by_alias: bool = True,
31
+ response_model_exclude_unset: bool = False,
32
+ response_model_exclude_defaults: bool = False,
33
+ response_model_exclude_none: bool = False,
34
+ include_in_schema: bool = True,
35
+ response_class: type[Response] = Response,
36
+ name: str | None = None,
37
+ openapi_extra: dict[str, Any] | None = None,
38
+ generate_unique_id_function: Callable[[APIRoute], str] | None = None,
39
+ func: Callable | None = None,
40
+ ):
41
+ self.path = path
42
+ self.response_model = response_model
43
+ self.status_code = status_code
44
+ self.tags = tags
45
+ self.dependencies = dependencies
46
+ self.summary = summary
47
+ self.description = description
48
+ self.response_description = response_description
49
+ self.responses = responses
50
+ self.deprecated = deprecated
51
+ self.methods = methods
52
+ self.operation_id = operation_id
53
+ self.response_model_include = response_model_include
54
+ self.response_model_exclude = response_model_exclude
55
+ self.response_model_by_alias = response_model_by_alias
56
+ self.response_model_exclude_unset = response_model_exclude_unset
57
+ self.response_model_exclude_defaults = response_model_exclude_defaults
58
+ self.response_model_exclude_none = response_model_exclude_none
59
+ self.include_in_schema = include_in_schema
60
+ self.response_class = response_class
61
+ self.name = name
62
+ self.openapi_extra = openapi_extra
63
+ self.generate_unique_id_function = generate_unique_id_function
64
+ self.func = func
65
+
66
+
67
+ class BaseUsecase:
68
+ _route_params: dict[str, RouteParam] = {}
69
+
70
+ @classmethod
71
+ def route(
72
+ cls,
73
+ path: str,
74
+ *,
75
+ response_model: Any = None,
76
+ status_code: int | None = None,
77
+ tags: list[str | Enum] | None = None,
78
+ dependencies: Sequence[params.Depends] | None = None,
79
+ summary: str | None = None,
80
+ description: str = None,
81
+ response_description: str = "Successful Response",
82
+ responses: dict[int | str, dict[str, Any]] | None = None,
83
+ deprecated: bool | None = None,
84
+ methods: set[str] | list[str] | None = None,
85
+ operation_id: str | None = None,
86
+ response_model_include: IncEx | None = None,
87
+ response_model_exclude: IncEx | None = None,
88
+ response_model_by_alias: bool = True,
89
+ response_model_exclude_unset: bool = False,
90
+ response_model_exclude_defaults: bool = False,
91
+ response_model_exclude_none: bool = False,
92
+ include_in_schema: bool = True,
93
+ response_class: type[Response] = JSONResponse,
94
+ name: str | None = None,
95
+ openapi_extra: dict[str, Any] | None = None,
96
+ generate_unique_id_function: Callable[[APIRoute], str] = generate_unique_id,
97
+ ):
98
+ """
99
+ Decorator to register a method with its HTTP details.
100
+ """
101
+
102
+ def decorator(func: Callable):
103
+ cls._route_params[func.__name__] = RouteParam(
104
+ path=path,
105
+ response_model=response_model,
106
+ status_code=status_code,
107
+ tags=tags,
108
+ dependencies=dependencies,
109
+ summary=summary,
110
+ description=description,
111
+ response_description=response_description,
112
+ responses=responses,
113
+ deprecated=deprecated,
114
+ methods=methods,
115
+ operation_id=operation_id,
116
+ response_model_include=response_model_include,
117
+ response_model_exclude=response_model_exclude,
118
+ response_model_by_alias=response_model_by_alias,
119
+ response_model_exclude_unset=response_model_exclude_unset,
120
+ response_model_exclude_defaults=response_model_exclude_defaults,
121
+ response_model_exclude_none=response_model_exclude_none,
122
+ include_in_schema=include_in_schema,
123
+ response_class=response_class,
124
+ name=name,
125
+ openapi_extra=openapi_extra,
126
+ generate_unique_id_function=generate_unique_id_function,
127
+ func=func,
128
+ )
129
+
130
+ @wraps(func)
131
+ async def wrapped(*args, **kwargs):
132
+ return await func(*args, **kwargs)
133
+
134
+ return wrapped
135
+
136
+ return decorator
137
+
138
+ def as_direct_client(self):
139
+ """
140
+ Dynamically create a direct client class.
141
+ """
142
+ _methods = self._route_params
143
+ DirectClient = create_client_class("DirectClient")
144
+ for name, details in _methods.items():
145
+ func = details.func
146
+ client_method = create_direct_client_method(func, self)
147
+ setattr(DirectClient, name, client_method.__get__(DirectClient))
148
+ return DirectClient
149
+
150
+ def as_api_client(self, base_url: str):
151
+ """
152
+ Dynamically create an API client class.
153
+ """
154
+ _methods = self._route_params
155
+ APIClient = create_client_class("APIClient")
156
+ # Dynamically generate methods
157
+ for name, param in _methods.items():
158
+ client_method = create_api_client_method(param, base_url)
159
+ setattr(APIClient, name, client_method.__get__(APIClient))
160
+ return APIClient
161
+
162
+ def serve_route(self, app: APIRouter):
163
+ """
164
+ Dynamically add routes to FastAPI.
165
+ """
166
+ for _, route_param in self._route_params.items():
167
+ bound_func = partial(route_param.func, self)
168
+ bound_func.__name__ = route_param.func.__name__
169
+ bound_func.__doc__ = route_param.func.__doc__
170
+ app.add_api_route(
171
+ path=route_param.path,
172
+ endpoint=bound_func,
173
+ response_model=route_param.response_model,
174
+ status_code=route_param.status_code,
175
+ tags=route_param.tags,
176
+ dependencies=route_param.dependencies,
177
+ summary=route_param.summary,
178
+ description=route_param.description,
179
+ response_description=route_param.response_description,
180
+ responses=route_param.responses,
181
+ deprecated=route_param.deprecated,
182
+ methods=route_param.methods,
183
+ operation_id=route_param.operation_id,
184
+ response_model_include=route_param.response_model_include,
185
+ response_model_exclude=route_param.response_model_exclude,
186
+ response_model_by_alias=route_param.response_model_by_alias,
187
+ response_model_exclude_unset=route_param.response_model_exclude_unset,
188
+ response_model_exclude_defaults=route_param.response_model_exclude_defaults,
189
+ response_model_exclude_none=route_param.response_model_exclude_none,
190
+ include_in_schema=route_param.include_in_schema,
191
+ response_class=route_param.response_class,
192
+ name=route_param.name,
193
+ openapi_extra=route_param.openapi_extra,
194
+ generate_unique_id_function=route_param.generate_unique_id_function,
195
+ )
196
+
197
+
198
+ def create_client_class(name):
199
+ class Client:
200
+ pass
201
+
202
+ Client.__name__ = name
203
+ return Client
204
+
205
+
206
+ def create_direct_client_method(func: Callable, usecase: BaseUsecase):
207
+ async def client_method(self, *args, **kwargs):
208
+ return await func(usecase, *args, **kwargs)
209
+
210
+ return client_method
211
+
212
+
213
+ def create_api_client_method(param: RouteParam, base_url: str):
214
+ _url = param.path
215
+ _methods = [method.lower() for method in param.methods]
216
+
217
+ async def client_method(self, *args, **kwargs):
218
+ async with httpx.AsyncClient() as client:
219
+ url = base_url + _url.format(**kwargs)
220
+ if "post" in _methods:
221
+ response = await client.post(url, json=kwargs)
222
+ elif "put" in _methods:
223
+ response = await client.put(url, json=kwargs)
224
+ elif "delete" in _methods:
225
+ response = await client.delete(url, json=kwargs)
226
+ else:
227
+ response = await client.get(url, params=kwargs)
228
+ # Add more HTTP methods as needed
229
+ response.raise_for_status()
230
+ return response.json()
231
+
232
+ return client_method
@@ -0,0 +1,29 @@
1
+ import os
2
+
3
+ APP_PATH = os.path.dirname(__file__)
4
+
5
+ APP_MODE = os.getenv("APP_NAME_MODE", "monolith")
6
+ APP_MODULES = [
7
+ module.strip()
8
+ for module in os.getenv("APP_NAME_MODULES", "").split(",")
9
+ if module.strip() != ""
10
+ ]
11
+ APP_PORT = int(os.getenv("APP_NAME_PORT", "3000"))
12
+ APP_COMMUNICATION = os.getenv(
13
+ "APP_NAME_COMMUNICATION", "direct" if APP_MODE == "monolith" else "api"
14
+ )
15
+ APP_REPOSITORY_TYPE = os.getenv("APP_REPOSITORY_TYPE", "db")
16
+ APP_DB_URL = os.getenv(
17
+ "APP_DB_URL",
18
+ (
19
+ f"sqlite:///{APP_PATH}/monolith.db"
20
+ if APP_MODE == "monolith" or len(APP_MODULES) == 0
21
+ else f"sqlite:///{APP_PATH}/{APP_MODULES[0]}_microservices.db"
22
+ ),
23
+ )
24
+ APP_AUTH_SUPER_USER = os.getenv("APP_NAME_AUTH_SUPER_USER", "admin")
25
+ APP_AUTH_SUPER_USER_PASSWORD = os.getenv(
26
+ "APP_NAME_AUTH_SUPER_USER_PASSWORD", "secure-password"
27
+ )
28
+
29
+ APP_AUTH_BASE_URL = os.getenv("APP_NAME_AUTH_BASE_URL", "http://localhost:3001")
@@ -0,0 +1,7 @@
1
+ from fastapp_template.common.app import app
2
+ from fastapp_template.module.auth import route as auth_route
3
+ from fastapp_template.module.gateway import route as gateway_route
4
+
5
+ assert app
6
+ assert gateway_route
7
+ assert auth_route
@@ -0,0 +1,3 @@
1
+ from fastapp_template.module.auth import migration as auth_migration
2
+
3
+ assert auth_migration
@@ -0,0 +1,117 @@
1
+ # A generic, single database configuration.
2
+
3
+ [alembic]
4
+ # path to migration scripts
5
+ # Use forward slashes (/) also on windows to provide an os agnostic path
6
+ script_location = migration
7
+
8
+ # template used to generate migration file names; The default value is %%(rev)s_%%(slug)s
9
+ # Uncomment the line below if you want the files to be prepended with date and time
10
+ # see https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file
11
+ # for all available tokens
12
+ # file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s
13
+
14
+ # sys.path path, will be prepended to sys.path if present.
15
+ # defaults to the current working directory.
16
+ prepend_sys_path = ../..
17
+
18
+ # timezone to use when rendering the date within the migration file
19
+ # as well as the filename.
20
+ # If specified, requires the python>=3.9 or backports.zoneinfo library.
21
+ # Any required deps can installed by adding `alembic[tz]` to the pip requirements
22
+ # string value is passed to ZoneInfo()
23
+ # leave blank for localtime
24
+ # timezone =
25
+
26
+ # max length of characters to apply to the "slug" field
27
+ # truncate_slug_length = 40
28
+
29
+ # set to 'true' to run the environment during
30
+ # the 'revision' command, regardless of autogenerate
31
+ # revision_environment = false
32
+
33
+ # set to 'true' to allow .pyc and .pyo files without
34
+ # a source .py file to be detected as revisions in the
35
+ # versions/ directory
36
+ # sourceless = false
37
+
38
+ # version location specification; This defaults
39
+ # to migration/versions. When using multiple version
40
+ # directories, initial revisions must be specified with --version-path.
41
+ # The path separator used here should be the separator specified by "version_path_separator" below.
42
+ # version_locations = %(here)s/bar:%(here)s/bat:migration/versions
43
+
44
+ # version path separator; As mentioned above, this is the character used to split
45
+ # version_locations. The default within new alembic.ini files is "os", which uses os.pathsep.
46
+ # If this key is omitted entirely, it falls back to the legacy behavior of splitting on spaces and/or commas.
47
+ # Valid values for version_path_separator are:
48
+ #
49
+ # version_path_separator = :
50
+ # version_path_separator = ;
51
+ # version_path_separator = space
52
+ # version_path_separator = newline
53
+ version_path_separator = os # Use os.pathsep. Default configuration used for new projects.
54
+
55
+ # set to 'true' to search source files recursively
56
+ # in each "version_locations" directory
57
+ # new in Alembic version 1.10
58
+ # recursive_version_locations = false
59
+
60
+ # the output encoding used when revision files
61
+ # are written from script.py.mako
62
+ # output_encoding = utf-8
63
+
64
+ sqlalchemy.url = driver://user:pass@localhost/dbname
65
+
66
+
67
+ [post_write_hooks]
68
+ # post_write_hooks defines scripts or Python functions that are run
69
+ # on newly generated revision scripts. See the documentation for further
70
+ # detail and examples
71
+
72
+ # format using "black" - use the console_scripts runner, against the "black" entrypoint
73
+ # hooks = black
74
+ # black.type = console_scripts
75
+ # black.entrypoint = black
76
+ # black.options = -l 79 REVISION_SCRIPT_FILENAME
77
+
78
+ # lint with attempts to fix using "ruff" - use the exec runner, execute a binary
79
+ # hooks = ruff
80
+ # ruff.type = exec
81
+ # ruff.executable = %(here)s/.venv/bin/ruff
82
+ # ruff.options = --fix REVISION_SCRIPT_FILENAME
83
+
84
+ # Logging configuration
85
+ [loggers]
86
+ keys = root,sqlalchemy,alembic
87
+
88
+ [handlers]
89
+ keys = console
90
+
91
+ [formatters]
92
+ keys = generic
93
+
94
+ [logger_root]
95
+ level = WARNING
96
+ handlers = console
97
+ qualname =
98
+
99
+ [logger_sqlalchemy]
100
+ level = WARNING
101
+ handlers =
102
+ qualname = sqlalchemy.engine
103
+
104
+ [logger_alembic]
105
+ level = INFO
106
+ handlers =
107
+ qualname = alembic
108
+
109
+ [handler_console]
110
+ class = StreamHandler
111
+ args = (sys.stderr,)
112
+ level = NOTSET
113
+ formatter = generic
114
+
115
+ [formatter_generic]
116
+ format = %(levelname)-5.5s [%(name)s] %(message)s
117
+ datefmt = %H:%M:%S
@@ -0,0 +1,7 @@
1
+ from fastapp_template.config import APP_AUTH_BASE_URL
2
+ from fastapp_template.module.auth.client.base_client import BaseClient
3
+ from fastapp_template.module.auth.service.user.usecase import user_usecase
4
+
5
+
6
+ class APIClient(user_usecase.as_api_client(base_url=APP_AUTH_BASE_URL), BaseClient):
7
+ pass
@@ -0,0 +1,27 @@
1
+ from abc import ABC, abstractmethod
2
+
3
+ from fastapp_template.schema.user import UserCreate, UserResponse, UserUpdate
4
+
5
+
6
+ class BaseClient(ABC):
7
+ @abstractmethod
8
+ async def get_user_by_id(self, user_id: str) -> UserResponse:
9
+ pass
10
+
11
+ @abstractmethod
12
+ async def get_all_users(self) -> list[UserResponse]:
13
+ pass
14
+
15
+ @abstractmethod
16
+ async def create_user(
17
+ self, data: UserCreate | list[UserCreate]
18
+ ) -> UserResponse | list[UserResponse]:
19
+ pass
20
+
21
+ @abstractmethod
22
+ async def update_user(self, user_id: str, data: UserUpdate) -> UserResponse:
23
+ pass
24
+
25
+ @abstractmethod
26
+ async def delete_user(self, user_id: str) -> UserResponse:
27
+ pass
@@ -0,0 +1,6 @@
1
+ from fastapp_template.module.auth.client.base_client import BaseClient
2
+ from fastapp_template.module.auth.service.user.usecase import user_usecase
3
+
4
+
5
+ class DirectClient(user_usecase.as_direct_client(), BaseClient):
6
+ pass
@@ -0,0 +1,9 @@
1
+ from fastapp_template.config import APP_COMMUNICATION
2
+ from fastapp_template.module.auth.client.api_client import APIClient
3
+ from fastapp_template.module.auth.client.base_client import BaseClient
4
+ from fastapp_template.module.auth.client.direct_client import DirectClient
5
+
6
+ if APP_COMMUNICATION == "direct":
7
+ client: BaseClient = DirectClient()
8
+ elif APP_COMMUNICATION == "api":
9
+ client: BaseClient = APIClient()
@@ -0,0 +1 @@
1
+ Generic single-database configuration.