runbeam-sdk 0.11.0

Rust SDK for integrating with the Runbeam Cloud API, providing JWT validation, API client, and generic secure token storage
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
use serde::{Deserialize, Serialize};

// ========================================================================================
// SHARED CONFIGURATION STRUCTURES
// ========================================================================================

/// Normalized connection configuration shared across peers, targets, endpoints, and backends
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConnectionConfig {
    #[serde(default)]
    pub host: Option<String>,
    #[serde(default)]
    pub port: Option<u16>,
    #[serde(default)]
    pub protocol: Option<String>,
    #[serde(default)]
    pub base_path: Option<String>,
}


// ========================================================================================
// PAGINATED RESPONSE STRUCTURES
// ========================================================================================

/// Paginated response wrapper
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PaginatedResponse<T> {
    pub data: Vec<T>,
    #[serde(default)]
    pub links: Option<PaginationLinks>,
    #[serde(default)]
    pub meta: Option<PaginationMeta>,
}

/// Pagination links
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PaginationLinks {
    pub first: Option<String>,
    pub last: Option<String>,
    pub prev: Option<String>,
    pub next: Option<String>,
}

/// Pagination metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PaginationMeta {
    pub current_page: u32,
    pub from: Option<u32>,
    pub last_page: u32,
    #[serde(default)]
    pub links: Option<Vec<serde_json::Value>>, // Laravel pagination links array
    pub path: Option<String>,
    pub per_page: u32,
    pub to: Option<u32>,
    pub total: u32,
}

/// Single resource response wrapper
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResourceResponse<T> {
    pub data: T,
}

// ========================================================================================
// RESOURCE TYPES
// ========================================================================================

/// Peer resource - represents external systems that send requests to Harmony
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Peer {
    #[serde(rename = "type")]
    pub resource_type: String,
    #[serde(default)]
    pub id: Option<String>,
    pub code: String,
    pub name: String,
    pub team_id: String,
    pub gateway_id: Option<String>,
    #[serde(default)]
    pub connection: Option<ConnectionConfig>,
    /// Protocol type - the type of protocol used by this peer (http, https, dicom, etc.)
    #[serde(default)]
    pub protocol: Option<String>,
    #[serde(default)]
    pub description: Option<String>,
    #[serde(default)]
    pub enabled: Option<bool>,
    /// Reference to an authentication defined in main config
    #[serde(default)]
    pub authentication: Option<String>,
    #[serde(default)]
    pub tags: Option<Vec<String>>,
    #[serde(default)]
    pub timeout_secs: Option<u32>,
    #[serde(default)]
    pub max_retries: Option<u32>,
    #[serde(default)]
    pub created_at: Option<String>,
    #[serde(default)]
    pub updated_at: Option<String>,
}

/// Target resource - represents external systems that receive requests from Harmony
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Target {
    #[serde(rename = "type")]
    pub resource_type: String,
    #[serde(default)]
    pub id: Option<String>,
    pub code: String,
    pub name: String,
    pub team_id: String,
    pub gateway_id: Option<String>,
    #[serde(default)]
    pub connection: Option<ConnectionConfig>,
    /// Protocol type - the type of protocol used by this target (http, https, dicom, etc.)
    #[serde(default)]
    pub protocol: Option<String>,
    #[serde(default)]
    pub description: Option<String>,
    #[serde(default)]
    pub enabled: Option<bool>,
    /// Reference to an authentication defined in main config
    #[serde(default)]
    pub authentication: Option<String>,
    #[serde(default)]
    pub tags: Option<Vec<String>>,
    #[serde(default)]
    pub timeout_secs: Option<u32>,
    #[serde(default)]
    pub max_retries: Option<u32>,
    #[serde(default)]
    pub created_at: Option<String>,
    #[serde(default)]
    pub updated_at: Option<String>,
}

/// Gateway resource
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Gateway {
    #[serde(rename = "type")]
    pub resource_type: String,
    #[serde(default)]
    pub id: Option<String>,
    pub code: String,
    pub name: String,
    pub team_id: String,
    pub enabled: Option<bool>,
    #[serde(default)]
    pub pipelines_path: Option<String>,
    #[serde(default)]
    pub transforms_path: Option<String>,
    #[serde(default)]
    pub jwks_cache_duration_hours: Option<u32>,
    #[serde(default)]
    pub management_enabled: Option<bool>,
    #[serde(default)]
    pub management_base_path: Option<String>,
    #[serde(default)]
    pub management_network_id: Option<String>,
    #[serde(default)]
    pub dns: Option<Vec<String>>,
    #[serde(default)]
    pub settings: Option<serde_json::Value>,
    #[serde(default)]
    pub created_at: Option<String>,
    #[serde(default)]
    pub updated_at: Option<String>,
}

