Skip to content

Commit

Permalink
[PT-5013] Drop Webhooks class (#620)
Browse files Browse the repository at this point in the history
* Drop Webhooks class

* Bump version

* Address comments
  • Loading branch information
javidq authored May 30, 2024
1 parent 3286ad6 commit 2d0e2ff
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 78 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ You can check your current version with the following command:
```

For more information, see [UP42 Python package description](https://pypi.org/project/up42-py/).
## 1.0.4a7

**May 30, 2024**

- Moved instance methods of `Webhooks` class to class methods of `Webhook` class and dropped the former.

## 1.0.4a6

**May 30, 2024**
Expand Down
2 changes: 0 additions & 2 deletions docs/macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ def format_funcs(function_methods: List[str]) -> str:
env.variables.docstring_order = indent(up42.order.Order.__doc__)
env.variables.docstring_storage = indent(up42.storage.Storage.__doc__)
env.variables.docstring_asset = indent(up42.asset.Asset.__doc__)
env.variables.docstring_webhooks = indent(up42.webhooks.Webhooks.__doc__)

# Class functions for reference and structure chapter
env.variables.funcs_up42 = get_methods(up42)
Expand All @@ -86,5 +85,4 @@ def format_funcs(function_methods: List[str]) -> str:
env.variables.funcs_order = get_methods(up42.order.Order)
env.variables.funcs_storage = get_methods(up42.storage.Storage)
env.variables.funcs_asset = get_methods(up42.asset.Asset)
env.variables.funcs_webhooks = get_methods(up42.webhooks.Webhooks)
env.variables.funcs_webhook = get_methods(up42.webhooks.Webhook)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "up42-py"
version = "1.0.4a6"
version = "1.0.4a7"
description = "Python SDK for UP42, the geospatial marketplace and developer platform."
authors = ["UP42 GmbH <[email protected]>"]
license = "https://github.com/up42/up42-py/blob/master/LICENSE"
Expand Down
15 changes: 7 additions & 8 deletions tests/test_initialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ def workspace(auth_mock):


@pytest.fixture(name="webhooks")
def _webhooks(auth_mock):
with mock.patch("up42.webhooks.Webhooks") as webhooks_class_mock:
yield webhooks_class_mock.return_value
webhooks_class_mock.assert_called_with(auth=auth_mock, workspace_id=constants.WORKSPACE_ID)
def _webhooks():
with mock.patch("up42.webhooks.Webhook") as webhooks_class_mock:
yield webhooks_class_mock


def test_should_initialize_objects(
Expand Down Expand Up @@ -51,9 +50,9 @@ def test_should_get_webhook_events(webhooks: mock.MagicMock):
@pytest.mark.parametrize("return_json", [False, True])
def test_should_get_webhooks(webhooks: mock.MagicMock, return_json):
hooks = mock.MagicMock()
webhooks.get_webhooks.return_value = hooks
webhooks.all.return_value = hooks
assert up42.get_webhooks(return_json=return_json) == hooks
webhooks.get_webhooks.assert_called_with(return_json=return_json)
webhooks.all.assert_called_with(return_json=return_json)


def test_should_create_webhook(webhooks: mock.MagicMock):
Expand All @@ -63,6 +62,6 @@ def test_should_create_webhook(webhooks: mock.MagicMock):
active = True
secret = "secret"
webhook = mock.MagicMock()
webhooks.create_webhook.return_value = webhook
webhooks.create.return_value = webhook
assert webhook == up42.create_webhook(name, url, events, active, secret)
webhooks.create_webhook.assert_called_with(name=name, url=url, events=events, active=active, secret=secret)
webhooks.create.assert_called_with(name=name, url=url, events=events, active=active, secret=secret)
20 changes: 7 additions & 13 deletions tests/test_webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,6 @@ def test_should_save(self, requests_mock: req_mock.Mocker, webhook):
assert hook == dataclasses.replace(webhook, updated_at=updated_at, **updates)
assert requests_mock.last_request and requests_mock.last_request.json() == updates


class TestWebhooks:
@pytest.fixture(autouse=True)
def setup_webhooks(self, auth_mock):
self.webhooks = webhooks.Webhooks(auth_mock, constants.WORKSPACE_ID)

def test_should_get_webhook_events(self, requests_mock: req_mock.Mocker):
url = f"{constants.API_HOST}/webhooks/events"
events = ["some-event"]
Expand All @@ -182,18 +176,18 @@ def test_should_get_webhook_events(self, requests_mock: req_mock.Mocker):
"error": {},
},
)
assert self.webhooks.get_webhook_events() == events
assert webhooks.Webhook.get_webhook_events() == events

def test_should_get_webhooks(self, requests_mock: req_mock.Mocker, webhook: webhooks.Webhook):
def test_should_get_all_webhooks(self, requests_mock: req_mock.Mocker, webhook: webhooks.Webhook):
requests_mock.get(HOOKS_URL, json={"data": [metadata]})
assert self.webhooks.get_webhooks() == [webhook]
assert webhooks.Webhook.all() == [webhook]

def test_should_get_webhooks_as_dict(self, requests_mock: req_mock.Mocker):
def test_should_get_all_webhooks_as_dict(self, requests_mock: req_mock.Mocker):
requests_mock.get(HOOKS_URL, json={"data": [metadata]})
assert self.webhooks.get_webhooks(return_json=True) == [metadata]
assert webhooks.Webhook.all(return_json=True) == [metadata]

@pytest.mark.parametrize("secret", [random_alphanumeric(), None])
def test_should_create_webhook(
def test_should_create(
self,
requests_mock: req_mock.Mocker,
webhook: webhooks.Webhook,
Expand All @@ -204,7 +198,7 @@ def test_should_create_webhook(
events = [random_alphanumeric()]
active = random.choice([True, False])
requests_mock.post(HOOKS_URL, json={"data": metadata})
assert self.webhooks.create_webhook(
assert webhooks.Webhook.create(
name=name, url=url, events=events, active=active, secret=secret
) == dataclasses.replace(webhook, name=name, url=url, active=active, events=events, secret=secret)

Expand Down
10 changes: 3 additions & 7 deletions up42/initialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ def get_webhooks(return_json: bool = False) -> List[webhooks.Webhook]:
Returns:
A list of the registered webhooks for this workspace.
"""
return webhooks.Webhooks(auth=base.workspace.auth, workspace_id=base.workspace.id).get_webhooks(
return_json=return_json
)
return webhooks.Webhook.all(return_json=return_json)


def create_webhook(
Expand All @@ -95,9 +93,7 @@ def create_webhook(
Returns:
A dict with details of the registered webhook.
"""
return webhooks.Webhooks(auth=base.workspace.auth, workspace_id=base.workspace.id).create_webhook(
name=name, url=url, events=events, active=active, secret=secret
)
return webhooks.Webhook.create(name=name, url=url, events=events, active=active, secret=secret)


def get_webhook_events() -> dict:
Expand All @@ -107,4 +103,4 @@ def get_webhook_events() -> dict:
Returns:
A dict of the available webhook events.
"""
return webhooks.Webhooks(auth=base.workspace.auth, workspace_id=base.workspace.id).get_webhook_events()
return webhooks.Webhook.get_webhook_events()
67 changes: 20 additions & 47 deletions up42/webhooks.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import dataclasses
from typing import List, Optional

from up42 import auth as up42_auth
from up42 import base, host, utils

logger = utils.get_logger(__name__)
Expand All @@ -22,12 +21,10 @@ class Webhook:

"""
# Webhook
Contains UP42 webhooks functionality to set up a custom callback e.g. when an order is finished
a webhook is triggered and an event notification is transmitted via HTTPS to a specific URL.
Webhook class to control a specific UP42 webhook, e.g. modify, test or delete the specific webhook.
```python
webhook = webhook.trigger_test_event()
```
Also see the [full webhook documentation](https://docs.up42.com/account/webhooks).
"""

def save(self):
Expand Down Expand Up @@ -84,7 +81,7 @@ def trigger_test_events(self) -> dict:
messages for each subscribed event to the specified webhook URL.
Returns:
A dict with information about the test events.
A dict with information about the emitted test events.
"""
url = host.endpoint(f"/workspaces/{self.workspace_id}/webhooks/{self.id}/tests")
return self.session.post(url=url).json()["data"]
Expand Down Expand Up @@ -127,43 +124,19 @@ def delete(self) -> None:
self.session.delete(url=url)
logger.info("Successfully deleted Webhook: %s", self.id)


class Webhooks:
"""
Contains UP42 webhooks functionality to set up a custom callback e.g. when an order is finished
he webhook is triggered and an event notification is transmitted via HTTPS to a specific URL.
Also see the [full webhook documentation](https://docs.up42.com/account/webhooks).
Create a new webhook or query a existing ones via the `up42` object, e.g.
```python
webhooks = up42.get_webhooks()
```
```python
webhook = up42.initialize_webhook(webhook_id = "...")
```
The resulting Webhook object lets you modify, test or delete the specific webhook, e.g.
```python
webhook = webhook.trigger_test_event()
```
"""

def __init__(self, auth: up42_auth.Auth, workspace_id: str):
self.auth = auth
self.workspace_id = workspace_id

def get_webhook_events(self) -> dict:
@classmethod
def get_webhook_events(cls) -> dict:
"""
Gets all available webhook events.
Returns:
A dict of the available webhook events.
"""
url = host.endpoint("/webhooks/events")
return self.auth.request(request_type="GET", url=url)["data"]
return cls.session.get(url=url).json()["data"]

def get_webhooks(self, return_json: bool = False) -> List[Webhook]:
@classmethod
def all(cls, return_json: bool = False) -> List["Webhook"]:
"""
Gets all registered webhooks for this workspace.
Expand All @@ -173,23 +146,23 @@ def get_webhooks(self, return_json: bool = False) -> List[Webhook]:
Returns:
A list of the registered webhooks for this workspace.
"""
url = host.endpoint(f"/workspaces/{self.workspace_id}/webhooks")
response_json = self.auth.request(request_type="GET", url=url)
logger.info("Queried %s webhooks.", len(response_json["data"]))
url = host.endpoint(f"/workspaces/{cls.workspace_id}/webhooks")
payload = cls.session.get(url=url).json()["data"]
logger.info("Queried %s webhooks.", len(payload))

if return_json:
return response_json["data"]
webhooks = [Webhook.from_metadata(metadata) for metadata in response_json["data"]]
return webhooks
return payload
return [cls.from_metadata(metadata) for metadata in payload]

def create_webhook(
self,
@classmethod
def create(
cls,
name: str,
url: str,
events: List[str],
active: bool = False,
secret: Optional[str] = None,
) -> Webhook:
) -> "Webhook":
"""
Registers a new webhook in the system.
Expand All @@ -201,8 +174,8 @@ def create_webhook(
secret: String that acts as signature to the https request sent to the url.
Returns:
A dict with details of the registered webhook.
The newly registered webhook.
"""
webhook = Webhook(name=name, url=url, events=events, secret=secret, active=active)
webhook = cls(name=name, url=url, events=events, secret=secret, active=active)
webhook.save()
return webhook

0 comments on commit 2d0e2ff

Please sign in to comment.