goadesign / goadesign/goa

feat: provide better `NewClient` methods with `baseURL`/`basePath`

Open
#3,735 5 comments 1 reaction 0 assignees View on GitHub
Dominant language
Go
Stars
6.1k
Forks
583
Avg merge
2d 10h
Merged PRs (30d)
15

Description

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:

```go
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.

```go
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

```go
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
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.