-
Notifications
You must be signed in to change notification settings - Fork 328
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 interceptors #276
Add interceptors #276
Conversation
@@ -28,7 +28,7 @@ test_python_client: generate build/clientcompat build/pycompat | |||
|
|||
setup: | |||
./install_proto.bash | |||
GOPATH=$(CURDIR)/_tools go install github.com/twitchtv/retool/... | |||
GO111MODULE=off GOPATH=$(CURDIR)/_tools GOBIN=$(CURDIR)/_tools/bin go get github.com/twitchtv/retool |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is needed for any development system that has GO111MODULE
and GOBIN
set, and has no effect on other systems for Twirp's Makefile
logic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice. It is looking good 👍
Looking forward to complete the implementation on the client side.
middleware.go
Outdated
|
||
// ChainInterceptors chains the Interceptors. | ||
// | ||
// Returns nil if interceptors is empty. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's remember to add more comments explaining Method
, Interceptor
and ChainInterceptors
. The extended comments should contain some basic example. We should also add a new page for interceptors in the docs, and make sure it is linked from docs for Hooks, and from other pages that mention hooks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a bit more Godoc, and a skeleton for the docs
folder - could you propose some more language you'd like to add here? Would be helpful.
@@ -574,6 +575,48 @@ func TestErroringHooks(t *testing.T) { | |||
}) | |||
} | |||
|
|||
func TestInterceptor(t *testing.T) { | |||
interceptor := func(next twirp.Method) twirp.Method { | |||
return func(ctx context.Context, request interface{}) (interface{}, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
verify that the context contains info about the request:
methodName, _ := twirp.MethodName(ctx)
if methodName != "MakeHat" {
return nil, fmt.Errorf("unexpected methodName: %q", methodName)
}
serviceName, _ := twirp.ServiceName(ctx)
if serviceName != "Haberdasher" {
return nil, fmt.Errorf("unexpected serviceName: %q", serviceName)
}
packageName, _ := twirp.PackageName(ctx)
if packageName != "twirp.internal.twirptest" {
return nil, fmt.Errorf("unexpected packageName: %q", packageName)
}
later, after calling the service, we should also verify that the context was annotated with the status code:
response, err := next(ctx, request)
status, ok := twirp.StatusCode(ctx)
if status != "200" || !ok {
return nil, fmt.Errorf("unexpected status in context: %q", status)
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added the first part, but the second part (status on context) isn't how contexts work - contexts are immutable, and we'd have to return a new context to do this (which isn't standard). I'd expect to get the status code off of the error, not the context, and all other middleware of this variety in the Golang ecosystem does the same.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good idea, thanks. Getting statusCode from context - doesn't work.
This is how I did:
twerr, ok := err.(twirp.Error)
if ok {
statusCode = twirp.ServerHTTPStatusFromErrorCode(twerr.Code())
}
Addressed comments, will start on client interceptors now. |
Update: the client interceptors are now added, including testing. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me. The initial documentation looks good, we can edit later.
Will merge in master with the release v7.1 soon. Great job! 👍 |
Implements #265