summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/wati_api.py
blob: 476f542d536a90cda443fca01f5b44cea065d3da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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))