Use gomatrixserverlib.Client for push notifications

This commit is contained in:
Neil Alexander 2022-05-05 10:31:45 +01:00
parent 01ba326d83
commit 236d7bd361
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944

View file

@ -3,31 +3,28 @@ package pushgateway
import ( import (
"bytes" "bytes"
"context" "context"
"crypto/tls"
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"time" "time"
"github.com/matrix-org/gomatrixserverlib"
"github.com/opentracing/opentracing-go" "github.com/opentracing/opentracing-go"
) )
type httpClient struct { type httpClient struct {
hc *http.Client hc *gomatrixserverlib.Client
} }
// NewHTTPClient creates a new Push Gateway client. // NewHTTPClient creates a new Push Gateway client.
func NewHTTPClient(disableTLSValidation bool) Client { func NewHTTPClient(disableTLSValidation bool) Client {
hc := &http.Client{ return &httpClient{
Timeout: 30 * time.Second, hc: gomatrixserverlib.NewClient(
Transport: &http.Transport{ gomatrixserverlib.WithTimeout(time.Second*30),
DisableKeepAlives: true, gomatrixserverlib.WithKeepAlives(false),
TLSClientConfig: &tls.Config{ gomatrixserverlib.WithSkipVerify(disableTLSValidation),
InsecureSkipVerify: disableTLSValidation, ),
},
},
} }
return &httpClient{hc: hc}
} }
func (h *httpClient) Notify(ctx context.Context, url string, req *NotifyRequest, resp *NotifyResponse) error { func (h *httpClient) Notify(ctx context.Context, url string, req *NotifyRequest, resp *NotifyResponse) error {
@ -44,7 +41,7 @@ func (h *httpClient) Notify(ctx context.Context, url string, req *NotifyRequest,
} }
hreq.Header.Set("Content-Type", "application/json") hreq.Header.Set("Content-Type", "application/json")
hresp, err := h.hc.Do(hreq) hresp, err := h.hc.DoHTTPRequest(ctx, hreq)
if err != nil { if err != nil {
return err return err
} }