Skip to content

Commit

Permalink
[ENHANCEMENT] argilla: expose webhooks API (#5490)
Browse files Browse the repository at this point in the history
# 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
frascuchon authored Sep 16, 2024
1 parent bea6eb0 commit 48a0dfe
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 2 deletions.
14 changes: 12 additions & 2 deletions argilla/src/argilla/_api/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
from typing import Optional

import httpx

from argilla._api._webhooks import WebhooksAPI
from argilla._exceptions._api import UnauthorizedError
from argilla._exceptions._client import ArgillaCredentialsError

Expand Down Expand Up @@ -46,15 +48,19 @@ class ArgillaAPI:
def __init__(self, http_client: httpx.Client):
self.http_client = http_client

self.__users = UsersAPI(http_client=self.http_client)
self.__workspaces = WorkspacesAPI(http_client=self.http_client)

self.__datasets = DatasetsAPI(http_client=self.http_client)
self.__users = UsersAPI(http_client=self.http_client)
self.__fields = FieldsAPI(http_client=self.http_client)
self.__questions = QuestionsAPI(http_client=self.http_client)
self.__records = RecordsAPI(http_client=self.http_client)
self.__vectors = VectorsAPI(http_client=self.http_client)
self.__metadata = MetadataAPI(http_client=self.http_client)

self.__records = RecordsAPI(http_client=self.http_client)

self.__webhooks = WebhooksAPI(http_client=self.http_client)

@property
def workspaces(self) -> "WorkspacesAPI":
return self.__workspaces
Expand Down Expand Up @@ -87,6 +93,10 @@ def vectors(self) -> "VectorsAPI":
def metadata(self) -> "MetadataAPI":
return self.__metadata

@property
def webhooks(self) -> "WebhooksAPI":
return self.__webhooks


class APIClient:
"""Initialize the SDK with the given API URL and API key.
Expand Down
137 changes: 137 additions & 0 deletions argilla/src/argilla/_api/_webhooks.py
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))

0 comments on commit 48a0dfe

Please sign in to comment.