-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sender example for protobuf data
This example mirrors the existing HTTP sender example except that it adds a protobuf object as the payload instead of a JSON map. Signed-off-by: Kevin Conway <kevinconway@invisionapp.com>
- Loading branch information
1 parent
70febdc
commit ba2040d
Showing
5 changed files
with
206 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// | ||
// Copyright 2021 The CloudEvents Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"log" | ||
|
||
pbcloudevents "github.com/cloudevents/sdk-go/binding/format/protobuf/v2" | ||
cloudevents "github.com/cloudevents/sdk-go/v2" | ||
cehttp "github.com/cloudevents/sdk-go/v2/protocol/http" | ||
) | ||
|
||
func main() { | ||
ctx := cloudevents.ContextWithTarget(context.Background(), "http://localhost:8080/") | ||
|
||
p, err := cloudevents.NewHTTP() | ||
if err != nil { | ||
log.Fatalf("failed to create protocol: %s", err.Error()) | ||
} | ||
|
||
c, err := cloudevents.NewClient(p, cloudevents.WithTimeNow(), cloudevents.WithUUIDs()) | ||
if err != nil { | ||
log.Fatalf("failed to create client, %v", err) | ||
} | ||
|
||
for i := 0; i < 10; i++ { | ||
data := &Sample{Value: "sample"} | ||
e := cloudevents.NewEvent() | ||
e.SetType("com.cloudevents.sample.sent") | ||
e.SetSource("https://github.com/cloudevents/sdk-go/v2/samples/http/sender-protobuf") | ||
e.SetDataSchema(string(data.ProtoReflect().Descriptor().FullName())) | ||
_ = e.SetData(pbcloudevents.ContentTypeProtobuf, data) | ||
|
||
res := c.Send(ctx, e) | ||
if cloudevents.IsUndelivered(res) { | ||
log.Printf("Failed to send: %v", res) | ||
} else { | ||
var httpResult *cehttp.Result | ||
cloudevents.ResultAs(res, &httpResult) | ||
log.Printf("Sent %d with status code %d", i, httpResult.StatusCode) | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
syntax = "proto3"; | ||
|
||
package pb; | ||
|
||
option go_package = "/;main"; | ||
|
||
message Sample { string value = 1; } |