Skip to content
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

Allow incremental addition of HTTP headers to generated Go clients #5458

Open
nyergler opened this issue Dec 20, 2024 · 0 comments
Open

Allow incremental addition of HTTP headers to generated Go clients #5458

nyergler opened this issue Dec 20, 2024 · 0 comments
Assignees
Labels
feature Requests for new features. product/sdk-generator Fern's SDK Generator that outputs client libraries in 7 languages

Comments

@nyergler
Copy link

Feature Description

We have two different auth mechanisms for our API: one uses a bearer token and one uses basic auth.

I was confused recently as to why some requests weren't coming through with the HTTP User-Agent I was configuring. Then I realized that it was because the option.WithHTTPHeader stomps on previously set headers.

For example, constructing a client with the following:

    return ptclient.NewClient(
        ptoption.WithBaseURL(deploymentURL),
        ptoption.WithHTTPHeader(http.Header{
            "User-Agent": []string{UserAgent},
        }),
        ptoption.WithHTTPHeader(http.Header{
            "Authorization": []string{"Basic " + basicAuth(p.DeploymentKey, "")},
        }),
    )

Replaces the user agent with the authorization. (This is slightly more obvious in this example; in my case I had a function that was applying the "base" options ---use, user-agent -- and appending the passed in auth option(s)).

I think it'd be clearer to if WithHTTPHeader was additive, with a option.WithHTTPHeaders (plural) that does the current behavior. Since changing the semantics may be tricky, perhaps an AddHTTPHeader is a reasonable name for an additive case.

Proposed Generated Code (Optional)

// HTTPHeaderOption implements the RequestOption interface.
type HTTPHeaderOption struct {
	HTTPHeader http.Header
	Replace bool
}

func (h *HTTPHeaderOption) applyRequestOptions(opts *RequestOptions) {
	if h.Replace {
		opts.HTTPHeader = h.HTTPHeader
	} else {
		opts.HTTPHeader = append(opts.HTTPHeader, h.HTTPHeader...)
	}
}
// WithHTTPHeader replaces the http.Headers for the request.
func WithHTTPHeader(httpHeader http.Header) *core.HTTPHeaderOption {
	return &core.HTTPHeaderOption{
		// Clone the headers so they can't be modified after the option call.
		HTTPHeader: httpHeader.Clone(),
	}
}

// AddHTTPHeader adds the given http.Headers to the request.
func AddHTTPHeader(httpHeader http.Header) *core.HTTPHeaderOption {
	return &core.HTTPHeaderOption{
		// Clone the headers so they can't be modified after the option call.
		HTTPHeader: httpHeader.Clone(),
		Replace:true,
	}
}
@nyergler nyergler added feature Requests for new features. product/sdk-generator Fern's SDK Generator that outputs client libraries in 7 languages labels Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Requests for new features. product/sdk-generator Fern's SDK Generator that outputs client libraries in 7 languages
Development

No branches or pull requests

2 participants