-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ENHANCEMENT]
argilla
: expose webhooks API (#5490)
# Description <!-- Please include a summary of the changes and the related issue. Please also include relevant motivation and context. List any dependencies that are required for this change. --> This PR exposes the new webhooks API through the low-level `client.api` component. Once the backend API and SDK flows are validated, we can work on exposing webhooks as client resource level **Type of change** <!-- Please delete options that are not relevant. Remember to title the PR according to the type of change --> - Bug fix (non-breaking change which fixes an issue) - New feature (non-breaking change which adds functionality) - Breaking change (fix or feature that would cause existing functionality to not work as expected) - Refactor (change restructuring the codebase without changing functionality) - Improvement (change adding some improvement to an existing functionality) - Documentation update **How Has This Been Tested** <!-- Please add some reference about how your feature has been tested. --> **Checklist** <!-- Please go over the list and make sure you've taken everything into account --> - I added relevant documentation - I followed the style guidelines of this project - I did a self-review of my code - I made corresponding changes to the documentation - I confirm My changes generate no new warnings - I have added tests that prove my fix is effective or that my feature works - I have added relevant notes to the CHANGELOG.md file (See https://keepachangelog.com/)
- Loading branch information
1 parent
bea6eb0
commit 48a0dfe
Showing
2 changed files
with
149 additions
and
2 deletions.
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
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,137 @@ | ||
# Copyright 2024-present, Argilla, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
__all__ = ["WebhooksAPI"] | ||
|
||
from typing import List, Optional | ||
|
||
import httpx | ||
from pydantic import ConfigDict, Field | ||
|
||
from argilla._api._base import ResourceAPI | ||
from argilla._exceptions import api_error_handler | ||
from argilla._models import ResourceModel | ||
|
||
|
||
class WebhookModel(ResourceModel): | ||
url: str | ||
events: List[str] | ||
enabled: bool = True | ||
description: Optional[str] = None | ||
|
||
secret: Optional[str] = Field(None, description="Webhook secret. Read-only.") | ||
|
||
model_config = ConfigDict( | ||
validate_assignment=True, | ||
str_strip_whitespace=True, | ||
) | ||
|
||
|
||
class WebhooksAPI(ResourceAPI[WebhookModel]): | ||
http_client: httpx.Client | ||
url_stub = "/api/v1/webhooks" | ||
|
||
@api_error_handler | ||
def list(self) -> List[WebhookModel]: | ||
""" | ||
Get a list of all webhooks | ||
Returns: | ||
List[WebhookModel]: List of webhooks | ||
""" | ||
response = self.http_client.get(url=self.url_stub) | ||
response.raise_for_status() | ||
response_json = response.json() | ||
webhooks = self._model_from_jsons(json_data=response_json["items"]) | ||
self._log_message(message=f"Got {len(webhooks)} webhooks") | ||
return webhooks | ||
|
||
@api_error_handler | ||
def create(self, webhook: WebhookModel) -> WebhookModel: | ||
""" | ||
Create a webhook | ||
Args: | ||
webhook (WebhookModel): Webhook to create | ||
Returns: | ||
WebhookModel: Created webhook | ||
""" | ||
response = self.http_client.post( | ||
url=self.url_stub, | ||
json={ | ||
"url": webhook.url, | ||
"events": webhook.events, | ||
"description": webhook.description, | ||
}, | ||
) | ||
response.raise_for_status() | ||
response_json = response.json() | ||
webhook = self._model_from_json(json_data=response_json) | ||
self._log_message(message=f"Created webhook with id {webhook.id}") | ||
return webhook | ||
|
||
@api_error_handler | ||
def delete(self, webhook_id: str) -> None: | ||
""" | ||
Delete a webhook | ||
Args: | ||
webhook_id (str): ID of the webhook to delete | ||
""" | ||
response = self.http_client.delete(url=f"{self.url_stub}/{webhook_id}") | ||
response.raise_for_status() | ||
self._log_message(message=f"Deleted webhook with id {webhook_id}") | ||
|
||
@api_error_handler | ||
def update(self, webhook: WebhookModel) -> WebhookModel: | ||
""" | ||
Update a webhook | ||
Args: | ||
webhook (WebhookModel): Webhook to update | ||
Returns: | ||
WebhookModel: Updated webhook | ||
""" | ||
response = self.http_client.patch(url=f"{self.url_stub}/{webhook.id}", json=webhook.model_dump()) | ||
response.raise_for_status() | ||
response_json = response.json() | ||
webhook = self._model_from_json(json_data=response_json) | ||
self._log_message(message=f"Updated webhook with id {webhook.id}") | ||
return webhook | ||
|
||
@api_error_handler | ||
def ping(self, webhook_id: str) -> None: | ||
""" | ||
Ping a webhook | ||
Args: | ||
webhook_id (str): ID of the webhook to ping | ||
""" | ||
response = self.http_client.post(url=f"{self.url_stub}/{webhook_id}/ping") | ||
response.raise_for_status() | ||
self._log_message(message=f"Pinged webhook with id {webhook_id}") | ||
|
||
@staticmethod | ||
def _model_from_json(json_data: dict) -> WebhookModel: | ||
return WebhookModel.model_validate(json_data) | ||
|
||
def _model_from_jsons(self, json_data: List[dict]) -> List[WebhookModel]: | ||
return list(map(self._model_from_json, json_data)) |