/// User who authorized a gateway
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuthorizedByInfo {
    pub id: String,
    pub name: String,
    pub email: String,
}

/// Service resource
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Service {
    #[serde(rename = "type")]
    pub resource_type: String,
    #[serde(default)]
    pub id: Option<String>,
    pub code: String,
    pub name: String,
    pub team_id: String,
    pub gateway_id: String,
    #[serde(default)]
    pub description: Option<String>,
    #[serde(default)]
    pub created_at: Option<String>,
    #[serde(default)]
    pub updated_at: Option<String>,
}

/// Endpoint resource
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Endpoint {
    #[serde(rename = "type")]
    pub resource_type: String,
    #[serde(default)]
    pub id: Option<String>,
    pub code: String,
    pub name: String,
    pub team_id: String,
    pub gateway_id: Option<String>,
    #[serde(default)]
    pub service_id: Option<String>,
    /// Reference to a peer defined in main config (inherits peer's connection settings)
    #[serde(default)]
    pub peer_ref: Option<String>,
    /// Connection configuration (overrides peer_ref settings if both specified)
    #[serde(default)]
    pub connection: Option<ConnectionConfig>,
    /// Authentication reference (overrides peer_ref settings if both specified)
    #[serde(default)]
    pub authentication: Option<String>,
    #[serde(default)]
    pub path: Option<String>,
    #[serde(default)]
    pub methods: Option<Vec<String>>,
    #[serde(default)]
    pub description: Option<String>,
    /// Service-specific options (highest precedence)
    #[serde(default)]
    pub options: Option<serde_json::Value>,
    #[serde(default)]
    pub created_at: Option<String>,
    #[serde(default)]
    pub updated_at: Option<String>,
}

/// Backend resource
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Backend {
    #[serde(rename = "type")]
    pub resource_type: String,
    #[serde(default)]
    pub id: Option<String>,
    pub code: String,
    pub name: String,
    pub team_id: String,
    pub gateway_id: Option<String>,
    #[serde(default)]
    pub service_id: Option<String>,
    /// Reference to a target defined in main config (inherits target's connection settings)
    #[serde(default)]
    pub target_ref: Option<String>,
    /// Connection configuration (overrides target_ref settings if both specified)
    #[serde(default)]
    pub connection: Option<ConnectionConfig>,
    /// Authentication reference (overrides target_ref settings if both specified)
    #[serde(default)]
    pub authentication: Option<String>,
    #[serde(default)]
    pub url: Option<String>,
    #[serde(default)]
    pub timeout_seconds: Option<u32>,
    /// Maximum retry attempts (overrides target_ref.max_retries)
    #[serde(default)]
    pub max_retries: Option<u32>,
    /// Service-specific options (highest precedence)
    #[serde(default)]
    pub options: Option<serde_json::Value>,
    #[serde(default)]
    pub created_at: Option<String>,
    #[serde(default)]
    pub updated_at: Option<String>,
}

/// Pipeline resource
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Pipeline {
    #[serde(rename = "type")]
    pub resource_type: String,
    #[serde(default)]
    pub id: Option<String>,
    pub code: String,
    pub name: String,
    pub description: String,
    pub team_id: String,
    pub gateway_id: Option<String>,
    #[serde(default)]
    pub networks: Option<Vec<String>>,
    #[serde(default)]
    pub endpoints: Option<serde_json::Value>,
    #[serde(default)]
    pub backends: Option<serde_json::Value>,
    #[serde(default)]
    pub middleware: Option<serde_json::Value>,
    #[serde(default)]
    pub created_at: Option<String>,
    #[serde(default)]
    pub updated_at: Option<String>,
}

/// Middleware resource  
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Middleware {
    #[serde(rename = "type")]
    pub resource_type: String,
    #[serde(default)]
    pub id: Option<String>,
    pub code: String,
    pub name: String,
    pub team_id: String,
    pub middleware_type: String,
    /// Reference to an authentication defined in main config
    #[serde(default)]
    pub authentication: Option<String>,
    #[serde(default)]
    pub options: Option<serde_json::Value>,
    #[serde(default)]
    pub created_at: Option<String>,
    #[serde(default)]
    pub updated_at: Option<String>,
}

/// Transform resource
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Transform {
    #[serde(rename = "type")]
    pub resource_type: String,
    #[serde(default)]
    pub id: Option<String>,
    pub code: String,
    pub name: String,
    pub team_id: String,
    pub gateway_id: String,
    #[serde(default)]
    pub options: Option<TransformOptions>,
    #[serde(default)]
    pub created_at: Option<String>,
    #[serde(default)]
    pub updated_at: Option<String>,
}

