src

Go monorepo.
git clone git://code.dwrz.net/src
Log | Files | Refs

user_agent.go (885B)


      1 package http
      2 
      3 import (
      4 	"strings"
      5 )
      6 
      7 // UserAgentBuilder is a builder for a HTTP User-Agent string.
      8 type UserAgentBuilder struct {
      9 	sb strings.Builder
     10 }
     11 
     12 // NewUserAgentBuilder returns a new UserAgentBuilder.
     13 func NewUserAgentBuilder() *UserAgentBuilder {
     14 	return &UserAgentBuilder{sb: strings.Builder{}}
     15 }
     16 
     17 // AddKey adds the named component/product to the agent string
     18 func (u *UserAgentBuilder) AddKey(key string) {
     19 	u.appendTo(key)
     20 }
     21 
     22 // AddKeyValue adds the named key to the agent string with the given value.
     23 func (u *UserAgentBuilder) AddKeyValue(key, value string) {
     24 	u.appendTo(key + "/" + value)
     25 }
     26 
     27 // Build returns the constructed User-Agent string. May be called multiple times.
     28 func (u *UserAgentBuilder) Build() string {
     29 	return u.sb.String()
     30 }
     31 
     32 func (u *UserAgentBuilder) appendTo(value string) {
     33 	if u.sb.Len() > 0 {
     34 		u.sb.WriteRune(' ')
     35 	}
     36 	u.sb.WriteString(value)
     37 }