-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEndpointAPI_Example_Python.py
More file actions
105 lines (83 loc) · 3.24 KB
/
Copy pathEndpointAPI_Example_Python.py
File metadata and controls
105 lines (83 loc) · 3.24 KB
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
import requests
import hashlib
import hmac
import binascii
import time
headers = {'Content-type': 'application/json'}
'''
Tested in Python 2.7 and Python 3.4
You might need to pip install requests
'''
# local IP address of your StarLeaf room system
endpoint = "192.168.1.10"
# ECAPI password or key of your StarLeaf room system, as configured in the portal (for Cloud endpoints)
# or Web UI / Maestro (for GTm)
password = "2345"
testcall = "demo@starleaf.com"
'''
Use this API for StarLeaf Cloud endpoints i.e GT Mini 3330 / GT 3351
This connects to port 23456 of http://<local IP address of endpoint>
'''
api = ":23456"
'''
Use this API for StarLeaf GTm (Skype for Business) endpoints 5250 and 5140
GTm uses port 80 for ECAPI requests
'''
# api = "/ecapi"
class StarLeafECAPIClient(object):
def __init__(self, endpoint, password, api):
self.endpoint = endpoint
self.password = password
self.api = api
self.key = None
self.authenticated = False
self.session = requests.Session()
def _get(self, path, params=None):
r = self.session.get('http://' + self.endpoint + api + path, params=params)
return r
def _apiauthentication(self, salt_hex, iterations, challenge):
if self.key is None:
salt = binascii.unhexlify(salt_hex)
self.key = hashlib.pbkdf2_hmac('sha256', str(self.password).encode(), salt, iterations)
_hash = hmac.new(self.key, str(challenge).encode(), hashlib.sha256)
response = _hash.hexdigest()
return response
def authenticate(self):
response = self._get('/auth')
body = response.json()
status = response.status_code
if status == 200:
authresponse = self._apiauthentication(body['salt'],
body['iterations'],
body['challenge'])
params = {'challenge': body['challenge'], 'response': authresponse}
authresult = self._get('/auth', params=params)
resultbody = authresult.json()
if resultbody['authenticated'] is True:
self.authenticated = True
return True
return False
def call(self, address):
params = {'action': 'dial', 'number': address}
result = self._get('/action', params=params)
return result.status_code
def start_recording(self):
params = {'action': 'start_recording'}
result = self._get('/action', params=params)
return result.status_code
def stop_recording(self):
params = {'action': 'stop_recording'}
result = self._get('/action', params=params)
return result.status_code
myClient = StarLeafECAPIClient(endpoint, password, api)
authStatus = myClient.authenticate()
if authStatus is True:
# place a test call to StarLeaf Demo Line
print("Authenticated successfully. Placing a test call to " + testcall)
myClient.call(testcall)
time.sleep(10)
print ("Rec start status code " + str(myClient.start_recording()))
time.sleep(10)
print ("Rec stop status code " + str(myClient.stop_recording()))
else:
print ("Sorry - authentication failed. Check the endpoint's ECAPI settings and password or key")