aboutsummaryrefslogtreecommitdiff
path: root/eduserver/inthttp/client.go
blob: 0690ed827abbc2b6db2299f86d81147a205ce2ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package inthttp

import (
	"context"
	"errors"
	"net/http"

	"github.com/matrix-org/dendrite/eduserver/api"
	"github.com/matrix-org/dendrite/internal/httputil"
	"github.com/opentracing/opentracing-go"
)

// HTTP paths for the internal HTTP APIs
const (
	EDUServerInputTypingEventPath       = "/eduserver/input"
	EDUServerInputSendToDeviceEventPath = "/eduserver/sendToDevice"
	EDUServerInputReceiptEventPath      = "/eduserver/receipt"
)

// 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
	return httputil.PostJSON(ctx, span, h.httpClient, apiURL, request, response)
}

// 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
	return httputil.PostJSON(ctx, span, h.httpClient, apiURL, request, response)
}

// 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)
}