-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Refactor Variable CRUD methods in client #16564
Merged
Merged
Changes from all commits
Commits
Show all changes
4 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 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
Empty file.
This file contains 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,157 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
import httpx | ||
|
||
from prefect.client.orchestration.base import BaseAsyncClient, BaseClient | ||
from prefect.exceptions import ObjectNotFound | ||
|
||
if TYPE_CHECKING: | ||
from prefect.client.schemas.actions import ( | ||
VariableCreate, | ||
VariableUpdate, | ||
) | ||
from prefect.client.schemas.objects import ( | ||
Variable, | ||
) | ||
|
||
|
||
class VariableClient(BaseClient): | ||
def create_variable(self, variable: "VariableCreate") -> "Variable": | ||
""" | ||
Creates an variable with the provided configuration. | ||
Args: | ||
variable: Desired configuration for the new variable. | ||
Returns: | ||
Information about the newly created variable. | ||
""" | ||
response = self._client.post( | ||
"/variables/", | ||
json=variable.model_dump(mode="json", exclude_unset=True), | ||
) | ||
from prefect.client.schemas.objects import Variable | ||
|
||
return Variable.model_validate(response.json()) | ||
|
||
def read_variable_by_name(self, name: str) -> "Variable | None": | ||
"""Reads a variable by name. Returns None if no variable is found.""" | ||
try: | ||
response = self.request( | ||
"GET", "/variables/name/{name}", path_params={"name": name} | ||
) | ||
from prefect.client.schemas.objects import Variable | ||
|
||
return Variable(**response.json()) | ||
except httpx.HTTPStatusError as e: | ||
if e.response.status_code == 404: | ||
return None | ||
else: | ||
raise | ||
|
||
def read_variables(self, limit: int | None = None) -> list["Variable"]: | ||
"""Reads all variables.""" | ||
response = self.request("POST", "/variables/filter", json={"limit": limit}) | ||
from prefect.client.schemas.objects import Variable | ||
|
||
return Variable.model_validate_list(response.json()) | ||
|
||
def update_variable(self, variable: "VariableUpdate") -> None: | ||
""" | ||
Updates a variable with the provided configuration. | ||
Args: | ||
variable: Desired configuration for the updated variable. | ||
Returns: | ||
Information about the updated variable. | ||
""" | ||
self._client.patch( | ||
f"/variables/name/{variable.name}", | ||
json=variable.model_dump(mode="json", exclude_unset=True), | ||
) | ||
return None | ||
|
||
def delete_variable_by_name(self, name: str) -> None: | ||
"""Deletes a variable by name.""" | ||
try: | ||
self.request( | ||
"DELETE", | ||
"/variables/name/{name}", | ||
path_params={"name": name}, | ||
) | ||
return None | ||
except httpx.HTTPStatusError as e: | ||
if e.response.status_code == 404: | ||
raise ObjectNotFound(http_exc=e) from e | ||
else: | ||
raise | ||
|
||
|
||
class VariableAsyncClient(BaseAsyncClient): | ||
async def create_variable(self, variable: "VariableCreate") -> "Variable": | ||
"""Creates a variable with the provided configuration.""" | ||
response = await self._client.post( | ||
"/variables/", | ||
json=variable.model_dump(mode="json", exclude_unset=True), | ||
) | ||
from prefect.client.schemas.objects import Variable | ||
|
||
return Variable.model_validate(response.json()) | ||
|
||
async def read_variable_by_name(self, name: str) -> "Variable | None": | ||
"""Reads a variable by name. Returns None if no variable is found.""" | ||
try: | ||
response = await self.request( | ||
"GET", | ||
"/variables/name/{name}", | ||
path_params={"name": name}, | ||
) | ||
from prefect.client.schemas.objects import Variable | ||
|
||
return Variable.model_validate(response.json()) | ||
except httpx.HTTPStatusError as e: | ||
if e.response.status_code == 404: | ||
return None | ||
else: | ||
raise | ||
|
||
async def read_variables(self, limit: int | None = None) -> list["Variable"]: | ||
"""Reads all variables.""" | ||
response = await self.request( | ||
"POST", "/variables/filter", json={"limit": limit} | ||
) | ||
from prefect.client.schemas.objects import Variable | ||
|
||
return Variable.model_validate_list(response.json()) | ||
|
||
async def update_variable(self, variable: "VariableUpdate") -> None: | ||
""" | ||
Updates a variable with the provided configuration. | ||
Args: | ||
variable: Desired configuration for the updated variable. | ||
Returns: | ||
Information about the updated variable. | ||
""" | ||
await self.request( | ||
"PATCH", | ||
"/variables/name/{name}", | ||
path_params={"name": variable.name}, | ||
json=variable.model_dump(mode="json", exclude_unset=True), | ||
) | ||
return None | ||
|
||
async def delete_variable_by_name(self, name: str) -> None: | ||
"""Deletes a variable by name.""" | ||
try: | ||
await self.request( | ||
"DELETE", | ||
"/variables/name/{name}", | ||
path_params={"name": name}, | ||
) | ||
except httpx.HTTPStatusError as e: | ||
if e.response.status_code == 404: | ||
raise ObjectNotFound(http_exc=e) from e | ||
else: | ||
raise |
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.
Do you want to use
self.request
for type safety here and in the other methods?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.
woof, yep!