-
Notifications
You must be signed in to change notification settings - Fork 432
feat: add decorator factory to create your own middleware #17
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
Merged
Merged
Changes from 33 commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
3555b43
feat(utils): add decorator factory
heitorlessa 11ff229
improv: use partial to reduce complexity
heitorlessa 774ee3d
improv: add error handling
heitorlessa 5364ca9
chore: type hint
heitorlessa 55c23d4
docs: include pypi downloads badge
heitorlessa 31eb0e2
feat: opt in to trace each middleware that runs
heitorlessa 5a79b12
improv: add initial util tests
heitorlessa ee52a15
improv: test explicit and implicit trace_execution
heitorlessa f57d000
improv: test decorator with params
heitorlessa 19d02dc
chore: linting
heitorlessa 021a324
docs: include utilities
heitorlessa b3cee6c
improv: correct tests, dec_factory only for func
heitorlessa 3390c52
improv: make util name more explicit
heitorlessa 4baf118
improv: doc trace_execution, fix casting
heitorlessa e7c2bfe
docs: add limitations, improve syntax
heitorlessa 1e8af14
docs: use new docs syntax
heitorlessa 1a62571
fix: remove middleware decorator from libs
heitorlessa 9d08ea0
feat: build docs in CI
heitorlessa 333e5c8
chore: linting
heitorlessa abbb12d
fix: CI python-version type
heitorlessa 600c9b1
chore: remove docs CI
heitorlessa 4f1ed1f
chore: kick CI
heitorlessa ef5d901
Merge branch 'develop' into feat/dec_factory
heitorlessa 4d1548f
chore: include build badge master branch
heitorlessa c0e3227
chore: refactor naming
heitorlessa 69b3529
fix: rearrange tracing tests
heitorlessa c3419c6
improv(tracer): toggle default auto patching
heitorlessa 365d561
feat(tracer): retrieve registered class instance
heitorlessa c3aad5f
fix(Makefile): make cov target more explicit
heitorlessa 98aeb77
improv(Register): support multiple classes reg.
heitorlessa 37d6a19
improv(Register): inject class methods correctly
heitorlessa 250e47f
docs: add how to reutilize Tracer
heitorlessa bfe3404
improv(tracer): test auto patch method
heitorlessa 1f0a6c9
improv: address nicolas feedback
heitorlessa 55ce3a1
improv: update example to reflect middleware feat
heitorlessa fddc26b
fix: metric dimension in root blob
heitorlessa 073ee4f
chore: version bump
heitorlessa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,65 @@ | ||
from typing import AnyStr, Dict, Tuple, Type | ||
|
||
|
||
class RegisterMeta(type): | ||
_instances = {} | ||
|
||
def __call__(cls: Type, *args: Tuple, **kwargs: Dict): | ||
"""Register class instance at initialization | ||
|
||
It only returns an existing instance via `instance` | ||
method e.g. `Tracer.instance()`. | ||
|
||
Not a Singleton per se as it only returns an existing | ||
instance via the `instance` method. | ||
|
||
Parameters | ||
---------- | ||
cls : type | ||
Class using metaclass | ||
args : tuple | ||
Tuple with arguments for class instantiation | ||
kwargs : dict | ||
Dict with all keyword arguments | ||
""" | ||
if cls not in RegisterMeta._instances: | ||
RegisterMeta._instances[cls] = super().__call__(*args, **kwargs) | ||
return RegisterMeta._instances[cls] | ||
|
||
return super().__call__(**kwargs) | ||
|
||
def __init__(cls: Type, cls_name: AnyStr, bases: Tuple, class_dict: Dict): | ||
"""Inject instance, clear_instance classmethods to newly built class | ||
|
||
Parameters | ||
---------- | ||
cls : type | ||
Class using metaclass | ||
cls_name : str | ||
Class name | ||
bases : tuple | ||
Inherited classes | ||
class_dict : dict | ||
Class body as dict | ||
""" | ||
setattr(cls, instance.__name__, classmethod(instance)) | ||
setattr(cls, clear_instance.__name__, classmethod(clear_instance)) | ||
|
||
|
||
def instance(cls): | ||
"""Returns registered class instance | ||
|
||
This allows us to prevent double initialization | ||
when needed, reuse previous instance and its attributes, | ||
and still allow multiple inheritance and __new__. | ||
""" | ||
if cls not in RegisterMeta._instances: | ||
return cls.__call__(cls) | ||
|
||
return RegisterMeta._instances[cls] | ||
|
||
|
||
def clear_instance(cls): | ||
"""Destroys registered class instance""" | ||
if cls in RegisterMeta._instances: | ||
del RegisterMeta._instances[cls] |
This file contains hidden or 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 hidden or 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,4 @@ | ||
""" Utilities to enhance middlewares """ | ||
from .factory import lambda_handler_decorator | ||
|
||
__all__ = ["lambda_handler_decorator"] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.