Skip to content

Commit

Permalink
Add client interceptors
Browse files Browse the repository at this point in the history
  • Loading branch information
bufdev committed Sep 18, 2020
1 parent 2688b1f commit ae97c56
Show file tree
Hide file tree
Showing 22 changed files with 190 additions and 126 deletions.
24 changes: 12 additions & 12 deletions clientcompat/internal/clientcompat/clientcompat.twirp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions docs/interceptors.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ title: "Interceptors"
sidebar_label: "Interceptors"
---

The service constructor can use the option `twirp.WithServerInterceptors(interceptors ...twirp.Interceptor)`
The client and service constructors can use the options
`twirp.WithClientInterceptors(interceptors ...twirp.Interceptor)`
and `twirp.WithServerInterceptors(interceptors ...twirp.Interceptor)`
to plug in additional functionality:

```go
server := NewHaberdasherServer(svcImpl, twirp.WithInterceptor(NewLogInterceptor(logger.New(os.Stderr, "", 0))))
client := NewHaberdasherProtobufClient(url, &http.Client{}, twirp.WithClientInterceptors(NewLogInterceptor(logger.New(os.Stderr, "", 0))))

server := NewHaberdasherServer(svcImpl, twirp.WithServerInterceptors(NewLogInterceptor(logger.New(os.Stderr, "", 0))))

// NewLogInterceptor logs various parts of a request using a standard Logger.
func NewLogInterceptor(l *log.Logger) twirp.Interceptor {
Expand Down
12 changes: 6 additions & 6 deletions example/service.twirp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions internal/twirptest/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,67 @@ func TestClientContextToHook(t *testing.T) {
}
}

func TestClientInterceptor(t *testing.T) {
interceptor := func(next twirp.Method) twirp.Method {
return func(ctx context.Context, request interface{}) (interface{}, error) {
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)
}
size, ok := request.(*Size)
if !ok {
return nil, fmt.Errorf("could not cast %T to a *Size", request)
}
size.Inches = size.Inches + 1
response, err := next(ctx, request)
hat, ok := response.(*Hat)
if ok && hat != nil {
hat.Color = hat.Color + "x"
return hat, err
}
return nil, err
}
}
h := PickyHatmaker(3)

s := httptest.NewServer(NewHaberdasherServer(h))
defer s.Close()
client := NewHaberdasherProtobufClient(
s.URL,
http.DefaultClient,
twirp.WithClientInterceptors(
interceptor,
interceptor,
),
)
hat, clientErr := client.MakeHat(context.Background(), &Size{Inches: 1})
if clientErr != nil {
t.Fatalf("client err=%q", clientErr)
}
if hat.Size != 3 {
t.Errorf("hat size expected=3 actual=%v", hat.Size)
}
if hat.Color != "bluexx" {
t.Errorf("hat color expected=bluexx actual=%v", hat.Color)
}
_, clientErr = client.MakeHat(context.Background(), &Size{Inches: 3})
twerr, ok := clientErr.(twirp.Error)
if !ok {
t.Fatalf("expected twirp.Error type error, have %T", clientErr)
}
if twerr.Code() != twirp.InvalidArgument {
t.Errorf("expected error type to be InvalidArgument, buf found %q", twerr.Code())
}
}

func TestClientIntermediaryErrors(t *testing.T) {
testcase := func(body string, code int, expectedErrorCode twirp.ErrorCode, clientMaker func(string, HTTPClient, ...twirp.ClientOption) Haberdasher) func(*testing.T) {
return func(t *testing.T) {
Expand Down
12 changes: 6 additions & 6 deletions internal/twirptest/gogo_compat/service.twirp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions internal/twirptest/google_protobuf_imports/service.twirp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions internal/twirptest/importable/importable.twirp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions internal/twirptest/importer/importer.twirp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ae97c56

Please sign in to comment.