From 7e42f26b4649b4f044366dd17159b3a40395494f Mon Sep 17 00:00:00 2001 From: Marius Date: Mon, 29 Nov 2021 14:32:17 +0100 Subject: [PATCH] Add example for HTTP hooks using Python --- examples/hooks/http/server.py | 48 +++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 examples/hooks/http/server.py diff --git a/examples/hooks/http/server.py b/examples/hooks/http/server.py new file mode 100644 index 0000000..460fd87 --- /dev/null +++ b/examples/hooks/http/server.py @@ -0,0 +1,48 @@ +from http.server import HTTPServer, BaseHTTPRequestHandler + +from io import BytesIO + +import json + + +class SimpleHTTPRequestHandler(BaseHTTPRequestHandler): + + def do_GET(self): + self.send_response(200) + self.end_headers() + self.wfile.write(b'Hello, world!') + + def do_POST(self): + content_length = int(self.headers['Content-Length']) + request_body = self.rfile.read(content_length) + + hook_request = json.loads(request_body) + print(hook_request) + + hook_response = { + 'HTTPResponse': { + 'Headers': {} + } + } + + if hook_request['Type'] == 'pre-create': + hook_response['HTTPResponse']['Headers']['X-From-Pre-Create'] = 'hello' + + hook_response['RejectUpload'] = True + + + if hook_request['Type'] == 'pre-finish': + hook_response['HTTPResponse']['StatusCode'] = 200 + hook_response['HTTPResponse']['Headers']['X-From-Pre-Finish'] = 'hello again' + hook_response['HTTPResponse']['Body'] = 'some information' + + response_body = json.dumps(hook_response) + print(response_body) + + self.send_response(200) + self.end_headers() + self.wfile.write(response_body.encode()) + + +httpd = HTTPServer(('localhost', 8000), SimpleHTTPRequestHandler) +httpd.serve_forever()