-
Notifications
You must be signed in to change notification settings - Fork 0
/
cuc.py
148 lines (139 loc) · 5.76 KB
/
cuc.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from io import StringIO
import json
from pprint import pprint
from requests_toolbelt.utils import dump
from xmlrpc.client import Fault
from requests import Request,Session
from requests.auth import HTTPBasicAuth
import requests
import urllib3
# from requests.packages.urllib3.exceptions import InsecureRequestWarning
class CUC:
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# headers = {'Content-type': 'content_type_value'}
def __init__(self,username,password,cuc) -> None:
self.username=username
self.password=password
self.cuc=cuc
def Add_User(self,UserId,Extension,Template):
headers = {'Accept':'application/json',"Connection": "keep-alive"}
req={ "Alias": UserId,"DtmfAccessId": Extension}
adminSession = Session()
params = { 'templateAlias': 'voicemailusertemplate' }
if Template=="":
params = { 'templateAlias': 'voicemailusertemplate' }
else:
params = { 'templateAlias': Template }
resp = requests.post(
f'https://{self.cuc}/vmrest/users',
auth=HTTPBasicAuth( f"{self.username}", f"{self.password}" ),
json = req,
params=params,
verify = False
) # tree=ElementTree.parse(response.content)
data=resp
try:
return data
except Fault as e:
return e
# data=responsejson
def List_User(self,UserId,Extension):
headers = {"Content-Type": "application/xml",'Accept':'application/json',"charset":"UTF-8","Connection": "keep-alive"}
req={ "Alias": UserId,"DtmfAccessId": Extension}
adminSession = requests.Session()
params = { 'templateAlias': 'voicemailusertemplate' }
resp = adminSession.get(
f'https://{self.cuc}/vmrest/users?templateAlias=voicemailusertemplate',
auth=HTTPBasicAuth( f"{self.username}", f"{self.password}" ),
headers=headers,
verify = False
)
data=resp.json()
try:
return data
except Fault as e:
return e
def Get_User(self,UserId):
headers = {"Content-Type": "application/xml",'Accept':'application/json',"charset":"UTF-8","Connection": "keep-alive"}
# req={ "Alias": UserId,"DtmfAccessId": Extension}
adminSession = requests.Session()
# params = { 'templateAlias': 'voicemailusertemplate' }
resp = adminSession.get(
f'https://{self.cuc}/vmrest/users?query=(alias%20is%20{UserId})',
auth=HTTPBasicAuth( f"{self.username}", f"{self.password}" ),
headers=headers,
verify = False
)
data=resp.json()
try:
return data
except Fault as e:
return e
def Delete_User(self,UserId):
headers = {"Content-Type": "application/xml",'Accept':'application/json',"charset":"UTF-8","Connection": "keep-alive"}
# req={ "Alias": UserId,"DtmfAccessId": Extension}
adminSession = requests.Session()
res = adminSession.get(
f'https://{self.cuc}/vmrest/users?query=(alias%20is%20{UserId})',
auth=HTTPBasicAuth( f"{self.username}", f"{self.password}" ),
headers=headers,
verify = False
)
dat=res.json()
try:
objectid=dat["User"]["ObjectId"]
except:
return dat
# params = { 'templateAlias': 'voicemailusertemplate' }
DeleteAction = adminSession.delete(
f'https://{self.cuc}/vmrest/users/{objectid}',
auth=HTTPBasicAuth( f"{self.username}", f"{self.password}" ),
headers=headers,
verify = False
)
data=DeleteAction
try:
return data
except Fault as e:
return e
def Get_Templates(self):
headers = {'Accept':'application/json',"charset":"UTF-8","Connection": "keep-alive"}
adminSession = requests.Session()
params = { 'templateAlias': 'voicemailusertemplate' }
resp = adminSession.get(
f'https://{self.cuc}/vmrest/usertemplates',
auth=HTTPBasicAuth( f"{self.username}", f"{self.password}" ),
headers=headers,
verify = False
)
data=resp.json()
try:
return data
except Fault as e:
return e
def Get_User_Extensions(self,UserId):
headers = {"Content-Type": "application/xml",'Accept':'application/json',"charset":"UTF-8","Connection": "keep-alive"}
# req={ "Alias": UserId,"DtmfAccessId": Extension}
adminSession = requests.Session()
res = adminSession.get(
f'https://{self.cuc}/vmrest/users?query=(alias%20is%20{UserId})',
auth=HTTPBasicAuth( f"{self.username}", f"{self.password}" ),
headers=headers,
verify = False
)
dat=res.json()
objectid=dat["User"]["ObjectId"]
# print(objectid)
GetUserExts = adminSession.get(
f'https://{self.cuc}/vmrest/users/{objectid}/alternateextensions',
auth=HTTPBasicAuth( f"{self.username}", f"{self.password}" ),
headers=headers,
verify = False
)
data=GetUserExts.json()
try:
return data
except Fault as e:
return e
# for Alias in data["User"]:
# print(Alias["Alias"])