feat: add uptime api command.

This commit is contained in:
Francisco 2023-07-08 12:08:29 -05:00
parent aacc06b84c
commit 75ad24fdbb
5 changed files with 41 additions and 3 deletions

View File

@ -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

View File

@ -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()

View File

@ -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)

View File

@ -15,3 +15,6 @@ try:
except:
client.close()
raise
print(client.uptime())

7
test.py Normal file
View File

@ -0,0 +1,7 @@
from FreeTwitch.response_metadata import ResponseMetadata
response = """Content-Type: api/response
Content-Length: 28"""
response_metadata = ResponseMetadata.from_string(response)
assert response_metadata.length == 28