forked from alfredfrancis/ai-chatbot-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
73 lines (60 loc) · 2.19 KB
/
utils.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import json
import requests
from jinja2 import Undefined
from app import app
from app.entities.models import Entity
def split_sentence(sentence):
return sentence.split("###")
def get_synonyms():
"""
Build synonyms dict from DB
:return:
"""
synonyms = {}
for entity in Entity.objects:
for value in entity.entity_values:
for synonym in value.synonyms:
synonyms[synonym] = value.value
app.logger.info("loaded synonyms %s", synonyms)
return synonyms
def call_api(url, type, headers={}, parameters={}, is_json=False):
"""
Call external API
:param url:
:param type:
:param parameters:
:param is_json:
:return:
"""
app.logger.info("Initiating API Call with following info: url => {} payload => {}".format(url, parameters))
if "GET" in type:
response = requests.get(url, headers=headers, params=parameters, timeout=5)
elif "POST" in type:
if is_json:
response = requests.post(url, headers=headers, json=parameters, timeout=5)
else:
response = requests.post(url, headers=headers, params=parameters, timeout=5)
elif "PUT" in type:
if is_json:
response = requests.put(url, headers=headers, json=parameters, timeout=5)
else:
response = requests.put(url, headers=headers, params=parameters, timeout=5)
elif "DELETE" in type:
response = requests.delete(url, headers=headers, params=parameters, timeout=5)
else:
raise Exception("unsupported request method.")
result = json.loads(response.text)
app.logger.info("API response => %s", result)
return result
class SilentUndefined(Undefined):
"""
Class to suppress jinja2 errors and warnings
"""
def _fail_with_undefined_error(self, *args, **kwargs):
return 'undefined'
__add__ = __radd__ = __mul__ = __rmul__ = __div__ = __rdiv__ = \
__truediv__ = __rtruediv__ = __floordiv__ = __rfloordiv__ = \
__mod__ = __rmod__ = __pos__ = __neg__ = __call__ = \
__getitem__ = __lt__ = __le__ = __gt__ = __ge__ = __int__ = \
__float__ = __complex__ = __pow__ = __rpow__ = \
_fail_with_undefined_error