fix: handle richtsse

This commit is contained in:
Kevin Franklin Kim 2024-03-05 15:04:14 +01:00
parent 74a97246b0
commit d943476d76
No known key found for this signature in database
2 changed files with 23 additions and 5 deletions

View File

@ -2,6 +2,7 @@ package gtm
import (
"encoding/json"
"fmt"
"net/http"
"github.com/ThreeDotsLabs/watermill/message"
@ -83,19 +84,20 @@ func (p *Publisher) Publish(topic string, messages ...*message.Message) error {
return err
}
// NOTE: `richsstsse` seems to be last parameter in the query to let's ensure it stays that way
var richsstsse bool
if values.Has("richsstsse") {
values.Del("richsstsse")
richsstsse = true
values.Del("richsstsse")
}
u := p.url + "?"
url := fmt.Sprintf("%s?%s", p.url, values.Encode())
if richsstsse {
u += "&richsstsse"
url += "&richsstsse"
}
req, err := http.NewRequestWithContext(msg.Context(), http.MethodPost, u, body)
req, err := http.NewRequestWithContext(msg.Context(), http.MethodPost, url, body)
if err != nil {
return errors.Wrap(err, "failed to create request")
}

View File

@ -25,6 +25,13 @@ const (
type Data map[string]any
func Marshal(input *Event) (url.Values, io.Reader, error) {
var richsstsse bool
// NOTE: `richsstsse` seems to be last parameter in the query to let's ensure it stays that way
if input.Richsstsse != nil {
richsstsse = true
input.Richsstsse = nil
}
a, err := json.Marshal(input)
if err != nil {
return nil, nil, err
@ -66,13 +73,22 @@ func Marshal(input *Event) (url.Values, io.Reader, error) {
var body []string
var reader io.Reader
for len(ret.Encode()) > 2048 {
maxQueryLength := 2048 //
if richsstsse {
maxQueryLength -= len("&richsstsse")
}
for len(ret.Encode()) > maxQueryLength {
for s, i := range ret {
ret.Del(s)
body = append(body, s+"="+i[0])
break
}
}
if richsstsse {
ret.Add("richsstsse", "")
}
if len(body) > 0 {
reader = bytes.NewReader([]byte(strings.Join(body, "&")))
}