1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package shared

import (
	"fmt"
	"net/http"
	"rss-kakapo/www"
	"strings"
)

const userAgentTemplate = "rss-kakapo/%s (+https://%s)"

type IUserAgent interface {
	AddUserAgent(req *http.Request)
}

type userAgent struct {
	userAgentValue string
}

func NewUserAgent(cfg *Config) IUserAgent {
	return &userAgent{
		userAgentValue: buildUserAgentString(cfg.Host),
	}
}

func buildUserAgentString(host string) string {
	versionStr := strings.TrimPrefix(www.Version, "v")
	return fmt.Sprintf(userAgentTemplate, versionStr, host)
}

func (ua *userAgent) AddUserAgent(req *http.Request) {
	req.Header.Add("User-Agent", ua.userAgentValue)
}