github.com/prebid/prebid-server/v2@v2.18.0/version/xprebidheader.go (about) 1 package version 2 3 import ( 4 "strings" 5 6 "github.com/prebid/openrtb/v20/openrtb2" 7 8 "github.com/prebid/prebid-server/v2/openrtb_ext" 9 ) 10 11 const xPrebidHeaderVersionPrefix = "pbs-go" 12 13 func BuildXPrebidHeader(version string) string { 14 sb := &strings.Builder{} 15 writeNameVersionRecord(sb, xPrebidHeaderVersionPrefix, version) 16 return sb.String() 17 } 18 19 func BuildXPrebidHeaderForRequest(bidRequest *openrtb2.BidRequest, version string) string { 20 req := &openrtb_ext.RequestWrapper{BidRequest: bidRequest} 21 22 sb := &strings.Builder{} 23 writeNameVersionRecord(sb, xPrebidHeaderVersionPrefix, version) 24 25 if reqExt, err := req.GetRequestExt(); err == nil && reqExt != nil { 26 if prebidExt := reqExt.GetPrebid(); prebidExt != nil { 27 if channel := prebidExt.Channel; channel != nil { 28 writeNameVersionRecord(sb, channel.Name, channel.Version) 29 } 30 } 31 } 32 if appExt, err := req.GetAppExt(); err == nil && appExt != nil { 33 if prebidExt := appExt.GetPrebid(); prebidExt != nil { 34 writeNameVersionRecord(sb, prebidExt.Source, prebidExt.Version) 35 } 36 } 37 return sb.String() 38 } 39 40 func writeNameVersionRecord(sb *strings.Builder, name, version string) { 41 if name == "" { 42 return 43 } 44 if version == "" { 45 version = VerUnknown 46 } 47 if sb.Len() != 0 { 48 sb.WriteString(",") 49 } 50 sb.WriteString(name) 51 sb.WriteString("/") 52 sb.WriteString(version) 53 }