-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost.go
47 lines (37 loc) · 947 Bytes
/
post.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
package requestsio
import (
"bytes"
"context"
"github.com/apache/beam/sdks/v2/go/pkg/beam"
"github.com/apache/beam/sdks/v2/go/pkg/beam/log"
"github.com/apache/beam/sdks/v2/go/pkg/beam/register"
"github.com/carlmjohnson/requests"
)
func init() {
register.DoFn2x1[context.Context, any, []byte](&RequestsIO{})
}
type PubsubResultError struct {
MessageID string
ErrorMessage string
}
func Post(s beam.Scope, url string, targetData beam.PCollection) beam.PCollection {
s = s.Scope("arqbeam.requestsIO.Post")
return beam.ParDo(s, &RequestsIO{
URL: url,
}, targetData)
}
type RequestsIO struct {
URL string
}
func (fn *RequestsIO) ProcessElement(ctx context.Context, value any) []byte {
var stationResp bytes.Buffer
err := requests.
URL(fn.URL).
BodyJSON(value).
ToBytesBuffer(&stationResp).
Fetch(ctx)
if err != nil {
log.Errorf(ctx, "RequestIO process element failed: %v", err)
}
return stationResp.Bytes()
}