Skip to content

Commit

Permalink
feat: add useagent option
Browse files Browse the repository at this point in the history
  • Loading branch information
9ssi7 committed Nov 13, 2024
1 parent 1439447 commit 9894c07
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
17 changes: 15 additions & 2 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ package osm

// Opts holds configuration options for requests.
type Opts struct {
Locale string // Locale specifies the language for the response.
Locale string // Locale specifies the language for the response.
UserAgent string // UserAgent specifies the User-Agent header for the request.
}

type optFunc func(*Opts)

// DefaultOpts provides the default options for requests.
var DefaultOpts = Opts{
Locale: "en", // Default locale is English.
Locale: "en", // Default locale is English.
UserAgent: "Chrome", // Default User-Agent is Chrome.
}

// WithLocale returns an optFunc that sets the locale option.
Expand All @@ -20,6 +22,14 @@ func WithLocale(locale string) optFunc {
}
}

// WithUserAgent returns an optFunc that sets the User-Agent option.
// userAgent specifies the User-Agent header for the request.
func WithUserAgent(userAgent string) optFunc {
return func(o *Opts) {
o.UserAgent = userAgent
}
}

func mergeOpts(optsList []optFunc) *Opts {
o := &Opts{}
for _, opt := range optsList {
Expand All @@ -28,5 +38,8 @@ func mergeOpts(optsList []optFunc) *Opts {
if o.Locale == "" {
o.Locale = DefaultOpts.Locale
}
if o.UserAgent == "" {
o.UserAgent = DefaultOpts.UserAgent
}
return o
}
1 change: 1 addition & 0 deletions req.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func runRequest[T any](ctx context.Context, url string, opts ...optFunc) (*T, er
return nil, err
}
req.Header.Set("Accept-Language", o.Locale)
req.Header.Set("User-Agent", o.UserAgent)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
Expand Down

0 comments on commit 9894c07

Please sign in to comment.