-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroutes.py
More file actions
81 lines (64 loc) · 2.87 KB
/
routes.py
File metadata and controls
81 lines (64 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""
Example route configuration demonstrating framework capabilities.
Shows pattern matching routes with JSON, form data, file streaming, and multipart
responses using the Axon API framework.
"""
import json
from urllib.parse import parse_qsl
def my_routes(environ, request_handler):
# Setup request context
method = environ['method']
path = [part for part in request_handler.path.split('/') if part]
# Log Route
request_handler.logger.info(f"Route: {method} /{'/'.join(path)}")
# Handle response
response = request_handler.response
match (method, path):
case ('GET', ['hello-world']):
# Return HTML
return response.file("examples/hello-world.html")
case ('GET', ['multipart', 'stream']):
files = [
'examples/files/file1.txt',
'examples/files/file2.txt',
'examples/files/file3.txt'
]
return response.stream(files)
case ('GET', ['api', 'query']):
raw_query_string = environ['query_string']
return response.json({"raw_query_string": raw_query_string})
case ('POST', ['api', 'json']):
try:
body_bytes = environ['wsgi_input'].read(environ['content_length'])
raw_json_body = body_bytes.decode('utf-8')
raw_parsed_data = json.loads(raw_json_body)
except (IOError, UnicodeDecodeError, json.JSONDecodeError):
raise
return response.json({"raw_parsed_data": raw_parsed_data})
case ('POST', ['api', 'form-data']):
try:
body_bytes = environ['wsgi_input'].read(environ['content_length'])
raw_form_data = body_bytes.decode('utf-8')
raw_form_data = parse_qsl(raw_form_data)
except (IOError, UnicodeDecodeError):
raise
return response.json({"raw_form_data": raw_form_data})
case ('GET', ['api', 'stream-files']):
# Return batched response for batched request
raw_query_string = environ['query_string']
if not raw_query_string:
return response.message("No files specified in query parameters", status_code=400)
# Parse query parameters to get file paths
query_params = dict(parse_qsl(raw_query_string))
# Extract all file paths from query parameters
files = list(query_params.values())
if not files:
return response.message("No valid file parameters found", status_code=400)
# Stream the requested files
return response.stream(files)
case ('GET', ['api', 'health']):
# Return JSON
return response.json({"status": "available"})
case _:
# Return message
return response.message("Not Found", status_code=404)