-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
124 lines (100 loc) Β· 2.9 KB
/
main.go
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"bytes"
"context"
"fmt"
"net/http"
"time"
"github.com/sonirico/withttp"
)
type metric struct {
Time time.Time `json:"t"`
Temp float32 `json:"T"`
}
func CreateStream() error {
points := []metric{
{
Time: time.Unix(time.Now().Unix()-1, 0),
Temp: 39,
},
{
Time: time.Now(),
Temp: 40,
},
}
stream := withttp.Slice[metric](points)
testEndpoint := withttp.NewEndpoint("webhook-site-request-stream-example").
Request(
withttp.WithBaseURL("https://webhook.site/24e84e8f-75cf-4239-828e-8bed244c0afb"),
)
call := withttp.NewCall[any](withttp.WithFasthttp()).
WithMethod(http.MethodPost).
WithContentType(withttp.ContentTypeJSONEachRow).
WithRequestSniffed(func(data []byte, err error) {
fmt.Printf("recv: '%s', err: %v", string(data), err)
}).
WithRequestStreamBody(
withttp.WithRequestStreamBody[any, metric](stream),
).
WithExpectedStatusCodes(http.StatusOK)
return call.CallEndpoint(context.Background(), testEndpoint)
}
func CreateStreamChannel() error {
points := make(chan metric, 2)
go func() {
points <- metric{
Time: time.Unix(time.Now().Unix()-1, 0),
Temp: 39,
}
points <- metric{
Time: time.Now(),
Temp: 40,
}
close(points)
}()
stream := withttp.Channel[metric](points)
testEndpoint := withttp.NewEndpoint("webhook-site-request-stream-example").
Request(
withttp.WithBaseURL("https://webhook.site/24e84e8f-75cf-4239-828e-8bed244c0afb"),
)
call := withttp.NewCall[any](withttp.WithFasthttp()).
WithMethod(http.MethodPost).
WithContentType(withttp.ContentTypeJSONEachRow).
WithRequestSniffed(func(data []byte, err error) {
fmt.Printf("recv: '%s', err: %v", string(data), err)
}).
WithRequestStreamBody(
withttp.WithRequestStreamBody[any, metric](stream),
).
WithExpectedStatusCodes(http.StatusOK)
return call.CallEndpoint(context.Background(), testEndpoint)
}
func CreateStreamReader() error {
buf := bytes.NewBuffer(nil)
go func() {
buf.WriteString("{\"t\":\"2022-09-01T00:58:15+02:00\"")
buf.WriteString(",\"T\":39}\n{\"t\":\"2022-09-01T00:59:15+02:00\",\"T\":40}\n")
}()
streamFactory := withttp.NewProxyStreamFactory(1 << 10)
stream := withttp.NewStreamFromReader(buf, streamFactory)
testEndpoint := withttp.NewEndpoint("webhook-site-request-stream-example").
Request(
withttp.WithBaseURL("https://webhook.site/24e84e8f-75cf-4239-828e-8bed244c0afb"),
)
call := withttp.NewCall[any](withttp.WithNetHttp()).
WithMethod(http.MethodPost).
WithRequestSniffed(func(data []byte, err error) {
fmt.Printf("recv: '%s', err: %v", string(data), err)
}).
WithContentType(withttp.ContentTypeJSONEachRow).
WithRequestStreamBody(
withttp.WithRequestStreamBody[any, []byte](stream),
).
WithExpectedStatusCodes(http.StatusOK)
return call.CallEndpoint(context.Background(), testEndpoint)
}
func main() {
_ = CreateStream()
_ = CreateStreamChannel()
_ = CreateStreamReader()
}