@@ -1084,132 +1084,114 @@ def create_item(request, body: CreateItemBody, query: CreateItemQueryParamsParam
1084
1084
1085
1085
# --- Streaming responses ---
1086
1086
1087
+
1087
1088
@app .get ("/stream/sync" )
1088
1089
def sync_stream ():
1089
1090
def number_generator ():
1090
1091
for i in range (5 ):
1091
1092
yield f"Chunk { i } \n " .encode ()
1092
-
1093
- return Response (
1094
- status_code = 200 ,
1095
- headers = {"Content-Type" : "text/plain" },
1096
- description = number_generator ()
1097
- )
1093
+
1094
+ return Response (status_code = 200 , headers = {"Content-Type" : "text/plain" }, description = number_generator ())
1095
+
1098
1096
1099
1097
@app .get ("/stream/async" )
1100
1098
async def async_stream ():
1101
1099
async def async_generator ():
1102
1100
import asyncio
1101
+
1103
1102
for i in range (5 ):
1104
1103
await asyncio .sleep (1 ) # Simulate async work
1105
1104
yield f"Async Chunk { i } \n " .encode ()
1106
-
1107
- return Response (
1108
- status_code = 200 ,
1109
- headers = {"Content-Type" : "text/plain" },
1110
- description = async_generator ()
1111
- )
1105
+
1106
+ return Response (status_code = 200 , headers = {"Content-Type" : "text/plain" }, description = async_generator ())
1107
+
1112
1108
1113
1109
@app .get ("/stream/mixed" )
1114
1110
async def mixed_stream ():
1115
1111
async def mixed_generator ():
1116
1112
import asyncio
1113
+
1117
1114
# Binary data
1118
1115
yield b"Binary chunk\n "
1119
1116
await asyncio .sleep (0.5 )
1120
-
1117
+
1121
1118
# String data
1122
1119
yield "String chunk\n " .encode ()
1123
1120
await asyncio .sleep (0.5 )
1124
-
1121
+
1125
1122
# Integer data
1126
1123
yield str (42 ).encode () + b"\n "
1127
1124
await asyncio .sleep (0.5 )
1128
-
1125
+
1129
1126
# JSON data
1130
1127
import json
1128
+
1131
1129
data = {"message" : "JSON chunk" , "number" : 123 }
1132
1130
yield json .dumps (data ).encode () + b"\n "
1133
-
1134
- return Response (
1135
- status_code = 200 ,
1136
- headers = {"Content-Type" : "text/plain" },
1137
- description = mixed_generator ()
1138
- )
1131
+
1132
+ return Response (status_code = 200 , headers = {"Content-Type" : "text/plain" }, description = mixed_generator ())
1133
+
1139
1134
1140
1135
@app .get ("/stream/events" )
1141
1136
async def server_sent_events ():
1142
1137
async def event_generator ():
1143
1138
import asyncio
1144
1139
import json
1145
1140
import time
1146
-
1141
+
1147
1142
# Regular event
1148
1143
yield f"event: message\n data: { json .dumps ({'time' : time .time (), 'type' : 'start' })} \n \n " .encode ()
1149
1144
await asyncio .sleep (1 )
1150
-
1145
+
1151
1146
# Event with ID
1152
1147
yield f"id: 1\n event: update\n data: { json .dumps ({'progress' : 50 })} \n \n " .encode ()
1153
1148
await asyncio .sleep (1 )
1154
-
1149
+
1155
1150
# Multiple data lines
1156
- data = json .dumps ({' status' : ' complete' , ' results' : [1 , 2 , 3 ]}, indent = 2 )
1151
+ data = json .dumps ({" status" : " complete" , " results" : [1 , 2 , 3 ]}, indent = 2 )
1157
1152
yield f"event: complete\n data: { data } \n \n " .encode ()
1158
-
1153
+
1159
1154
return Response (
1160
- status_code = 200 ,
1161
- headers = {
1162
- "Content-Type" : "text/event-stream" ,
1163
- "Cache-Control" : "no-cache" ,
1164
- "Connection" : "keep-alive"
1165
- },
1166
- description = event_generator ()
1155
+ status_code = 200 , headers = {"Content-Type" : "text/event-stream" , "Cache-Control" : "no-cache" , "Connection" : "keep-alive" }, description = event_generator ()
1167
1156
)
1168
1157
1158
+
1169
1159
@app .get ("/stream/large-file" )
1170
1160
async def stream_large_file ():
1171
1161
async def file_generator ():
1172
1162
# Simulate streaming a large file in chunks
1173
1163
chunk_size = 1024 # 1KB chunks
1174
1164
total_size = 10 * chunk_size # 10KB total
1175
-
1165
+
1176
1166
for offset in range (0 , total_size , chunk_size ):
1177
1167
# Simulate reading file chunk
1178
1168
chunk = b"X" * min (chunk_size , total_size - offset )
1179
1169
yield chunk
1180
-
1170
+
1181
1171
return Response (
1182
1172
status_code = 200 ,
1183
- headers = {
1184
- "Content-Type" : "application/octet-stream" ,
1185
- "Content-Disposition" : "attachment; filename=large-file.bin"
1186
- },
1187
- description = file_generator ()
1173
+ headers = {"Content-Type" : "application/octet-stream" , "Content-Disposition" : "attachment; filename=large-file.bin" },
1174
+ description = file_generator (),
1188
1175
)
1189
1176
1177
+
1190
1178
@app .get ("/stream/csv" )
1191
1179
async def stream_csv ():
1192
1180
async def csv_generator ():
1193
1181
# CSV header
1194
1182
yield "id,name,value\n " .encode ()
1195
-
1183
+
1196
1184
import asyncio
1197
1185
import random
1198
-
1186
+
1199
1187
# Generate rows
1200
1188
for i in range (5 ):
1201
1189
await asyncio .sleep (0.5 ) # Simulate data processing
1202
1190
row = f"{ i } ,item-{ i } ,{ random .randint (1 , 100 )} \n "
1203
1191
yield row .encode ()
1204
-
1205
- return Response (
1206
- status_code = 200 ,
1207
- headers = {
1208
- "Content-Type" : "text/csv" ,
1209
- "Content-Disposition" : "attachment; filename=data.csv"
1210
- },
1211
- description = csv_generator ()
1212
- )
1192
+
1193
+ return Response (status_code = 200 , headers = {"Content-Type" : "text/csv" , "Content-Disposition" : "attachment; filename=data.csv" }, description = csv_generator ())
1194
+
1213
1195
1214
1196
def main ():
1215
1197
app .set_response_header ("server" , "robyn" )
0 commit comments