dink-sdk 0.3.1

Rust SDK for Dink edge mesh platform — JSON-over-NATS RPC for IoT and edge computing
Documentation
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::Duration;

// ============================
// Base Service Types
// ============================

/// Service definition metadata. Uses `&'static` references for codegen constants.
#[derive(Debug, Clone, Copy)]
pub struct ServiceDefinition {
    pub name: &'static str,
    pub version: &'static str,
    pub methods: &'static [&'static str],
}

// ============================
// Edge Client Configuration
// ============================

/// Configuration for EdgeClient.
#[derive(Debug, Clone)]
pub struct EdgeConfig {
    /// Edge API key (required).
    pub api_key: String,
    /// dinkd server URL. Optional if using new key format with embedded URL.
    pub server_url: Option<String>,
    /// Optional display name for the edge.
    pub edge_name: Option<String>,
    /// Optional labels for edge discovery.
    pub labels: HashMap<String, String>,
    /// Optional version string.
    pub version: Option<String>,
    /// Optional hostname.
    pub hostname: Option<String>,
    /// Optional IP address.
    pub ip_address: Option<String>,
    /// Heartbeat interval (default: 30s).
    pub heartbeat_interval: Duration,
    /// Connection/request timeout (default: 30s).
    pub timeout: Duration,
    /// Reconnect wait time (default: 2s).
    pub reconnect_wait: Duration,
    /// Max reconnect attempts, -1 for infinite (default: -1).
    pub max_reconnects: i32,
}

impl Default for EdgeConfig {
    fn default() -> Self {
        Self {
            api_key: String::new(),
            server_url: None,
            edge_name: None,
            labels: HashMap::new(),
            version: None,
            hostname: None,
            ip_address: None,
            heartbeat_interval: Duration::from_secs(30),
            timeout: Duration::from_secs(30),
            reconnect_wait: Duration::from_secs(2),
            max_reconnects: -1,
        }
    }
}

// ============================
// Center Client Configuration
// ============================

/// Configuration for CenterClient.
#[derive(Debug, Clone)]
pub struct CenterConfig {
    /// dinkd server URL (e.g., "nats://localhost:4222").
    pub server_url: String,
    /// App API key for authentication.
    pub api_key: Option<String>,
    /// App ID (optional, defaults to "platform").
    pub app_id: Option<String>,
    /// Request timeout (default: 30s).
    pub timeout: Duration,
}

impl Default for CenterConfig {
    fn default() -> Self {
        Self {
            server_url: String::new(),
            api_key: None,
            app_id: None,
            timeout: Duration::from_secs(30),
        }
    }
}

// ============================
// Connection Quality Types
// ============================

/// Connection quality level.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ConnectionQualityLevel {
    Excellent,
    Good,
    Fair,
    Poor,
    Unknown,
}

/// Connection state.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ConnectionState {
    Connecting,
    Connected,
    Reconnecting,
    Disconnected,
}

/// Connection quality metrics.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct ConnectionQuality {
    pub state: ConnectionState,
    pub latency_ms: Option<f64>,
    pub avg_latency_ms: Option<f64>,
    pub messages_sent_per_second: f64,
    pub messages_received_per_second: f64,
    pub total_messages_sent: u64,
    pub total_messages_received: u64,
    pub last_ping_at: Option<u64>,
    pub quality_level: ConnectionQualityLevel,
}

// ============================
// Edge Discovery Types
// ============================

/// Information about a discovered edge.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct EdgeInfo {
    pub id: String,
    pub name: String,
    #[serde(default)]
    pub app_id: String,
    pub status: String,
    #[serde(default)]
    pub labels: HashMap<String, String>,
    #[serde(default)]
    pub services: Vec<String>,
    pub groups: Option<Vec<String>>,
}

/// Options for discovering edges.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct DiscoverOptions {
    pub service_name: Option<String>,
    pub labels: Option<HashMap<String, String>>,
    pub online_only: Option<bool>,
}

/// Options for RPC calls.
#[derive(Debug, Clone)]
pub struct CallOptions {
    pub timeout: Option<Duration>,
    pub retries: Option<u32>,
    pub retry_delay: Option<Duration>,
}

// ============================
// Key Management Types
// ============================

/// Type of API key.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum KeyType {
    Edge,
    Center,
    Client,
    Admin,
    AppMaster,
    SyncKey,
}

/// Status of an API key.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum KeyStatus {
    Active,
    Expired,
    Revoked,
    Disabled,
}

/// Resource restrictions for a key.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct KeyResources {
    pub services: Option<Vec<String>>,
    pub streams: Option<Vec<String>>,
    pub kv_buckets: Option<Vec<String>>,
    pub events: Option<Vec<String>>,
}

/// Metadata about an API key.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct KeyMetadata {
    pub id: String,
    pub app_id: String,
    pub name: String,
    #[serde(rename = "type")]
    pub key_type: KeyType,
    pub edge_id: Option<String>,
    #[serde(default)]
    pub scopes: Vec<String>,
    pub resources: Option<KeyResources>,
    pub labels: Option<HashMap<String, String>>,
    pub status: KeyStatus,
    pub created_at: String,
    pub expires_at: Option<String>,
    pub last_used_at: Option<String>,
    pub revoked_at: Option<String>,
    pub revoke_reason: Option<String>,
    pub groups: Option<Vec<String>>,
}

/// Options for creating an API key.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct CreateKeyOptions {
    pub name: String,
    #[serde(rename = "type")]
    pub key_type: KeyType,
    pub edge_id: Option<String>,
    pub scopes: Option<Vec<String>>,
    pub scope_bundle: Option<String>,
    pub expires_in: Option<String>,
    pub labels: Option<HashMap<String, String>>,
    pub groups: Option<Vec<String>>,
}

/// Result from creating an API key.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct CreateKeyResult {
    pub key: KeyMetadata,
    pub api_key: String,
}

/// Options for listing keys.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct ListKeysOptions {
    #[serde(rename = "type")]
    pub key_type: Option<KeyType>,
    pub labels: Option<HashMap<String, String>>,
}

// ============================
// Wire Protocol Envelopes
// ============================

/// Platform response envelope (management APIs).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PlatformResponse<T> {
    pub success: bool,
    #[serde(default)]
    pub data: Option<T>,
    #[serde(default)]
    pub error: Option<ErrorPayload>,
}

/// Service response envelope (edge→caller).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServiceResponse<T> {
    pub success: bool,
    #[serde(default)]
    pub result: Option<T>,
    #[serde(default)]
    pub error: Option<ErrorPayload>,
}

/// Error payload in responses.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ErrorPayload {
    pub code: String,
    pub message: String,
}

/// Stream initiation request.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StreamRequest<T> {
    pub reply_subject: String,
    pub data: T,
}

/// Stream acknowledgment from edge.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct StreamAck {
    pub status: String,
    pub cancel_subject: String,
}

// ============================
// Presence Protocol Types
// ============================

/// Edge registration payload.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct RegisterPayload {
    pub name: String,
    #[serde(rename = "type")]
    pub edge_type: String,
    #[serde(default)]
    pub labels: HashMap<String, String>,
    #[serde(default)]
    pub version: String,
    #[serde(default)]
    pub hostname: String,
    #[serde(default)]
    pub ip_address: String,
}

/// Service registration payload.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct ServiceRegisterPayload {
    pub name: String,
    pub version: String,
    pub location: String,
    pub edge_id: String,
}

/// Service deregistration payload.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct ServiceDeregisterPayload {
    pub service_id: String,
}

/// Edge key parsed info.
#[derive(Debug, Clone)]
pub struct ParsedEdgeKey {
    pub app_id: String,
    pub edge_id: String,
    pub server_url: Option<String>,
    pub http_url: Option<String>,
    pub secret: String,
}

// ============================
// Webhook Types
// ============================

/// Metadata for a webhook-annotated method.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct WebhookMethodMeta {
    pub method: String,
    pub secret_mode: String,
    pub description: String,
}

/// A webhook registration returned by the platform.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct WebhookRegistration {
    pub id: String,
    pub edge_id: String,
    pub service: String,
    pub method: String,
    pub app_id: String,
    #[serde(default)]
    pub secret: String,
    #[serde(default)]
    pub secret_mode: String,
    #[serde(default)]
    pub description: String,
    #[serde(default)]
    pub labels: HashMap<String, String>,
    #[serde(default)]
    pub call_count: i64,
    pub last_used_at: Option<String>,
    pub created_at: Option<String>,
}

/// Metadata extracted from the RPC request envelope.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct RequestMetadata {
    #[serde(default)]
    pub request_id: Option<String>,
    #[serde(default)]
    pub source_edge: Option<String>,
    #[serde(default)]
    pub labels: HashMap<String, String>,
    #[serde(default)]
    pub timestamp: Option<u64>,
}

/// Context provided to service handlers with envelope metadata.
#[derive(Debug, Clone, Default)]
pub struct RequestContext {
    pub metadata: RequestMetadata,
}