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

feat: support for setting user-agent #158

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions manager_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@ const (
// DefaultAppVersion is the default app version used to communicate with the API.
// This must be changed (using the WithAppVersion option) for production use.
DefaultAppVersion = "go-proton-api"

// DefaultUserAgent is the default user agent used to communicate with the API.
// See: https://github.com/emersion/hydroxide/issues/252
DefaultUserAgent = ""
)

type managerBuilder struct {
hostURL string
appVersion string
userAgent string
transport http.RoundTripper
verifyProofs bool
cookieJar http.CookieJar
Expand All @@ -33,6 +38,7 @@ func newManagerBuilder() *managerBuilder {
return &managerBuilder{
hostURL: DefaultHostURL,
appVersion: DefaultAppVersion,
userAgent: DefaultUserAgent,
transport: http.DefaultTransport,
verifyProofs: true,
cookieJar: nil,
Expand Down Expand Up @@ -74,6 +80,7 @@ func (builder *managerBuilder) build() *Manager {
// Set app version in header.
m.rc.OnBeforeRequest(func(_ *resty.Client, req *resty.Request) error {
req.SetHeader("x-pm-appversion", builder.appVersion)
req.SetHeader("User-Agent", builder.userAgent)
return nil
})

Expand Down
14 changes: 14 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ func WithAppVersion(appVersion string) Option {
}
}

type withUserAgent struct {
userAgent string
}

func (opt withUserAgent) config(builder *managerBuilder) {
builder.userAgent = opt.userAgent
}

func WithUserAgent(userAgent string) Option {
return &withUserAgent{
userAgent: userAgent,
}
}

type withAppVersion struct {
appVersion string
}
Expand Down