mirror of https://git.48k.eu/ogclient
Add ogRest class to generate API Rest Responses
This class now, allow us to generate API Rest responses. In the future, the idea is to complete this class with get and post messages if it's needed.more_events
parent
7548870a92
commit
694bc492a2
|
@ -12,6 +12,7 @@ from enum import Enum
|
|||
class State(Enum):
|
||||
CONNECTING = 0
|
||||
RECEIVING = 1
|
||||
FORCE_DISCONNECTED = 2
|
||||
|
||||
class ogClient:
|
||||
def __init__(self, ip, port):
|
||||
|
@ -84,10 +85,8 @@ class ogClient:
|
|||
|
||||
if self.trailer and len(self.data) >= self.content_len:
|
||||
httpparser.parser(self.data)
|
||||
if not ogprocess.processOperation(httpparser.getRequestOP(), httpparser.getURI()):
|
||||
self.sock.send(bytes('HTTP/1.0 400 Bad request\r\n\r\n', 'utf-8'))
|
||||
else:
|
||||
self.sock.send(bytes('HTTP/1.0 200 OK\r\n\r\n', 'utf-8'))
|
||||
if not ogprocess.processOperation(httpparser.getRequestOP(), httpparser.getURI(), self.sock):
|
||||
self.state = State.FORCE_DISCONNECTED
|
||||
|
||||
# Cleanup state information from request
|
||||
self.data = ""
|
||||
|
@ -102,6 +101,8 @@ class ogClient:
|
|||
if state == State.CONNECTING:
|
||||
readset = [ sock ]
|
||||
writeset = [ sock ]
|
||||
elif state == State.FORCE_DISCONNECTED:
|
||||
return 0
|
||||
else:
|
||||
readset = [ sock ]
|
||||
writeset = [ ]
|
||||
|
|
|
@ -1,22 +1,29 @@
|
|||
import threading
|
||||
import platform
|
||||
import time
|
||||
from src import ogRest
|
||||
|
||||
if platform.system() == 'Linux':
|
||||
from src.linux import ogOperations
|
||||
|
||||
class ogProcess():
|
||||
def processOperation(self, op, URI):
|
||||
def processOperation(self, op, URI, sock):
|
||||
if ("poweroff" in URI):
|
||||
sock.send(bytes(ogRest.getResponse(ogRest.ogResponses.IN_PROGRESS), 'utf-8'))
|
||||
sock.close()
|
||||
self.process_poweroff()
|
||||
return 1
|
||||
return 0
|
||||
elif ("reboot" in URI):
|
||||
sock.send(bytes(ogRest.getResponse(ogRest.ogResponses.IN_PROGRESS), 'utf-8'))
|
||||
sock.close()
|
||||
self.process_reboot()
|
||||
return 1
|
||||
return 0
|
||||
elif ("probe" in URI):
|
||||
return 1
|
||||
sock.send(bytes(ogRest.getResponse(ogRest.ogResponses.OK), 'utf-8'))
|
||||
else:
|
||||
sock.send(bytes(ogRest.getResponse(ogRest.ogResponses.BAD_REQUEST), 'utf-8'))
|
||||
|
||||
return 0
|
||||
return 1
|
||||
|
||||
def process_reboot(self):
|
||||
# Rebooting thread
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
from enum import Enum
|
||||
|
||||
class ogResponses(Enum):
|
||||
BAD_REQUEST=0
|
||||
IN_PROGRESS=1
|
||||
OK=2
|
||||
|
||||
def getResponse(response):
|
||||
if response == ogResponses.BAD_REQUEST:
|
||||
return 'HTTP/1.0 400 Bad request\r\n\r\n'
|
||||
if response == ogResponses.IN_PROGRESS:
|
||||
return 'HTTP/1.0 202 Accepted\r\n\r\n'
|
||||
if response == ogResponses.OK:
|
||||
return 'HTTP/1.0 200 OK\r\n\r\n'
|
Loading…
Reference in New Issue