from odoo import models import requests import urllib.parse from odoo.exceptions import UserError import json class WatiApi(models.Model): _name = 'wati.api' def api_header(self): api_token = self.env['ir.config_parameter'].get_param('wati.api_token') return { "Authorization": "Bearer %s" % api_token } def api_url(self): return self.env['ir.config_parameter'].get_param('wati.api_url') def http_get(self, endpoint: str, url_params: dict): try: url_params = urllib.parse.urlencode(url_params) url = self.api_url() + endpoint + "?%s" % url_params headers = self.api_header() response = requests.get(url, headers=headers) return response.json() except Exception: return Exception def http_post(self, endpoint: str, data: dict): try: url = self.api_url() + endpoint headers = self.api_header() response = requests.post(url, headers=headers, json=data) return response.json() except Exception: return Exception def _test(self, method): res = False if method == 'GET': res = self.http_get('/api/v1/getContacts', { "name": 'Darren', "pageSize": 10, "pageNumber": 1 }) elif method == 'POST': res = self.http_post('/api/v1/updateContactAttributes/6281290696017', { "customParams": [{"name": "name", "value": "Darren"}] }) raise UserError(json.dumps(res, indent=4, sort_keys=True))