Skip to content

Commit 5e0926f

Browse files
committed
feat: support for setting user-agent
Extracted from fork https://github.com/henrybear327/go-proton-api
1 parent 98a73ff commit 5e0926f

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

manager_builder.go

+7
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,16 @@ const (
1515
// DefaultAppVersion is the default app version used to communicate with the API.
1616
// This must be changed (using the WithAppVersion option) for production use.
1717
DefaultAppVersion = "go-proton-api"
18+
19+
// DefaultUserAgent is the default user agent used to communicate with the API.
20+
// See: https://github.com/emersion/hydroxide/issues/252
21+
DefaultUserAgent = ""
1822
)
1923

2024
type managerBuilder struct {
2125
hostURL string
2226
appVersion string
27+
userAgent string
2328
transport http.RoundTripper
2429
verifyProofs bool
2530
cookieJar http.CookieJar
@@ -33,6 +38,7 @@ func newManagerBuilder() *managerBuilder {
3338
return &managerBuilder{
3439
hostURL: DefaultHostURL,
3540
appVersion: DefaultAppVersion,
41+
userAgent: DefaultUserAgent,
3642
transport: http.DefaultTransport,
3743
verifyProofs: true,
3844
cookieJar: nil,
@@ -74,6 +80,7 @@ func (builder *managerBuilder) build() *Manager {
7480
// Set app version in header.
7581
m.rc.OnBeforeRequest(func(_ *resty.Client, req *resty.Request) error {
7682
req.SetHeader("x-pm-appversion", builder.appVersion)
83+
req.SetHeader("User-Agent", builder.userAgent)
7784
return nil
7885
})
7986

option.go

+14
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,20 @@ func WithAppVersion(appVersion string) Option {
3232
}
3333
}
3434

35+
type withUserAgent struct {
36+
userAgent string
37+
}
38+
39+
func (opt withUserAgent) config(builder *managerBuilder) {
40+
builder.userAgent = opt.userAgent
41+
}
42+
43+
func WithUserAgent(userAgent string) Option {
44+
return &withUserAgent{
45+
userAgent: userAgent,
46+
}
47+
}
48+
3549
type withAppVersion struct {
3650
appVersion string
3751
}

0 commit comments

Comments
 (0)