Skip to content

Commit 13f441d

Browse files
feat: Add support for JsonL on test server (#26)
1 parent 78bd4fb commit 13f441d

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

cmd/server/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/speakeasy-api/speakeasy-api-test-service/internal/ecommerce"
1313
"github.com/speakeasy-api/speakeasy-api-test-service/internal/errors"
1414
"github.com/speakeasy-api/speakeasy-api-test-service/internal/eventstreams"
15+
"github.com/speakeasy-api/speakeasy-api-test-service/internal/jsonLines"
1516
"github.com/speakeasy-api/speakeasy-api-test-service/internal/method"
1617
"github.com/speakeasy-api/speakeasy-api-test-service/internal/middleware"
1718
"github.com/speakeasy-api/speakeasy-api-test-service/internal/pagination"
@@ -63,6 +64,8 @@ func main() {
6364
r.HandleFunc("/eventstreams/chat-chunked", eventstreams.HandleEventStreamChat).Methods(http.MethodPost)
6465
r.HandleFunc("/eventstreams/differentdataschemas", eventstreams.HandleEventStreamDifferentDataSchemas).Methods(http.MethodPost)
6566
r.HandleFunc("/eventstreams/differentdataschemas-flat", eventstreams.HandleEventStreamDifferentDataSchemasFlatten).Methods(http.MethodPost)
67+
r.HandleFunc("/jsonl", jsonLines.HandleJSONLinesRich).Methods(http.MethodGet)
68+
r.HandleFunc("/jsonl/chunks", jsonLines.HandleJSONLinesChunksRich).Methods(http.MethodGet)
6669
r.HandleFunc("/clientcredentials/token", clientcredentials.HandleTokenRequest).Methods(http.MethodPost)
6770
r.HandleFunc("/clientcredentials/authenticatedrequest", clientcredentials.HandleAuthenticatedRequest).Methods(http.MethodPost)
6871
r.HandleFunc("/clientcredentials/alt/token", clientcredentials.HandleTokenRequest).Methods(http.MethodPost)

internal/jsonLines/service.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package jsonLines
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"time"
7+
)
8+
9+
func pushEvents(rw http.ResponseWriter, events [][]string) {
10+
for _, event := range events {
11+
for _, line := range event {
12+
fmt.Fprint(rw, line)
13+
}
14+
15+
if f, ok := rw.(http.Flusher); ok {
16+
f.Flush()
17+
}
18+
19+
time.Sleep(100 * time.Millisecond)
20+
}
21+
}
22+
23+
func pushChunks(rw http.ResponseWriter, chunks []string) {
24+
for _, chunk := range chunks {
25+
fmt.Fprint(rw, chunk)
26+
27+
if f, ok := rw.(http.Flusher); ok {
28+
f.Flush()
29+
}
30+
31+
time.Sleep(100 * time.Millisecond)
32+
}
33+
}
34+
35+
func HandleJSONLinesChunksRich(rw http.ResponseWriter, _ *http.Request) {
36+
rw.Header().Add("Content-Type", "application/jsonl")
37+
38+
pushChunks(rw, []string{
39+
"{\"name\": \"Peter\", \"skills\": [\"Go\"",
40+
", \"Python\"]}\n{\"name\": \"John\"",
41+
", \"skills\": [\"Go\", \"Rust\"]}\n",
42+
})
43+
}
44+
45+
func HandleJSONLinesRich(rw http.ResponseWriter, _ *http.Request) {
46+
rw.Header().Add("Content-Type", "application/jsonl")
47+
48+
pushEvents(rw, [][]string{
49+
{
50+
"{\"name\": \"Peter\", \"skills\": [\"Go\", \"Python\"]}\n",
51+
},
52+
{
53+
"{\"name\": \"John\", \"skills\": [\"Go\", \"Rust\"]}\n",
54+
},
55+
})
56+
}

0 commit comments

Comments
 (0)