add async capabilities to server

This commit is contained in:
adamoutler 2023-02-13 23:33:25 +00:00
parent a6469184fb
commit 2752702d17

View File

@ -2,6 +2,9 @@
This server will communicate with OpenAI to generate responses to the client."""
import json
import random
import threading
import requests
import merge
from http.server import BaseHTTPRequestHandler, HTTPServer
import idgaf
@ -10,7 +13,7 @@ import security
from const import UTF8
default_request_body = b'{"message":{"command":"aidgaf","data":{"username":"AdamOutler"}}}'
request_headers = {}
class IDGAFServer(BaseHTTPRequestHandler):
""" This class handles the requests from the client. """
@ -37,12 +40,67 @@ class IDGAFServer(BaseHTTPRequestHandler):
command: The command object received from the client. """
print(command)
if command['message']['command'] == 'aidgaf':
self.idgaf_command_handler(command)
def idgaf_command_handler(self, command):
""" This function handles the idgaf command. It will generate a
response and send it to the client.
Parameters:
command: The command object received from the client. """
if not settings.ASYNC_URL:
[code,body]=self.getGPTResponse(command)
self.handle_response(code,body)
return
thread = threading.Thread(target=self.send_async_request, args=(command,))
thread.start()
self.handle_response(200,json.dumps(command))
def send_async_request(self,command):
""" This function sends the request to the async URL. It will generate a
response and send it to the client. This function is called in a separate
thread. It will not block the main thread.
Parameters:
command: The command object received from the client. """
[code,body] = self.getGPTResponse(command)
merged=merge.merge_dict_no_overwrite(json.loads(body),command)
merged['hash']=security.get_message_hash(json.dumps(merged))
merged['service']='JavaShark'
data=json.dumps(merged)
print(str(code)+": "+data)
if (code != 200):
return
if (settings.ASYNC_METHOD == "POST"):
requests.post(settings.ASYNC_URL, data=data, headers=request_headers)
elif (settings.ASYNC_METHOD == "PUT"):
requests.put(settings.ASYNC_URL, data=data, headers=request_headers)
elif (settings.ASYNC_METHOD == "PATCH"):
resp=requests.patch(settings.ASYNC_URL, data=data, headers=request_headers)
print(str(resp)+resp.text)
# gpt_response = requests.post(URL, json=data, headers=request_headers)
def getGPTResponse(self, command) -> [int, str]:
""" This function generates a response using OpenAI. It will return
the response code and the response body.
Parameters:
command: The command object received from the client.
Returns:
The response code and the response body. """
[responseCode, json_response] = idgaf.parse_idgaf_request(command)
json_response['hash'] = security.get_message_hash(json.dumps(command))
response_body = json.dumps(json_response)
self.handle_response(responseCode, response_body)
return [responseCode, response_body]
def get_body(self)->bytes:
def get_body(self) -> bytes:
""" This function returns the body of the request.
Returns:
The body of the request.
@ -56,7 +114,6 @@ class IDGAFServer(BaseHTTPRequestHandler):
print(request_body)
return request_body
def handle_response(self, code, body):
""" This function handles the response to the client.
Parameters:
@ -68,7 +125,8 @@ class IDGAFServer(BaseHTTPRequestHandler):
print("sending:"+body)
self.wfile.write(bytes(body, UTF8))
def perform_sanity_checks(str_request)->bool:
def perform_sanity_checks(str_request) -> bool:
""" Performs a hash check on the message, and verifies the timestamp is valid.
If either check fails, return False. Otherwise, return True.
Parameters:
@ -86,8 +144,6 @@ def perform_sanity_checks(str_request)->bool:
return True
def main():
""" This function starts the server. """
webServer = HTTPServer(