Files
meowlib/http.go
T
yves 8b106db52f
continuous-integration/drone/push Build is failing
duplicate messages send fixes
2026-04-21 15:53:56 +02:00

51 lines
1.1 KiB
Go

package meowlib
import (
"bytes"
"encoding/json"
"io"
"net/http"
"time"
)
func HttpGetId(url string) (response map[string]string, err error) {
srvId := make(map[string]string)
resp, err := http.Get(url + "/id")
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = json.Unmarshal(body, &srvId)
if err != nil {
return nil, err
}
return srvId, nil
}
func HttpPostMessage(url string, msg []byte, timeout int) (response []byte, err error) {
client := http.Client{
Timeout: time.Duration(timeout) * time.Second,
}
resp, err := client.Post(url+"/msg",
"application/octet-stream", bytes.NewBuffer(msg))
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
// Server already accepted the request (2xx) — body truncation on our
// side doesn't mean the message wasn't stored. Return what we have so
// the caller doesn't retry and produce a duplicate.
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
return body, nil
}
return nil, err
}
return body, nil
}