fix: use standard odoo jsonrpc protocol
This commit is contained in:
@@ -52,9 +52,16 @@ def load_config():
|
||||
|
||||
required_options = [
|
||||
("odoo", "url"),
|
||||
("odoo", "database"),
|
||||
("odoo", "api_key"),
|
||||
("rocket", "webhook_url"),
|
||||
]
|
||||
if not (
|
||||
conf_wapt.has_option("odoo", "uid")
|
||||
or conf_wapt.has_option("odoo", "username")
|
||||
):
|
||||
required_options.append(("odoo", "username"))
|
||||
|
||||
missing_options = [
|
||||
"%s.%s" % (section, option)
|
||||
for section, option in required_options
|
||||
@@ -85,31 +92,27 @@ def get_http_timeout(conf_wapt):
|
||||
return conf_wapt.getint("http", "timeout", fallback=DEFAULT_TIMEOUT)
|
||||
|
||||
|
||||
def build_odoo_payload(conf_wapt):
|
||||
def build_odoo_payload(service, method, args):
|
||||
return {
|
||||
"jsonrpc": "2.0",
|
||||
"method": "call",
|
||||
"params": {
|
||||
"model": conf_wapt.get("odoo", "model", fallback="helpdesk.ticket"),
|
||||
"method": "search_read",
|
||||
"args": [[], ["id", "write_date"]],
|
||||
"service": service,
|
||||
"method": method,
|
||||
"args": args,
|
||||
},
|
||||
"id": 1,
|
||||
}
|
||||
|
||||
|
||||
def fetch_odoo_tickets(conf_wapt):
|
||||
def call_odoo_jsonrpc(conf_wapt, service, method, args):
|
||||
odoo_url = conf_wapt.get("odoo", "url").rstrip("/")
|
||||
endpoint = "%s/jsonrpc" % odoo_url
|
||||
headers = {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": "Bearer %s" % conf_wapt.get("odoo", "api_key"),
|
||||
}
|
||||
|
||||
response = requests.post(
|
||||
endpoint,
|
||||
json=build_odoo_payload(conf_wapt),
|
||||
headers=headers,
|
||||
json=build_odoo_payload(service, method, args),
|
||||
headers={"Content-Type": "application/json"},
|
||||
timeout=get_http_timeout(conf_wapt),
|
||||
)
|
||||
response.raise_for_status()
|
||||
@@ -123,6 +126,45 @@ def fetch_odoo_tickets(conf_wapt):
|
||||
return response_data["result"]
|
||||
|
||||
|
||||
def get_odoo_uid(conf_wapt):
|
||||
if conf_wapt.has_option("odoo", "uid"):
|
||||
return conf_wapt.getint("odoo", "uid")
|
||||
|
||||
uid = call_odoo_jsonrpc(
|
||||
conf_wapt,
|
||||
"common",
|
||||
"authenticate",
|
||||
[
|
||||
conf_wapt.get("odoo", "database"),
|
||||
conf_wapt.get("odoo", "username"),
|
||||
conf_wapt.get("odoo", "api_key"),
|
||||
{},
|
||||
],
|
||||
)
|
||||
if not uid:
|
||||
raise Exception("Authentification Odoo impossible")
|
||||
return uid
|
||||
|
||||
|
||||
def fetch_odoo_tickets(conf_wapt):
|
||||
uid = get_odoo_uid(conf_wapt)
|
||||
|
||||
return call_odoo_jsonrpc(
|
||||
conf_wapt,
|
||||
"object",
|
||||
"execute_kw",
|
||||
[
|
||||
conf_wapt.get("odoo", "database"),
|
||||
uid,
|
||||
conf_wapt.get("odoo", "api_key"),
|
||||
conf_wapt.get("odoo", "model", fallback="helpdesk.ticket"),
|
||||
"search_read",
|
||||
[[]],
|
||||
{"fields": ["id", "write_date"]},
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
def load_tickets_state(state_path):
|
||||
if not os.path.exists(state_path):
|
||||
return {}
|
||||
|
||||
Reference in New Issue
Block a user