/// Transform options
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransformOptions {
    pub instructions: Option<String>,
}

/// Array of rule IDs for a policy.
///
/// In harmony-dsl v1.7.0+, policies reference rules by ID using a simple array of strings.
/// Each ID in this array corresponds to a top-level `[rules.*]` table definition.
///
/// # Example
///
/// ```json
/// ["rate_limit_rule_1", "rate_limit_rule_2", "allow_internal"]
/// ```
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct PolicyRules(pub Vec<String>);

impl PolicyRules {
    /// Create a new PolicyRules from a vector of rule IDs
    pub fn new(rules: Vec<String>) -> Self {
        PolicyRules(rules)
    }

    /// Get a reference to the underlying rule IDs
    pub fn rules(&self) -> &[String] {
        &self.0
    }

    /// Convert into the underlying vector of rule IDs
    pub fn into_inner(self) -> Vec<String> {
        self.0
    }
}

/// Rule resource
///
/// Represents a rule definition that can be referenced by policies.
/// Rules define access control logic with configurable options.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Rule {
    #[serde(rename = "type")]
    pub resource_type: String,
    pub id: String,
    pub code: String,
    #[serde(default)]
    pub name: Option<String>,
    pub rule_type: String,
    pub team_id: String,
    pub gateway_id: String,
    #[serde(default)]
    pub weight: Option<u32>,
    #[serde(default)]
    pub enabled: Option<bool>,
    #[serde(default)]
    pub options: Option<serde_json::Value>,
    #[serde(default)]
    pub created_at: Option<String>,
    #[serde(default)]
    pub updated_at: Option<String>,
}

/// Policy resource
///
/// Represents a policy that contains references to rules.
/// In v1.7.0+, rules are defined as top-level resources and referenced by ID.
///
/// # Example (v1.7.0 format)
///
/// ```json
/// {
///   "type": "policy",
///   "id": "policy-123",
///   "code": "rate-limit",
///   "name": "Rate Limiting Policy",
///   "enabled": 1,
///   "team_id": "team-456",
///   "gateway_id": "gateway-789",
///   "rules": ["rate_limit_rule_1", "rate_limit_rule_2"],
///   "created_at": "2024-01-01T00:00:00Z",
///   "updated_at": "2024-01-01T00:00:00Z"
/// }
/// ```
///
/// # Breaking Change (v1.6.0 → v1.7.0)
///
/// In v1.7.0, the `rules` field format changed from nested objects to an array of rule ID strings.
/// This aligns with the new flat/reusable policies and rules structure in harmony-dsl.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Policy {
    #[serde(rename = "type")]
    pub resource_type: String,
    #[serde(default)]
    pub id: Option<String>,
    pub code: String,
    pub name: String,
    pub enabled: u32,
    pub team_id: String,
    pub gateway_id: String,
    /// Array of rule IDs referenced by this policy.
    ///
    /// In v1.7.0+, rules are defined as top-level `[rules.*]` tables and referenced by ID.
    /// Each ID in this array should correspond to a rule defined at the top level.
    /// Rules are represented as a typed array of string IDs for better type safety.
    #[serde(default)]
    pub rules: Option<PolicyRules>,
    #[serde(default)]
    pub created_at: Option<String>,
    #[serde(default)]
    pub updated_at: Option<String>,
}

/// Network resource
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Network {
    #[serde(rename = "type")]
    pub resource_type: String,
    #[serde(default)]
    pub id: Option<String>,
    pub code: String,
    pub name: String,
    pub team_id: String,
    pub gateway_id: Option<String>,
    pub enable_wireguard: bool,
    #[serde(default)]
    pub interface: Option<String>,
    #[serde(default, alias = "http")]
    pub tcp_config: Option<TcpConfig>,
    #[serde(default)]
    pub created_at: Option<String>,
    #[serde(default)]
    pub updated_at: Option<String>,
}

/// TCP configuration for network - used by all protocol adapters (HTTP, DIMSE, etc.)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TcpConfig {
    pub bind_address: Option<String>,
    pub bind_port: Option<u16>,
}

/// Runbeam Cloud integration configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Runbeam {
    #[serde(rename = "type")]
    pub resource_type: String,
    #[serde(default)]
    pub id: Option<String>,
    pub code: String,
    pub name: String,
    pub team_id: String,
    pub gateway_id: Option<String>,
    #[serde(default)]
    pub enabled: Option<bool>,
    #[serde(default)]
    pub cloud_api_base_url: Option<String>,
    #[serde(default)]
    pub poll_interval_secs: Option<u32>,
    #[serde(default)]
    pub created_at: Option<String>,
    #[serde(default)]
    pub updated_at: Option<String>,
}

/// Authentication resource
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Authentication {
    #[serde(rename = "type")]
    pub resource_type: String,
    #[serde(default)]
    pub id: Option<String>,
    pub code: Option<String>,
    pub name: String,
    pub team_id: Option<String>,
    pub gateway_id: Option<String>,
    pub method: String,
    #[serde(default)]
    pub options: Option<serde_json::Value>,
    #[serde(default)]
    pub created_at: Option<String>,
    #[serde(default)]
    pub updated_at: Option<String>,
}

/// Full gateway configuration (for downloading complete config)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GatewayConfiguration {
    pub gateway: Gateway,
    #[serde(default)]
    pub peers: Vec<Peer>,
    #[serde(default)]
    pub targets: Vec<Target>,
    #[serde(default)]
    pub services: Vec<Service>,
    #[serde(default)]
    pub endpoints: Vec<Endpoint>,
    #[serde(default)]
    pub backends: Vec<Backend>,
    #[serde(default)]
    pub pipelines: Vec<Pipeline>,
    #[serde(default)]
    pub middlewares: Vec<Middleware>,
    #[serde(default)]
    pub transforms: Vec<Transform>,
    #[serde(default)]
    pub policies: Vec<Policy>,
    #[serde(default)]
    pub rules: Vec<Rule>,
    #[serde(default)]
    pub networks: Vec<Network>,
    #[serde(default)]
    pub runbeam: Option<Runbeam>,
    #[serde(default)]
    pub authentications: Vec<Authentication>,
}

/// Change resource for configuration change tracking (API v1.0)
///
/// This represents a configuration change that needs to be applied to a gateway.
/// The API returns two different levels of detail:
///
/// 1. ChangeMetadata (list view) - returned from `/api/harmony/changes` endpoints
///    Contains: id, status, type, gateway_id, created_at
///
/// 2. ChangeResource (detail view) - returned from `/api/harmony/changes/{change}` endpoint  
///    Contains all metadata fields plus: pipeline_id, toml_config, metadata, timestamps, error info
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Change {
    pub id: String,
    #[serde(default)]
    pub status: Option<String>,
    #[serde(rename = "type")]
    pub resource_type: String,
    pub gateway_id: String,
    #[serde(default)]
    pub pipeline_id: Option<String>,
    /// TOML configuration content (only present in detail view)
    #[serde(default)]
    pub toml_config: Option<String>,
    /// Additional metadata (only present in detail view)
    #[serde(default)]
    pub metadata: Option<serde_json::Value>,
    pub created_at: String,
    #[serde(default)]
    pub acknowledged_at: Option<String>,
    #[serde(default)]
    pub applied_at: Option<String>,
    #[serde(default)]
    pub failed_at: Option<String>,
    #[serde(default)]
    pub error_message: Option<String>,
    #[serde(default)]
    pub error_details: Option<serde_json::Value>,
}

/// Response from the base URL discovery endpoint
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BaseUrlResponse {
    /// Base URL for the Harmony API (e.g., https://runbeam.lndo.site/api)
    pub base_url: String,
    /// Optional path for changes API (e.g., "/")
    #[serde(default)]
    pub changes_path: Option<String>,
    /// Optional fully resolved URL (base_url + changes_path)
    #[serde(default)]
    pub full_url: Option<String>,
}

/// Request payload for acknowledging multiple changes
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AcknowledgeChangesRequest {
    pub change_ids: Vec<String>,
}

/// Request payload for reporting a failed change
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChangeFailedRequest {
    pub error: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub details: Option<Vec<String>>,
}

/// Response from acknowledging multiple changes
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AcknowledgeChangesResponse {
    pub acknowledged: Vec<String>,
    pub failed: Vec<String>,
}

/// Response from marking a change as applied or failed
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChangeStatusResponse {
    pub success: bool,
    pub message: String,
}

/// Type alias for change applied response
pub type ChangeAppliedResponse = ChangeStatusResponse;

/// Type alias for change failed response  
pub type ChangeFailedResponse = ChangeStatusResponse;

// ========================================================================================
// MESH AUTHENTICATION
// ========================================================================================

/// Request payload for mesh token
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MeshTokenRequest {
    /// The mesh ID to authenticate against
    pub mesh_id: String,
    /// The destination URL the token is being requested for
    pub destination_url: String,
}

/// Response from mesh token request
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MeshTokenResponse {
    /// The signed JWT token
    pub token: String,
    /// When the token expires (ISO 8601 format)
    pub expires_at: String,
    /// The mesh ID this token is valid for
    pub mesh_id: String,
}