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