mirror of
https://github.com/matrix-org/dendrite.git
synced 2024-11-27 08:41:57 -06:00
9a46d8d95c
In an attempt to: - make on-boarding a bit easier (`go test ./...` should now not need additional postgres setup) - get code coverage faster, not only scheduled at night - test the `create-account` binary
69 lines
1.6 KiB
Go
69 lines
1.6 KiB
Go
package pushgateway
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"crypto/tls"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/matrix-org/dendrite/internal"
|
|
|
|
"github.com/opentracing/opentracing-go"
|
|
)
|
|
|
|
type httpClient struct {
|
|
hc *http.Client
|
|
}
|
|
|
|
// NewHTTPClient creates a new Push Gateway client.
|
|
func NewHTTPClient(disableTLSValidation bool) Client {
|
|
hc := &http.Client{
|
|
Timeout: 30 * time.Second,
|
|
Transport: &http.Transport{
|
|
DisableKeepAlives: true,
|
|
TLSClientConfig: &tls.Config{
|
|
InsecureSkipVerify: disableTLSValidation,
|
|
},
|
|
Proxy: http.ProxyFromEnvironment,
|
|
},
|
|
}
|
|
return &httpClient{hc: hc}
|
|
}
|
|
|
|
func (h *httpClient) Notify(ctx context.Context, url string, req *NotifyRequest, resp *NotifyResponse) error {
|
|
span, ctx := opentracing.StartSpanFromContext(ctx, "Notify")
|
|
defer span.Finish()
|
|
|
|
body, err := json.Marshal(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
hreq, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
hreq.Header.Set("Content-Type", "application/json")
|
|
|
|
hresp, err := h.hc.Do(hreq)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
defer internal.CloseAndLogIfError(ctx, hresp.Body, "failed to close response body")
|
|
|
|
if hresp.StatusCode == http.StatusOK {
|
|
return json.NewDecoder(hresp.Body).Decode(resp)
|
|
}
|
|
|
|
var errorBody struct {
|
|
Message string `json:"message"`
|
|
}
|
|
if err := json.NewDecoder(hresp.Body).Decode(&errorBody); err == nil {
|
|
return fmt.Errorf("push gateway: %d from %s: %s", hresp.StatusCode, url, errorBody.Message)
|
|
}
|
|
return fmt.Errorf("push gateway: %d from %s", hresp.StatusCode, url)
|
|
}
|