Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add EventResponse for send/receive. #42

Merged
merged 13 commits into from
Mar 8, 2019

Conversation

n3wscott
Copy link
Member

@n3wscott n3wscott commented Mar 5, 2019

This PR enables control over response codes for a transport (transport dependent). For HTTP this means letting the receiver control which HTTP status code is returned, as well as a response cloudevents.Event object. That response object will be passed through the client's defaulter chain if it the client is used to Send/Receive events ✨.

The signature of client.Receive has changed significantly, but in a followup PR I will reduce its complexity once again, and the README will not be wrong with the following:

func Receive(event cloudevents.Event) {
	// do something with event.Context and event.Data (via event.DataAs(foo)
}

Until then, I want to leave the README in a broken state, because the samples do work and after the next PR, the README will be correct as well.

Fixes: #31

Signed-off-by: Scott Nichols nicholss@google.com

Scott Nichols added 9 commits March 4, 2019 15:41
…alue on recieve.

Signed-off-by: Scott Nichols <nicholss@google.com>
Signed-off-by: Scott Nichols <nicholss@google.com>
Signed-off-by: Scott Nichols <nicholss@google.com>
Signed-off-by: Scott Nichols <nicholss@google.com>
Signed-off-by: Scott Nichols <nicholss@google.com>
Signed-off-by: Scott Nichols <nicholss@google.com>
Signed-off-by: Scott Nichols <nicholss@google.com>
Signed-off-by: Scott Nichols <nicholss@google.com>
Signed-off-by: Scott Nichols <nicholss@google.com>
@n3wscott
Copy link
Member Author

n3wscott commented Mar 6, 2019

Note: This is dependent on #45

Scott Nichols added 3 commits March 7, 2019 10:40
Signed-off-by: Scott Nichols <nicholss@google.com>
…the defaulter chain to the outbound message.

Signed-off-by: Scott Nichols <nicholss@google.com>
@n3wscott n3wscott changed the title [WIP] Enable error responses, responses for send/receive. Add EventResponse for send/receive. Mar 7, 2019
Signed-off-by: Scott Nichols <nicholss@google.com>
@n3wscott
Copy link
Member Author

n3wscott commented Mar 7, 2019

HTTP Receive Sample now outputs like:

----------------------------
Got Event Context: {CloudEventsVersion:0.1 EventID:de2128dc-3802-43e0-8cde-e8f5b6d7b7a4 EventTime:2019-03-07T21:31:07.350717Z EventType:com.cloudevents.sample.sent EventTypeVersion:<nil> SchemaURL: ContentType:0xc0001a2040 Source:{URL:{Scheme:https Opaque: User: Host:github.com Path:/cloudevents/sdk-go/cmd/samples/sender RawPath: ForceQuery:false RawQuery: Fragment:}} Extensions:map[]}
Got Data: &{Sequence:12 Message:Hello, Binary Encoding v0.1!}
Got Transport Context: {URI:/interesting/ Host:localhost:8080 Method:POST}
----------------------------

@n3wscott n3wscott marked this pull request as ready for review March 7, 2019 21:37
@n3wscott n3wscott requested a review from markpeek March 7, 2019 21:39
fmt.Printf("Got Event Context: %+v\n", event.Context)
data := &Example{}
if err := event.DataAs(data); err != nil {
fmt.Printf("Got Data Error: %s\n", err.Error())
}
fmt.Printf("Got Data: %+v\n", data)

fmt.Printf("Got Transport Context: %+v\n", http.TransportContextFrom(ctx))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

look at this @Harwayne ^^

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems to include the HTTP host, path, and method. Nifty.

cehttp.WithPort(env.Port),
cehttp.WithPath(env.Path),
client.WithUUIDs(),
client.WithTimeNow(),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is REALLY subtle and awesome. I am setting client options on the http client wrapper, and using those event defaulters to default values on the response event. 💯

// Receive is called from from the transport on event delivery.
func (c *ceClient) Receive(ctx context.Context, event cloudevents.Event, resp *cloudevents.EventResponse) error {
if c.receive != nil {
err := c.receive(ctx, event, resp)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

client.receive should be mappable to an event type or other interesting filtering. Look into this.

*resp.Event = fn(*resp.Event)
}
// Validate the event conforms to the CloudEvents Spec.
if err := resp.Event.Validate(); err != nil {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AND!!! outbound events are validated.

}
defer resp.Body.Close()

body, _ := ioutil.ReadAll(resp.Body)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is gathering the response and smashing it into an event if it can.

@markpeek markpeek merged commit 76d94e4 into cloudevents:master Mar 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants