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/middleware #102

Merged

Conversation

MEHRSHAD-MIRSHEKARY
Copy link
Collaborator

###base middleware:

  • Synchronous and Asynchronous Support: The middleware can handle requests in both synchronous and asynchronous modes, determined dynamically at initialization.
  • String Representation: Implements a repr method for easy identification of the middleware instance, showing the callable associated with it.
  • Method Stubs: Defines sync_call and acall methods that must be implemented by subclasses, enforcing a structure for middleware development.

###request middleware:

  • Context Variable Management: Introduced a context manager to bind relevant request details, such as
    equest_id, ip_address, and user_agent, improving log traceability and enabling more structured logging.
  • SQL Query Logging: Added functionality to log SQL queries executed during the request lifecycle. The middleware tracks the number of queries executed and logs them conditionally based on the configuration, allowing developers to debug SQL interactions effectively.
  • Streaming Content Handling: Enhanced handling of streaming responses by introducing dedicated synchronous and asynchronous streaming wrappers, ensuring accurate logging during streaming operations.

This refactor improves the logging capabilities of the middleware while ensuring it can operate in both sync and async contexts, making it a more robust component for logging request-related information in Django applications.

Closes #95

…Functionality

- **Synchronous and Asynchronous Support**: The middleware can handle requests in both synchronous and asynchronous modes, determined dynamically at initialization.
- **String Representation**: Implements a __repr__ method for easy identification of the middleware instance, showing the callable associated with it.
- **Method Stubs**: Defines __sync_call__ and __acall__ methods that must be implemented by subclasses, enforcing a structure for middleware development.

This base class serves as a reusable component for developers building custom middleware in Django, ensuring compatibility with both synchronous and asynchronous request flows.
- **Asynchronous Support**: The middleware now implements the __acall__ method, enabling it to handle asynchronous requests effectively while maintaining the functionality of synchronous requests through the existing __sync_call__ method.
- **Context Variable Management**: Introduced a context manager to bind relevant request details, such as 
equest_id, ip_address, and user_agent, improving log traceability and enabling more structured logging.
- **SQL Query Logging**: Added functionality to log SQL queries executed during the request lifecycle. The middleware tracks the number of queries executed and logs them conditionally based on the configuration, allowing developers to debug SQL interactions effectively.
- **Streaming Content Handling**: Enhanced handling of streaming responses by introducing dedicated synchronous and asynchronous streaming wrappers, ensuring accurate logging during streaming operations.

This refactor improves the logging capabilities of the middleware while ensuring it can operate in both sync and async contexts, making it a more robust component for logging request-related information in Django applications.

Closes Lazarus-org#95
Copy link
Collaborator

@ARYAN-NIKNEZHAD ARYAN-NIKNEZHAD left a comment

Choose a reason for hiding this comment

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

Approved 👌

@ARYAN-NIKNEZHAD ARYAN-NIKNEZHAD added the enhancement New feature or request label Oct 4, 2024
@ARYAN-NIKNEZHAD ARYAN-NIKNEZHAD merged commit c8a2bb2 into Lazarus-org:main Oct 4, 2024
5 checks passed
@MEHRSHAD-MIRSHEKARY MEHRSHAD-MIRSHEKARY deleted the feat/middleware branch October 11, 2024 20:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Asynchronous Mode Support in Request Middleware and Improve Functionality
2 participants