From 2752702d17964a091cb1d34df8798d0c96dbf2cb Mon Sep 17 00:00:00 2001 From: adamoutler Date: Mon, 13 Feb 2023 23:33:25 +0000 Subject: [PATCH] add async capabilities to server --- src/aidgaf/aidgaf-server/server.py | 84 +++++++++++++++++++++++++----- 1 file changed, 70 insertions(+), 14 deletions(-) diff --git a/src/aidgaf/aidgaf-server/server.py b/src/aidgaf/aidgaf-server/server.py index 05b31bf..afdac23 100644 --- a/src/aidgaf/aidgaf-server/server.py +++ b/src/aidgaf/aidgaf-server/server.py @@ -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,11 +13,11 @@ 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. """ - + def do_GET(self): """ This function handles GET requests. """ self.send_response(418) @@ -37,12 +40,67 @@ class IDGAFServer(BaseHTTPRequestHandler): command: The command object received from the client. """ print(command) if command['message']['command'] == 'aidgaf': - [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) + 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 - def get_body(self)->bytes: + 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) + return [responseCode, response_body] + + def get_body(self) -> bytes: """ This function returns the body of the request. Returns: The body of the request. @@ -55,8 +113,7 @@ class IDGAFServer(BaseHTTPRequestHandler): str_request = str(request_body, UTF8) print(request_body) return request_body - - + def handle_response(self, code, body): """ This function handles the response to the client. Parameters: @@ -67,14 +124,15 @@ class IDGAFServer(BaseHTTPRequestHandler): self.end_headers() 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: str_request: The request body as a string. Returns: - True if the message is valid, False otherwise. """ + True if the message is valid, False otherwise. """ if settings.HASHKEY is not None: hash = security.get_message_hash(str_request) if hash not in str_request: @@ -86,8 +144,6 @@ def perform_sanity_checks(str_request)->bool: return True - - def main(): """ This function starts the server. """ webServer = HTTPServer(