-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubscriptions.go
93 lines (74 loc) · 2.16 KB
/
subscriptions.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package corroclient
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
)
type event struct {
EOQ *EOQ `json:"eoq"`
Columns Columns `json:"columns"`
Row []any `json:"row"`
Change []any `json:"change"`
Error *string `json:"error"`
}
var ErrMissedChange = errors.New("corrosubs: missed change")
var ErrMaxRetryExceeded = errors.New("corrosubs: lost connection")
var ErrSubscriptionClosed = errors.New("corrosubs: subscription closed")
var ErrUnrecoverableSub = errors.New("corrosubs: unrecoverable subscription")
var ErrUnknownEvent = errors.New("corroclient: unknown event in subscription")
func (c *CorroClient) postSubscription(ctx context.Context, statement Statement, skipRows bool, from uint64) (*http.Response, error) {
data, err := json.Marshal(statement)
if err != nil {
return nil, err
}
path := fmt.Sprintf("/v1/subscriptions?skip_rows=%t", skipRows)
if from != 0 {
path += fmt.Sprintf("&from=%d", from)
}
req, err := http.NewRequestWithContext(ctx, "POST", c.getURL("/v1/subscriptions"), bytes.NewBuffer(data))
if err != nil {
return nil, err
}
resp, err := c.request(req)
if err != nil {
return nil, err
}
if resp.StatusCode == http.StatusOK {
return resp, nil
}
if resp.StatusCode == http.StatusNotFound {
return nil, errNotFound
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return nil, fmt.Errorf("corroclient-error: %s", string(body))
}
var errNotFound = errors.New("corroclient: subscription not found")
func (c *CorroClient) getSub(ctx context.Context, subscriptionId string, skipRows bool, from uint64) (*http.Response, error) {
path := fmt.Sprintf("/v1/subscriptions/%s?skip_rows=%t", subscriptionId, skipRows)
if from != 0 {
path += fmt.Sprintf("&from=%d", from)
}
req, err := http.NewRequestWithContext(ctx, "GET", c.getURL(path), nil)
if err != nil {
return nil, err
}
resp, err := c.request(req)
if err != nil {
return nil, err
}
if resp.StatusCode == http.StatusOK {
return resp, nil
}
resp.Body.Close()
if resp.StatusCode == http.StatusNotFound {
return nil, errNotFound
}
return nil, errors.New("corrosubs: Invalid status code")
}