-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsequin.go
More file actions
199 lines (163 loc) · 5.08 KB
/
Copy pathsequin.go
File metadata and controls
199 lines (163 loc) · 5.08 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package sequin
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"time"
)
// SequinClient defines the interface for Sequin client operations
type SequinClient interface {
Receive(ctx context.Context, consumerGroupID string, params *ReceiveParams) ([]Message, error)
Ack(ctx context.Context, consumerGroupID string, ackIDs []string) error
Nack(ctx context.Context, consumerGroupID string, ackIDs []string) error
}
// Client represents a Sequin client
type Client struct {
baseURL string
token string
httpClient *http.Client
}
// Ensure Client implements SequinClient interface
var _ SequinClient = (*Client)(nil)
// ClientOptions configures the client behavior
type ClientOptions struct {
Token string // API authentication token
BaseURL string // API base URL, defaults to "https://api.sequinstream.com/api"
HTTPClient *http.Client // Custom HTTP client, optional
Timeout time.Duration // HTTP client timeout, defaults to 30s
}
// NewClient creates a new Sequin client
func NewClient(opts *ClientOptions) *Client {
if opts == nil {
opts = &ClientOptions{}
}
if opts.Token == "" {
panic("token is required")
}
if opts.BaseURL == "" {
opts.BaseURL = "https://api.sequinstream.com/api"
}
if opts.HTTPClient == nil {
timeout := opts.Timeout
if timeout == 0 {
timeout = 150 * time.Second
}
opts.HTTPClient = &http.Client{
Timeout: timeout,
}
}
return &Client{
baseURL: opts.BaseURL,
token: opts.Token,
httpClient: opts.HTTPClient,
}
}
// ReceiveResponse represents the response from the receive endpoint
type ReceiveResponse struct {
Data []struct {
AckID string `json:"ack_id"`
Data struct {
Record json.RawMessage `json:"record"`
} `json:"data"`
} `json:"data"`
}
// ReceiveParams represents parameters for the receive request
type ReceiveParams struct {
MaxBatchSize int `json:"max_batch_size,omitempty"`
WaitFor int `json:"wait_for,omitempty"` // milliseconds
}
// Receive fetches messages from a consumer
func (c *Client) Receive(ctx context.Context, consumerGroupID string, params *ReceiveParams) ([]Message, error) {
url := fmt.Sprintf("%s/api/http_pull_consumers/%s/receive", c.baseURL, consumerGroupID)
var body []byte
var err error
if params != nil {
body, err = json.Marshal(params)
if err != nil {
return nil, fmt.Errorf("marshaling receive params: %w", err)
}
}
req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(body))
if err != nil {
return nil, fmt.Errorf("creating request: %w", err)
}
req.Header.Set("Authorization", "Bearer "+c.token)
req.Header.Set("Content-Type", "application/json")
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("making request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
var receiveResp ReceiveResponse
if err := json.NewDecoder(resp.Body).Decode(&receiveResp); err != nil {
return nil, fmt.Errorf("decoding response: %w", err)
}
messages := make([]Message, len(receiveResp.Data))
for i, msg := range receiveResp.Data {
messages[i] = Message{
AckID: msg.AckID,
Record: msg.Data.Record,
}
}
return messages, nil
}
// Ack acknowledges messages as processed
func (c *Client) Ack(ctx context.Context, consumerGroupID string, ackIDs []string) error {
url := fmt.Sprintf("%s/api/http_pull_consumers/%s/ack", c.baseURL, consumerGroupID)
body, err := json.Marshal(map[string][]string{
"ack_ids": ackIDs,
})
if err != nil {
return fmt.Errorf("marshaling ack request: %w", err)
}
req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(body))
if err != nil {
return fmt.Errorf("creating request: %w", err)
}
req.Header.Set("Authorization", "Bearer "+c.token)
req.Header.Set("Content-Type", "application/json")
resp, err := c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("making request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
return nil
}
// Nack negative acknowledges messages, making them available for redelivery
func (c *Client) Nack(ctx context.Context, consumerGroupID string, ackIDs []string) error {
url := fmt.Sprintf("%s/api/http_pull_consumers/%s/nack", c.baseURL, consumerGroupID)
body, err := json.Marshal(map[string][]string{
"ack_ids": ackIDs,
})
if err != nil {
return fmt.Errorf("marshaling nack request: %w", err)
}
req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(body))
if err != nil {
return fmt.Errorf("creating request: %w", err)
}
req.Header.Set("Authorization", "Bearer "+c.token)
req.Header.Set("Content-Type", "application/json")
resp, err := c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("making request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status code: %w", err)
}
return nil
}
// Message represents a single message with its acknowledgment ID
type Message struct {
AckID string
Record json.RawMessage
}