feat: add uptime api command.
This commit is contained in:
parent
aacc06b84c
commit
75ad24fdbb
|
@ -3,7 +3,7 @@ import socket
|
|||
class Client:
|
||||
timeout = 5
|
||||
coding = 'utf-8'
|
||||
buffer_size = 1024
|
||||
buffer_size = 1
|
||||
|
||||
def __init__(self, host, port, string_control='\n\n'):
|
||||
self.host = host
|
||||
|
@ -16,7 +16,8 @@ class Client:
|
|||
self.socket.connect((self.host, self.port))
|
||||
|
||||
if expected_response:
|
||||
return self.getText(len(expected_response))
|
||||
buffer_size = len(expected_response) + len(self.string_control)
|
||||
return self.getText(buffer_size)
|
||||
|
||||
def getText(self, length=0):
|
||||
if length:
|
||||
|
@ -26,7 +27,8 @@ class Client:
|
|||
return self._getUnlimitedText()
|
||||
|
||||
def _getFixedText(self, length):
|
||||
response = self.socket.recv(length).decode(self.coding)
|
||||
buffer_size = length - len(self.string_control)
|
||||
response = self.socket.recv(buffer_size).decode(self.coding)
|
||||
self.socket.recv(len(self.string_control))
|
||||
return response
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
from FreeTwitch.exceptions import FreeSwitchConnectException, FreeSwitchAuthException
|
||||
from FreeTwitch.client import Client
|
||||
from FreeTwitch.response_metadata import ResponseMetadata
|
||||
|
||||
class FreeswitchClient:
|
||||
string_control = '\n\n'
|
||||
|
@ -23,5 +24,16 @@ class FreeswitchClient:
|
|||
if expected_response not in response:
|
||||
raise FreeSwitchAuthException(response)
|
||||
|
||||
def _apiCommand(self, api_command):
|
||||
command = "api"
|
||||
operation = " ".join([command, api_command])
|
||||
self.client.send(operation)
|
||||
instruction_response = self.client.getText()
|
||||
response_metadata = ResponseMetadata.from_string(instruction_response)
|
||||
return self.client.getText(response_metadata.length)
|
||||
|
||||
def uptime(self):
|
||||
return self._apiCommand('uptime')
|
||||
|
||||
def close(self):
|
||||
self.client.close()
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
class ResponseMetadata:
|
||||
|
||||
def __init__(self, _type, length):
|
||||
self._type = _type
|
||||
self.length = length
|
||||
|
||||
@classmethod
|
||||
def from_string(self, string_response):
|
||||
lines = string_response.split('\n')
|
||||
_type = lines[0].split(": ")[1]
|
||||
length = int(lines[1].split(": ")[1])
|
||||
return ResponseMetadata(_type, length)
|
||||
|
||||
|
|
@ -15,3 +15,6 @@ try:
|
|||
except:
|
||||
client.close()
|
||||
raise
|
||||
|
||||
|
||||
print(client.uptime())
|
||||
|
|
Loading…
Reference in New Issue