Add example for HTTP hooks using Python

This commit is contained in:
Marius 2021-11-29 14:32:17 +01:00
parent 273a47db7f
commit 7e42f26b46
1 changed files with 48 additions and 0 deletions

View File

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