Skip to content

feat: provide better NewClient methods with baseURL/basePath #3735

Open
@gabyx

Description

@gabyx

Hi there,

Could we make a pull request to add an essential part which is missing on instantiating a client from the Goa generated code:

The client takes the following signature:

func NewClient(
	scheme string,
	host string,
	doer goahttp.Doer,
	enc func(*http.Request) goahttp.Encoder,
	dec func(*http.Response) goahttp.Decoder,
	restoreBody bool,
) *Client

which leaves no room to set the base url like schema: https , baseURL: domain.com/a/b/c and all endpoints will start with https://domain.com/a/b/c/..... The host parameter cannot be misused as its used in url.URL{...} which has different semantics.

We would like to add another non-breaking function NewClientWithOpts(opts ... ClientOption) which lets us chose
all relevant options, and defaulting the ones not set.

NewClientWithOpts(
  WithSchema("https"), 
  WithHost("domain.com:8080"), 
  WithURL(url.Parse("https://domain.com/a/b/c?a=asdf), // here only taking host and schema and path.
  WithDoer(...), 
  WithDecoder(...), 
  WithEncoder(...), 
  WithRestoreBody(true))

etc.

The old function NewClient can then forward to
NewClientsWithOpts(WithBaseURL(url.URL{Scheme: scheme, Host: host})).

We really need this somewhat soon.

Implementation

type Client struct {
   ...
	scheme  string
	host    string
    basePath string // this is probably enough to add (prefix with `/` if not given)

	encoder func(*http.Request) goahttp.Encoder
	decoder func(*http.Response) goahttp.Decoder
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions