2020-06-04 14:43:07 +00:00
|
|
|
package inthttp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/eduserver/api"
|
2020-06-12 13:55:57 +00:00
|
|
|
"github.com/matrix-org/dendrite/internal/httputil"
|
2020-06-04 14:43:07 +00:00
|
|
|
"github.com/opentracing/opentracing-go"
|
|
|
|
)
|
|
|
|
|
|
|
|
// HTTP paths for the internal HTTP APIs
|
|
|
|
const (
|
|
|
|
EDUServerInputTypingEventPath = "/eduserver/input"
|
|
|
|
EDUServerInputSendToDeviceEventPath = "/eduserver/sendToDevice"
|
2020-11-09 18:46:11 +00:00
|
|
|
EDUServerInputReceiptEventPath = "/eduserver/receipt"
|
2020-06-04 14:43:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewEDUServerClient creates a EDUServerInputAPI implemented by talking to a HTTP POST API.
|
|
|
|
func NewEDUServerClient(eduServerURL string, httpClient *http.Client) (api.EDUServerInputAPI, error) {
|
|
|
|
if httpClient == nil {
|
|
|
|
return nil, errors.New("NewEDUServerClient: httpClient is <nil>")
|
|
|
|
}
|
|
|
|
return &httpEDUServerInputAPI{eduServerURL, httpClient}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type httpEDUServerInputAPI struct {
|
|
|
|
eduServerURL string
|
|
|
|
httpClient *http.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// InputTypingEvent implements EDUServerInputAPI
|
|
|
|
func (h *httpEDUServerInputAPI) InputTypingEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
request *api.InputTypingEventRequest,
|
|
|
|
response *api.InputTypingEventResponse,
|
|
|
|
) error {
|
|
|
|
span, ctx := opentracing.StartSpanFromContext(ctx, "InputTypingEvent")
|
|
|
|
defer span.Finish()
|
|
|
|
|
|
|
|
apiURL := h.eduServerURL + EDUServerInputTypingEventPath
|
2020-06-12 13:55:57 +00:00
|
|
|
return httputil.PostJSON(ctx, span, h.httpClient, apiURL, request, response)
|
2020-06-04 14:43:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// InputSendToDeviceEvent implements EDUServerInputAPI
|
|
|
|
func (h *httpEDUServerInputAPI) InputSendToDeviceEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
request *api.InputSendToDeviceEventRequest,
|
|
|
|
response *api.InputSendToDeviceEventResponse,
|
|
|
|
) error {
|
|
|
|
span, ctx := opentracing.StartSpanFromContext(ctx, "InputSendToDeviceEvent")
|
|
|
|
defer span.Finish()
|
|
|
|
|
|
|
|
apiURL := h.eduServerURL + EDUServerInputSendToDeviceEventPath
|
2020-06-12 13:55:57 +00:00
|
|
|
return httputil.PostJSON(ctx, span, h.httpClient, apiURL, request, response)
|
2020-06-04 14:43:07 +00:00
|
|
|
}
|
2020-11-09 18:46:11 +00:00
|
|
|
|
|
|
|
// InputSendToDeviceEvent implements EDUServerInputAPI
|
|
|
|
func (h *httpEDUServerInputAPI) InputReceiptEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
request *api.InputReceiptEventRequest,
|
|
|
|
response *api.InputReceiptEventResponse,
|
|
|
|
) error {
|
|
|
|
span, ctx := opentracing.StartSpanFromContext(ctx, "InputReceiptEventPath")
|
|
|
|
defer span.Finish()
|
|
|
|
|
|
|
|
apiURL := h.eduServerURL + EDUServerInputReceiptEventPath
|
|
|
|
return httputil.PostJSON(ctx, span, h.httpClient, apiURL, request, response)
|
|
|
|
}
|