/
provider.go
43 lines (36 loc) · 1.14 KB
/
provider.go
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
package binance
import (
"context"
"time"
"github.com/alextsa22/cryptocurrency-order-book/internal/config"
"github.com/alextsa22/cryptocurrency-order-book/internal/config/binance"
"github.com/alextsa22/cryptocurrency-order-book/internal/delivery"
"github.com/alextsa22/cryptocurrency-order-book/internal/domain"
)
type DeliveryMethod int
const (
// RestMethod indicates to use REST for delivery
RestMethod = iota
// WebSocket indicates to use WS for delivery
WebSocket
)
type DeliveryProvider struct {
apiConfig *binance.ApiConfig
method DeliveryMethod
rate time.Duration
}
func NewDeliveryProvider(serviceConfig *config.ServiceConfig, apiConfig *binance.ApiConfig, method DeliveryMethod) delivery.DepthDeliveryProvider {
return &DeliveryProvider{
apiConfig: apiConfig,
method: method,
rate: time.Duration(serviceConfig.Rate) * time.Second,
}
}
func (p *DeliveryProvider) DepthDelivery(ctx context.Context, symbol string, limit int, dataCh chan *domain.Depth) {
switch p.method {
case RestMethod:
p.restDepthDelivery(ctx, symbol, limit, dataCh)
case WebSocket:
p.wsDepthDelivery(ctx, symbol, limit, dataCh)
}
}