Skip to content

Commit

Permalink
Client option twirp.WithClientPathPrefix(prefix), where /twirp is the…
Browse files Browse the repository at this point in the history
… default prefix
  • Loading branch information
marioizquierdo committed Sep 2, 2020
1 parent f8cb685 commit fc1a41a
Show file tree
Hide file tree
Showing 22 changed files with 464 additions and 374 deletions.
26 changes: 15 additions & 11 deletions client_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,15 @@ type ClientOption func(*ClientOptions)

// ClientOptions encapsulate the configurable parameters on a Twirp client.
type ClientOptions struct {
Hooks *ClientHooks
SkipPathPrefix bool // if true, skip the default "/twirp" path prefix
Hooks *ClientHooks
pathPrefix *string // default "/twirp", prefix appended to the baseURL when making requests
}

func (opts *ClientOptions) PathPrefix() string {
if opts.pathPrefix == nil {
return "/twirp" // default prefix
}
return *opts.pathPrefix
}

// ClientHooks is a container for callbacks that can instrument a
Expand Down Expand Up @@ -61,17 +68,14 @@ func WithClientHooks(hooks *ClientHooks) ClientOption {
}
}

// SkipClientPathPrefix skips the default "/twirp" prefix in request URLs.
// By default, Twirp routes have the prefix appended to the baseURL:
// <baseURL>/twirp/<package>.<Service>/<Method>
// but with this option, the client makes requests without the prefix:
// <baseURL>/<package>.<Service>/<Method>
// Note: A different path prefix can be set in the baseURL.
// Note: The service needs to be configured to handle requests on the same baseURL.
// WithClientPathPrefix appends a different prefix to the baseURL.
// If not specified, the "/twirp" prefix is used by default.
// The service must be configured to serve on the same prefix.
// URL format: "<baseURL>[<prefix>]/<package>.<Service>/<Method>"
// More info on Twirp docs: https://twitchtv.github.io/twirp/docs/routing.html
func SkipClientPathPrefix() ClientOption {
func WithClientPathPrefix(prefix string) ClientOption {
return func(o *ClientOptions) {
o.SkipPathPrefix = true
o.pathPrefix = &prefix
}
}

Expand Down
20 changes: 14 additions & 6 deletions client_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,23 @@ func TestChainClientHooks(t *testing.T) {
}
}

func TestSkipClientPathPrefix(t *testing.T) {
func TestWithClientPathPrefix(t *testing.T) {
opts := &ClientOptions{}

if have, want := opts.SkipPathPrefix, false; have != want {
t.Errorf("unexpected opts.SkipPathPrefix, have: %v, want: %v", have, want)
// Default value
if have, want := opts.PathPrefix(), "/twirp"; have != want {
t.Errorf("unexpected default PathPrefix() on ClientOptions, have: %q, want: %q", have, want)
}

SkipClientPathPrefix()(opts)
if have, want := opts.SkipPathPrefix, true; have != want {
t.Errorf("unexpected opts.SkipPathPrefix, have: %v, want: %v", have, want)
// Set a different prefix
WithClientPathPrefix("/newprfx/foobar")(opts)
if have, want := opts.PathPrefix(), "/newprfx/foobar"; have != want {
t.Errorf("unexpected value after WithClientPathPrefix, have: %q, want: %q", have, want)
}

// Use empty value for no-prefix
WithClientPathPrefix("")(opts)
if have, want := opts.PathPrefix(), ""; have != want {
t.Errorf("unexpected value after WithClientPathPrefix, have: %q, want: %q", have, want)
}
}
46 changes: 25 additions & 21 deletions clientcompat/internal/clientcompat/clientcompat.twirp.go

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

42 changes: 23 additions & 19 deletions example/service.twirp.go

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

22 changes: 17 additions & 5 deletions internal/twirptest/empty_service/empty_service.twirp.go

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

42 changes: 23 additions & 19 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.

Loading

0 comments on commit fc1a41a

Please sign in to comment.