This repository has been archived by the owner on Sep 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathsubscriber.go
78 lines (69 loc) · 2.66 KB
/
subscriber.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
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
71
72
73
74
75
76
77
78
// Package subscriber holds logic to communicate between the
// external initiator service and the external endpoints it
// subscribes to.
package subscriber
import "github.com/smartcontractkit/external-initiator/store"
// Type holds the connection type for the subscription
type Type int
const (
// WS are connections made over WebSocket
WS Type = iota
// RPC are connections made by POSTing a JSON payload
// to the external endpoint.
RPC
// Client are connections encapsulated in its
// entirety by the blockchain implementation.
Client
// Unknown is just a placeholder for when
// it cannot be determined how connections
// should be made. When this is returned,
// it should be considered an error.
Unknown
)
// SubConfig holds the configuration required to connect
// to the external endpoint.
type SubConfig struct {
Endpoint string
}
// Event is the individual event that occurs during
// the subscription.
type Event []byte
// JsonManager holds the interface for generating blockchain
// specific payloads and parsing the response for the
// appropriate blockchain.
type JsonManager interface {
// Get JSON payload to send when opening a new subscription
GetTriggerJson() []byte
// Parse the response returned after sending GetTriggerJson()
ParseResponse(data []byte) ([]Event, bool)
// Get JSON payload to send when testing a connection
GetTestJson() []byte
// Parse the response returned after sending GetTestJson()
ParseTestResponse(data []byte) error
}
// ISubscription holds the interface for interacting
// with an active subscription.
type ISubscription interface {
// Unsubscribe closes the connection to the external endpoint
// and stops any processes related to this subscription.
Unsubscribe()
}
// ISubscriber holds the interface for interacting
// with a not-yet-active subscription.
type ISubscriber interface {
// SubscribeToEvents subscribes to events using the endpoint and configuration
// as set in ISubscriber. All events will be sent in the channel. If anything is
// passed as a param after channel, it will not expect to receive any confirmation
// message after opening the initial subscription.
SubscribeToEvents(channel chan<- Event, runtimeConfig store.RuntimeConfig) (ISubscription, error)
// Test attempts to open a connection using the endpoint and configuration
// as set in ISubscriber. If connection is succesful, it sends GetTestJson() as a payload
// and attempts to parse response with ParseTestResponse().
Test() error
}
// IParser holds the interface for parsing data
// from the external endpoint into an array of Events
// based on the blockchain's parser.
type IParser interface {
ParseResponse(data []byte) ([]Event, bool)
}