feat: pass through query

This commit is contained in:
Kevin Franklin Kim 2024-09-30 10:12:33 +02:00
parent c22b610038
commit 519267b962
No known key found for this signature in database
4 changed files with 27 additions and 10 deletions

View File

@ -0,0 +1,5 @@
package mpv2
const (
MetadataRequestQuery = "RequestQuery"
)

View File

@ -119,17 +119,20 @@ func (p *Publisher) handle(l *zap.Logger, msg *message.Message) error {
return errors.Wrap(err, "failed to create request") return errors.Wrap(err, "failed to create request")
} }
for s, s2 := range msg.Metadata { for key, value := range msg.Metadata {
if s == "Cookie" { switch key {
for _, s3 := range strings.Split(s2, "; ") { case "Cookie":
for _, s3 := range strings.Split(value, "; ") {
val := strings.Split(s3, "=") val := strings.Split(s3, "=")
req.AddCookie(&http.Cookie{ req.AddCookie(&http.Cookie{
Name: val[0], Name: val[0],
Value: strings.Join(val[1:], "="), Value: strings.Join(val[1:], "="),
}) })
} }
} else { case MetadataRequestQuery:
req.Header.Set(s, s2) req.URL.RawQuery = value
default:
req.Header.Set(key, value)
} }
} }

View File

@ -119,6 +119,10 @@ func (s *Subscriber) handle(l *zap.Logger, r *http.Request, payload *mpv2.Payloa
l = l.With(zap.String("message_id", msg.UUID)) l = l.With(zap.String("message_id", msg.UUID))
msg.SetContext(context.WithoutCancel(r.Context())) msg.SetContext(context.WithoutCancel(r.Context()))
// store query
msg.Metadata.Set(MetadataRequestQuery, r.URL.RawQuery)
// store header
for name, headers := range r.Header { for name, headers := range r.Header {
msg.Metadata.Set(name, strings.Join(headers, ",")) msg.Metadata.Set(name, strings.Join(headers, ","))
} }

View File

@ -16,11 +16,15 @@ import (
type ( type (
MPv2 struct { MPv2 struct {
l *zap.Logger l *zap.Logger
path string path string
host string host string
cookies []string cookies []string
apiSecret string // To create a new secret, navigate in the Google Analytics UI to:
// Admin > Data Streams > choose your stream > Measurement Protocol > Create
apiSecret string
// Measurement ID. The identifier for a Data Stream. Found in the Google Analytics UI under:
// Admin > Data Streams > choose your stream > Measurement ID
measurementID string measurementID string
protocolVersion string protocolVersion string
httpClient *http.Client httpClient *http.Client
@ -139,6 +143,7 @@ func (c *MPv2) SendRaw(r *http.Request, payload *mpv2.Payload[any]) error {
return errors.Wrap(err, "failed to create request") return errors.Wrap(err, "failed to create request")
} }
// query
qry := req.URL.Query() qry := req.URL.Query()
if len(c.apiSecret) > 0 { if len(c.apiSecret) > 0 {
qry.Add("api_secret", c.apiSecret) qry.Add("api_secret", c.apiSecret)