Commit 09a1904
Add Response.withEvents for SSE support (#4120)
## Summary
Add a public method to set Server-Sent Events on HTTP Response, enabling
endpoints to dynamically choose between JSON responses and SSE
streaming.
## Changes
- Add `Response.withEvents(Rx[ServerSentEvent])` method that:
- Sets the SSE events stream on the response
- Automatically sets `Content-Type: text/event-stream` if not already
set
- Preserves existing event-stream content type if already specified
- Add `HttpHeader.MediaType.TextEventStream` constant
- Add unit tests for the new functionality
## Use Case
This enables building API endpoints that can return either:
- A standard JSON response (non-streaming)
- An SSE stream (streaming)
Based on request parameters, for example:
```scala
def messages(request: MessagesRequest): Response = {
if (request.stream.getOrElse(false)) {
val events: Rx[ServerSentEvent] = streamingService.stream(request)
Http.response().withEvents(events)
} else {
val response = blockingService.complete(request)
Http.response().withJson(response.toJson)
}
}
```
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 72faff3 commit 09a1904
3 files changed
Lines changed: 50 additions & 0 deletions
File tree
- airframe-http
- .jvm/src/test/scala/wvlet/airframe/http
- src/main/scala/wvlet/airframe/http
Lines changed: 29 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
170 | 170 | | |
171 | 171 | | |
172 | 172 | | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
173 | 202 | | |
Lines changed: 1 addition & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
87 | 87 | | |
88 | 88 | | |
89 | 89 | | |
| 90 | + | |
90 | 91 | | |
91 | 92 | | |
Lines changed: 20 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
332 | 332 | | |
333 | 333 | | |
334 | 334 | | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
335 | 355 | | |
336 | 356 | | |
337 | 357 | | |
| |||
0 commit comments