Skip to content

Commit

Permalink
Datashare Websub: Fixed bugs
Browse files Browse the repository at this point in the history
Signed-off-by: Lalith Kota <[email protected]>
  • Loading branch information
lalithkota committed Nov 14, 2024
1 parent 5847dd2 commit b9c7098
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
1 change: 1 addition & 0 deletions g2p_registry_datashare_websub/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"g2p_registry_base",
"g2p_registry_individual",
"g2p_registry_group",
"g2p_registry_membership",
],
"external_dependencies": {"python": ["jq"]},
"data": [
Expand Down
56 changes: 39 additions & 17 deletions g2p_registry_datashare_websub/models/datashare_config_websub.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import os
from datetime import datetime, timedelta
from urllib.parse import parse_qs

import jq
import requests
Expand All @@ -14,7 +15,7 @@
WEBSUB_BASE_URL = os.getenv("WEBSUB_BASE_URL", "http://websub/hub")
WEBSUB_AUTH_URL = os.getenv(
"WEBSUB_AUTH_URL",
"http://keycloak.keycloak/realms/openg2p/protocol/openid-connect/token",
"http://keycloak.keycloak/realms/master/protocol/openid-connect/token",
)
WEBSUB_AUTH_CLIENT_ID = os.getenv("WEBSUB_AUTH_CLIENT_ID", "openg2p-admin-client")
WEBSUB_AUTH_CLIENT_SECRET = os.getenv("WEBSUB_AUTH_CLIENT_SECRET", "")
Expand All @@ -27,7 +28,7 @@ class G2PDatashareConfigWebsub(models.Model):

name = fields.Char(required=True)

partner_id = fields.Char(string="Partner ID", required=True)
partner_id = fields.Char(string="Partner ID")

event_type = fields.Selection(
[
Expand Down Expand Up @@ -72,11 +73,14 @@ def create(self, vals):
return res

def write(self, vals):
if isinstance(vals, dict) and "event_type" in vals:
for rec in self:
rec.deregister_websub_event()
try:
if isinstance(vals, dict) and ("event_type" in vals or "partner_id" in vals):
for rec in self:
rec.deregister_websub_event()
except Exception:
_logger.exception("WebSub - Changed event: couldnt deregister")
res = super().write(vals)
if isinstance(vals, dict) and "event_type" in vals:
if isinstance(vals, dict) and ("event_type" in vals or "partner_id" in vals):
for rec in self:
rec.register_websub_event()
return res
Expand All @@ -102,21 +106,22 @@ def publish_by_publisher(self, data: dict):
record_id = data["id"]
record = self.env["res.partner"].browse(record_id)
record_data = self.get_full_record_data(record)
record_data = {"data": data, "record_data": record_data}
record_data = WebSubJSONEncoder.python_dict_to_json_dict(
{
"web_base_url": web_base_url,
"publisher": self.read()[0],
"curr_datetime": curr_datetime,
"data": data,
"record_data": record_data,
},
)

if not jq.first(self.condition_jq, record_data):
return

data_transformed = jq.first(
self.transform_data_jq,
WebSubJSONEncoder.python_dict_to_json_dict(
{
"web_base_url": web_base_url,
"publisher": self.read()[0],
"curr_datetime": curr_datetime,
**record_data,
},
),
record_data,
)
self.publish_event_websub(data_transformed)

Expand All @@ -127,22 +132,36 @@ def publish_event_websub(self, data):
self.websub_base_url,
params={
"hub.mode": "publish",
"hub.topic": f"{self.partner_id}{self.topic_joiner}{self.event_type}",
"hub.topic": (
f"{self.partner_id}"
f"{self.topic_joiner if self.partner_id else ''}"
f"{self.event_type}"
),
},
headers={"Authorization": f"Bearer {token}"},
json=data,
timeout=self.websub_api_timeout,
)
res.raise_for_status()
_logger.info("WebSub Publish Success. Response: %s. Headers: %s", res.text, res.headers)
res = parse_qs(res.text)
if not len(res.get("hub.mode", [])) or res["hub.mode"][0].lower() != "accepted":
raise ValueError("WebSub Publish: Invalid hub response")

def register_websub_event(self, mode="register"):
self.ensure_one()
token = self.get_access_token()
res = requests.post(
self.websub_base_url,
headers={"Authorization": f"Bearer {token}"},
data={"hub.mode": mode, "hub.topic": self.event_type},
data={
"hub.mode": mode,
"hub.topic": (
f"{self.partner_id}"
f"{self.topic_joiner if self.partner_id else ''}"
f"{self.event_type}"
),
},
timeout=self.websub_api_timeout,
)
res.raise_for_status()
Expand All @@ -151,6 +170,9 @@ def register_websub_event(self, mode="register"):
res.text,
res.headers,
)
res = parse_qs(res.text)
if not len(res.get("hub.mode", [])) or res["hub.mode"][0].lower() != "accepted":
raise ValueError("WebSub Topic Register: Invalid hub response")

def deregister_websub_event(self):
return self.register_websub_event(mode="deregister")
Expand Down

0 comments on commit b9c7098

Please sign in to comment.