Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): build protocol layer to make pynest framework agnostic #98

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

itay-dar-lmnd
Copy link
Contributor

@itay-dar-lmnd itay-dar-lmnd commented Jan 22, 2025

Generated description

Below is a concise technical summary of the changes proposed in this PR:

Implements a comprehensive protocol layer to make the PyNest framework agnostic of the underlying web framework. This major architectural change introduces new protocols for web framework abstraction, implements a FastAPI adapter, refactors core components to use these protocols, and updates the PyNest factory to support multiple adapters. The changes also include significant updates to controller decorators, error handling, and CLI support, paving the way for a more flexible and extensible framework.

TopicDetails
FastAPI Adapter Implements a FastAPI adapter that conforms to the new WebFrameworkAdapterProtocol, allowing PyNest to use FastAPI as its underlying web framework.
Modified files (4)
  • nest/core/adapters/fastapi/__init__.py
  • nest/core/adapters/fastapi/fastapi_adapter.py
  • nest/core/adapters/fastapi/utils.py
  • nest/core/adapters/__init__.py
Latest Contributors(0)
UserCommitDate
Example App Updates Updates the BlankApp example to work with the new protocol-based architecture, demonstrating practical usage of the refactored framework.
Modified files (5)
  • examples/BlankApp/src/app_module.py
  • examples/BlankApp/main.py
  • examples/BlankApp/src/user/user_model.py
  • examples/BlankApp/src/user/user_service.py
  • examples/BlankApp/src/user/user_controller.py
Latest Contributors(2)
UserCommitDate
ItayTheDarFix-docs-52April 15, 2024
danielmoumeny212Introducing-Module-Dec...April 11, 2024
Protocol Layer Introduces a comprehensive set of protocols to abstract web framework functionality, including request/response handling, routing, middleware, and security.
Modified files (5)
  • nest/core/pynest_factory.py
  • nest/core/protocols.py
  • nest/core/pynest_container.py
  • nest/core/pynest_application.py
  • nest/core/__init__.py
Latest Contributors(2)
UserCommitDate
itay.dar@lemonade.comfeat-build-cli-apps-wi...August 06, 2024
amirm.lavasani@gmail.comAdd-HTTP-Status-Code-F...June 24, 2024
Other Other files
Modified files (2)
  • nest/core/decorators/class_based_view.py
  • nest/common/route_resolver.py
Latest Contributors(2)
UserCommitDate
itay.dar@lemonade.comMigrate-pynest-to-poet...December 20, 2024
amirm.lavasani@gmail.comAdd-HTTP-Status-Code-F...June 24, 2024
Controller Updates Updates controller decorators and related functionality to work with the new protocol-based architecture.
Modified files (3)
  • nest/core/decorators/cli/cli_decorators.py
  • nest/core/decorators/controller.py
  • examples/BlankApp/src/user/user_controller.py
Latest Contributors(2)
UserCommitDate
itay.dar@lemonade.comfeat-build-cli-apps-wi...August 06, 2024
amirm.lavasani@gmail.comAdd-HTTP-Status-Code-F...June 24, 2024
CLI Support Adds support for CLI adapters, allowing PyNest to be used for both web and command-line applications.
Modified files (3)
  • nest/core/cli_factory.py
  • nest/core/adapters/click/__init__.py
  • nest/core/adapters/click/click_adapter.py
Latest Contributors(1)
UserCommitDate
itay.dar@lemonade.comfeat-build-cli-apps-wi...August 06, 2024
Core Refactoring Refactors core PyNest components to use the new protocol layer, making the framework more modular and extensible.
Modified files (4)
  • nest/core/pynest_factory.py
  • nest/core/pynest_container.py
  • nest/common/module.py
  • nest/core/pynest_application.py
Latest Contributors(2)
UserCommitDate
itay.dar@lemonade.comfeat-build-cli-apps-wi...August 06, 2024
amirm.lavasani@gmail.comAdd-HTTP-Status-Code-F...June 24, 2024
This pull request is reviewed by Baz. Join @itay-dar-lmnd and the rest of your team on (Baz).

"""

@property
def method(self) -> str:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should change type to Literal['GET' | 'POST' | 'PUT' | 'DELETE']
Or string eum, Thus allowing string manipulation and string input

...

@property
def url(self) -> str:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could be a UrlConstraints type (pydatic)

...

@property
def headers(self) -> Dict[str, str]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some headers can be json by themself.
Need to think how to represent that .

Abstract representation of an HTTP response.
"""

def set_status_code(self, status_code: int) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

status code should be of Enum (type checker will warn from incorrect use)

def add_route(
self,
path: str,
endpoint: Callable[..., Any],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be using ParamSpec instead of ....
for more reading url

route_definitions = collect_route_definitions(cls, route_prefix)

# 5. Store routes in class attribute for later usage
setattr(cls, "__pynest_routes__", route_definitions)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Magic string, should create reflection class (str_enum) for all _pynest_routes magic attributes (injectable for example) and refrence it

return route_prefix


def process_dependencies(cls: Type) -> None:
"""Parse and set dependencies for the class."""
"""Parse and set dependencies for the class (via your DI system)."""
dependencies = parse_dependencies(cls)
setattr(cls, "__dependencies__", dependencies)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reflection refrence

not hasattr(method_function, "__route_path__")
or not method_function.__route_path__
):
if not hasattr(method_function, "__route_path__") or not method_function.__route_path__:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reflection refrence

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this comment has not been addressed yet. The commit f619cbd outdated the comment, but the changes in the diff are not related to the "reflection refrence" mentioned in the thread. The diff shows changes to various files in the project structure and implementation details, but does not address any reflection-related concerns in the specified file and line.


def add_middleware(
self,
middleware_cls: Any,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this be Type[MiddlewareProtocol] ?

T = TypeVar("T")


class RequestAttribute:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should talk , can use new to register new Param request and reuse it.
init_subclass -> for register
new -> for casting

that can be added to the application.
"""

def __call__(self, request: RequestProtocol, call_next: Callable) -> Any:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should use ParamSpec

"""
Create and configure the FastAPI application.
"""
print("Creating FastAPI app")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove print

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants