-
-
Save hyperbolix/b66f07d5d007ca2df321550241d748ac to your computer and use it in GitHub Desktop.
A python3 HTTP server compatible with Apache Drill HTTP Storage Plugin that demonstrates that the 'INDEX' pagination method does not work when the 'has_next' and 'next_page' fields come after the 'results' data path field.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
from socketserver import ThreadingMixIn | |
from http.server import BaseHTTPRequestHandler, HTTPServer | |
import json | |
from urllib.parse import urlparse | |
from urllib.parse import parse_qs | |
import threading | |
hostName = "localhost" | |
serverPort = 8081 | |
class ThreadingSimpleServer(ThreadingMixIn, HTTPServer): | |
pass | |
class MyServer(BaseHTTPRequestHandler): | |
def do_GET(self): | |
self.send_response(200) | |
self.send_header("Content-type", "application/json") | |
self.end_headers() | |
parsed_url = urlparse('http://localhost' + self.path).query | |
param_map = parse_qs(parsed_url) | |
total_results = int(param_map.get('total_results_val')[0]) | |
page_size = int(param_map.get('page_size_val')[0]) | |
page_token = int(param_map.get('next_page')[0][40:]) if 'next_page' in param_map else 0 | |
print("got parameters: " + str(total_results) + " and " + str(page_size) + " and " + str(page_token)) | |
result_json = {} | |
result_list = [] | |
i = page_token | |
while i < total_results and i < page_token+page_size: | |
result_row = {} | |
result_row['foo'] = i | |
result_row["foo_id"] = "ca4b3480-4c27-4bcc-8aa5-a046acb0c7c8" | |
result_row["bar_id"] = "57c2cd6b-f483-4aee-b536-5b2922284d92" | |
result_row["foo_name"] = "jkljkljkljkljkl" | |
result_row["bar_name"] = "asdfasdf" | |
result_row["some_integer"] = 1662758489 | |
result_row["some_double"] = 2.2449264978773344 | |
result_list.append(result_row) | |
i = i + 1 | |
result_json['results'] = result_list | |
# if this block is pulled up to line 49, before results, pagination will work. | |
if i < total_results: | |
result_json['has_more'] = True | |
result_json['next_page'] = 'asdf'*10 + str(i) | |
else: | |
result_json['has_more'] = False | |
result_json['next_page'] = None | |
print("output: " + json.dumps(result_json)) | |
self.wfile.write(bytes(json.dumps(result_json), 'utf-8')) | |
if __name__ == "__main__": | |
webServer = ThreadingSimpleServer((hostName, serverPort), MyServer) | |
print("Server started http://%s:%s" % (hostName, serverPort)) | |
try: | |
webServer.serve_forever() | |
except KeyboardInterrupt: | |
pass | |
webServer.server_close() | |
print("Server stopped.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment