Skip to content
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

Reapply "Use restful hydra enpoint URLs (#3142)" #3146

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Fix the boolean filters in the API for the "new system" endpoints [#3139](https://github.com/opendatateam/udata/pull/3139)
- Update authlib dependency from 0.14.3 to 1.3.1 [#3135](https://github.com/opendatateam/udata/pull/3135)
- Add CORS on resource redirect [#3145](https://github.com/opendatateam/udata/pull/3145)
- Use hydra's RESTful endpoint URLs [#3142](https://github.com/opendatateam/udata/pull/3142)

## 9.1.4 (2024-08-26)

Expand Down
22 changes: 14 additions & 8 deletions udata/core/dataset/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,16 @@ def serialize_resource_for_event(resource):

@task(route="high.resource")
def publish(url, document, resource_id, action):
if action == EventMessageType.DELETED:
resource = None
else:
resource = serialize_resource_for_event(get_by(document.resources, "id", resource_id))
match action:
case EventMessageType.CREATED:
method = requests.post
resource = serialize_resource_for_event(get_by(document.resources, "id", resource_id))
case EventMessageType.MODIFIED:
method = requests.put
resource = serialize_resource_for_event(get_by(document.resources, "id", resource_id))
case EventMessageType.DELETED:
method = requests.delete
resource = None
payload = {
"resource_id": str(resource_id),
"dataset_id": str(document.id),
Expand All @@ -52,7 +58,7 @@ def publish(url, document, resource_id, action):
headers = {}
if current_app.config["RESOURCES_ANALYSER_API_KEY"]:
headers = {"Authorization": f"Bearer {current_app.config['RESOURCES_ANALYSER_API_KEY']}"}
r = requests.post(url, json=payload, headers=headers)
r = method(url, json=payload, headers=headers)
r.raise_for_status()


Expand All @@ -62,7 +68,7 @@ def publish_added_resource_message(sender, document, **kwargs):
"RESOURCES_ANALYSER_URI"
):
publish.delay(
f"{current_app.config.get('RESOURCES_ANALYSER_URI')}/api/resource/created/",
f"{current_app.config.get('RESOURCES_ANALYSER_URI')}/api/resources/",
document,
kwargs["resource_id"],
EventMessageType.CREATED,
Expand All @@ -75,7 +81,7 @@ def publish_updated_resource_message(sender, document, **kwargs):
"RESOURCES_ANALYSER_URI"
):
publish.delay(
f"{current_app.config.get('RESOURCES_ANALYSER_URI')}/api/resource/updated/",
f"{current_app.config.get('RESOURCES_ANALYSER_URI')}/api/resources/",
document,
kwargs["resource_id"],
EventMessageType.MODIFIED,
Expand All @@ -88,7 +94,7 @@ def publish_removed_resource_message(sender, document, **kwargs):
"RESOURCES_ANALYSER_URI"
):
publish.delay(
f"{current_app.config.get('RESOURCES_ANALYSER_URI')}/api/resource/deleted/",
f"{current_app.config.get('RESOURCES_ANALYSER_URI')}/api/resources/",
document,
kwargs["resource_id"],
EventMessageType.DELETED,
Expand Down
12 changes: 6 additions & 6 deletions udata/tests/dataset/test_dataset_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_publish_message_resource_created_no_api_key(self, mock_req):
}

mock_req.assert_called_with(
f"{current_app.config['RESOURCES_ANALYSER_URI']}/api/resource/created/",
f"{current_app.config['RESOURCES_ANALYSER_URI']}/api/resources/",
json=expected_value,
headers={}, # No RESOURCES_ANALYSER_API_KEY, no headers.
)
Expand All @@ -53,12 +53,12 @@ def test_publish_message_resource_created(self, mock_req):
dataset.add_resource(resource)

mock_req.assert_called_with(
f"{current_app.config['RESOURCES_ANALYSER_URI']}/api/resource/created/",
f"{current_app.config['RESOURCES_ANALYSER_URI']}/api/resources/",
json=expected_value,
headers={"Authorization": "Bearer foobar-api-key"},
)

@patch("requests.post")
@patch("requests.put")
@pytest.mark.options(RESOURCES_ANALYSER_API_KEY="foobar-api-key")
def test_publish_message_resource_modified(self, mock_req):
resource = ResourceFactory(schema=Schema(url="http://localhost/my-schema"))
Expand All @@ -77,7 +77,7 @@ def test_publish_message_resource_modified(self, mock_req):
dataset.update_resource(resource)

mock_req.assert_called_with(
f"{current_app.config['RESOURCES_ANALYSER_URI']}/api/resource/updated/",
f"{current_app.config['RESOURCES_ANALYSER_URI']}/api/resources/",
json=expected_value,
headers={"Authorization": "Bearer foobar-api-key"},
)
Expand All @@ -87,7 +87,7 @@ def test_publish_message_resource_modified(self, mock_req):
# (for example, encoding Embeds fails)
complexjson.dumps(expected_value)

@patch("requests.post")
@patch("requests.delete")
@pytest.mark.options(RESOURCES_ANALYSER_API_KEY="foobar-api-key")
def test_publish_message_resource_removed(self, mock_req):
resource = ResourceFactory()
Expand All @@ -104,7 +104,7 @@ def test_publish_message_resource_removed(self, mock_req):
dataset.remove_resource(resource)

mock_req.assert_called_with(
f"{current_app.config['RESOURCES_ANALYSER_URI']}/api/resource/deleted/",
f"{current_app.config['RESOURCES_ANALYSER_URI']}/api/resources/",
json=expected_value,
headers={"Authorization": "Bearer foobar-api-key"},
)