-
Notifications
You must be signed in to change notification settings - Fork 1
Add FastlyResource Base Class #47
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 all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| """Internal base class for Fastly resource wrappers | ||
|
|
||
| This module provides an internal generic base class for wrapping WIT binding | ||
| resources with consistent lifecycle management and context manager protocol. | ||
|
|
||
| **Note**: This module is for internal SDK use only. End users should not | ||
| need to import or use these classes directly. Instead, use the public resource | ||
| classes like ConfigStore, RateCounter, PenaltyBox, and LogEndpoint. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from types import TracebackType | ||
| from typing import Protocol, Self | ||
|
|
||
|
|
||
| class WitResource(Protocol): | ||
| """Internal protocol for WIT-generated resource types. | ||
|
|
||
| This protocol defines the context manager interface that all WIT resources | ||
| must implement for resource lifecycle management. | ||
| """ | ||
|
|
||
| def __enter__(self) -> Self: | ||
| """Enter the context manager.""" | ||
| ... | ||
|
|
||
| def __exit__( | ||
| self, | ||
| exc_type: type[BaseException] | None, | ||
| exc_value: BaseException | None, | ||
| traceback: TracebackType | None, | ||
| ) -> bool | None: | ||
| """Exit the context manager and release resources.""" | ||
| ... | ||
|
|
||
|
|
||
| class FastlyResource[T: WitResource]: | ||
| """Internal base class for Fastly resource wrappers. | ||
|
|
||
| This generic base class provides consistent context manager protocol and | ||
| resource lifecycle management for all Fastly resource types that wrap | ||
| WIT bindings (e.g., ConfigStore, RateCounter, PenaltyBox, LogEndpoint). | ||
|
|
||
| The type parameter T represents the underlying WIT binding resource type | ||
| and must satisfy the WitResource protocol (context manager support). | ||
| """ | ||
|
|
||
| def __init__(self, wit_resource: T): | ||
| """Initialize the resource wrapper with an inner WIT binding. | ||
|
|
||
| :param wit_resource: The underlying WIT binding resource to wrap | ||
| """ | ||
| self._wit_resource = wit_resource | ||
|
|
||
| def close(self) -> None: | ||
| """Explicitly close the resource, releasing its resources. | ||
|
|
||
| This is called automatically when using the resource as a context | ||
| manager. If not called explicitly, resources will eventually be freed | ||
| by the garbage collector. | ||
|
|
||
| Note: Attempting to use the resource after it is closed will result | ||
| in a trap. | ||
| """ | ||
| self._wit_resource.__exit__(None, None, None) | ||
|
|
||
| def __enter__(self) -> Self: | ||
| """Context manager entry. | ||
|
|
||
| Allows use of the resource in a 'with' statement. | ||
|
|
||
| Example:: | ||
|
|
||
| with Resource.open("foo") as foo: | ||
| value = foo.bar("baz") | ||
| """ | ||
| return self | ||
|
|
||
| def __exit__( | ||
| self, | ||
| exc_type: type[BaseException] | None, | ||
| exc_val: BaseException | None, | ||
| exc_tb: TracebackType | None, | ||
| ) -> None: | ||
| """Context manager exit. | ||
|
|
||
| Use of the context manager will free up the underlying host resource on | ||
| exit. Referencing the resource after context manager exit will result in | ||
| a trap. | ||
|
|
||
| Exception information from the context is passed through to the inner | ||
| resource's __exit__ method for proper cleanup. | ||
|
|
||
| :param exc_type: Exception type if an exception occurred, None otherwise | ||
| :param exc_val: Exception value if an exception occurred, None otherwise | ||
| :param exc_tb: Exception traceback if an exception occurred, None otherwise | ||
| """ | ||
| self._wit_resource.__exit__(exc_type, exc_val, exc_tb) | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we call this
_resource.py, then we don't have to scream at the read so much about it being internal. :-)