asterisk-rs-ari 0.8.0

Async Rust client for the Asterisk REST Interface (ARI)
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
//! channel operations — originate, answer, hangup, dtmf, hold, mute, etc.

use std::collections::HashMap;

use crate::client::{AriClient, url_encode};
use crate::error::Result;
use crate::event::{Channel, LiveRecording, Playback};
use crate::resources::playback::PlaybackHandle;
use crate::resources::recording::RecordingHandle;

/// parameters for originating a new channel
#[derive(Debug, Clone, serde::Serialize)]
#[must_use]
#[non_exhaustive]
pub struct OriginateParams {
    endpoint: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    extension: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    context: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    priority: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    app: Option<String>,
    #[serde(rename = "appArgs", skip_serializing_if = "Option::is_none")]
    app_args: Option<String>,
    #[serde(rename = "callerId", skip_serializing_if = "Option::is_none")]
    caller_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    timeout: Option<i32>,
    #[serde(rename = "channelId", skip_serializing_if = "Option::is_none")]
    pub(crate) channel_id: Option<String>,
    #[serde(rename = "otherChannelId", skip_serializing_if = "Option::is_none")]
    other_channel_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    originator: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    formats: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    variables: Option<HashMap<String, String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    label: Option<String>,
}

impl OriginateParams {
    /// Create originate parameters with the required endpoint.
    pub fn new(endpoint: impl Into<String>) -> Self {
        Self {
            endpoint: endpoint.into(),
            extension: None,
            context: None,
            priority: None,
            app: None,
            app_args: None,
            caller_id: None,
            timeout: None,
            channel_id: None,
            other_channel_id: None,
            originator: None,
            formats: None,
            variables: None,
            label: None,
        }
    }

    pub fn extension(mut self, value: impl Into<String>) -> Self {
        self.extension = Some(value.into());
        self
    }
    pub fn context(mut self, value: impl Into<String>) -> Self {
        self.context = Some(value.into());
        self
    }
    pub fn priority(mut self, value: i64) -> Self {
        self.priority = Some(value);
        self
    }
    pub fn app(mut self, value: impl Into<String>) -> Self {
        self.app = Some(value.into());
        self
    }
    pub fn app_args(mut self, value: impl Into<String>) -> Self {
        self.app_args = Some(value.into());
        self
    }
    pub fn caller_id(mut self, value: impl Into<String>) -> Self {
        self.caller_id = Some(value.into());
        self
    }
    pub fn timeout(mut self, value: i32) -> Self {
        self.timeout = Some(value);
        self
    }
    pub fn channel_id(mut self, value: impl Into<String>) -> Self {
        self.channel_id = Some(value.into());
        self
    }
    pub fn other_channel_id(mut self, value: impl Into<String>) -> Self {
        self.other_channel_id = Some(value.into());
        self
    }
    pub fn originator(mut self, value: impl Into<String>) -> Self {
        self.originator = Some(value.into());
        self
    }
    pub fn formats(mut self, value: impl Into<String>) -> Self {
        self.formats = Some(value.into());
        self
    }
    pub fn variables(mut self, value: HashMap<String, String>) -> Self {
        self.variables = Some(value);
        self
    }
    pub fn label(mut self, value: impl Into<String>) -> Self {
        self.label = Some(value.into());
        self
    }
}

/// parameters for starting an external media session
#[derive(Debug, Clone, serde::Serialize)]
#[must_use]
#[non_exhaustive]
pub struct ExternalMediaParams {
    app: String,
    external_host: String,
    format: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    encapsulation: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    transport: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    connection_type: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    direction: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    data: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    transport_data: Option<String>,
    #[serde(rename = "channelId", skip_serializing_if = "Option::is_none")]
    channel_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    variables: Option<HashMap<String, String>>,
}

impl ExternalMediaParams {
    /// create params with required fields; optional fields default to none
    pub fn new(
        app: impl Into<String>,
        external_host: impl Into<String>,
        format: impl Into<String>,
    ) -> Self {
        Self {
            app: app.into(),
            external_host: external_host.into(),
            format: format.into(),
            encapsulation: None,
            transport: None,
            connection_type: None,
            direction: None,
            data: None,
            transport_data: None,
            channel_id: None,
            variables: None,
        }
    }

    /// create params for chan_websocket's JSON control protocol
    ///
    /// Asterisk defaults chan_websocket control messages to plaintext. This
    /// constructor selects the JSON protocol required by [`crate::media::MediaChannel`].
    pub fn websocket_json(
        app: impl Into<String>,
        external_host: impl Into<String>,
        format: impl Into<String>,
    ) -> Self {
        Self::new(app, external_host, format)
            .encapsulation("none")
            .transport("websocket")
            .transport_data("f(json)")
    }

    /// set the encapsulation type (e.g. `rtp`)
    pub fn encapsulation(mut self, encapsulation: impl Into<String>) -> Self {
        self.encapsulation = Some(encapsulation.into());
        self
    }

    /// set the transport protocol (e.g. `udp`)
    pub fn transport(mut self, transport: impl Into<String>) -> Self {
        self.transport = Some(transport.into());
        self
    }

    /// set the connection type
    pub fn connection_type(mut self, connection_type: impl Into<String>) -> Self {
        self.connection_type = Some(connection_type.into());
        self
    }

    /// set the media direction (e.g. `both`, `in`, `out`)
    pub fn direction(mut self, direction: impl Into<String>) -> Self {
        self.direction = Some(direction.into());
        self
    }

    /// set the arbitrary data passed to the external-media channel
    pub fn data(mut self, data: impl Into<String>) -> Self {
        self.data = Some(data.into());
        self
    }

    /// set transport-specific dial-string data
    ///
    /// For chan_websocket, use `f(json)` to select JSON control messages.
    pub fn transport_data(mut self, transport_data: impl Into<String>) -> Self {
        self.transport_data = Some(transport_data.into());
        self
    }

    /// set a specific channel id
    pub fn channel_id(mut self, channel_id: impl Into<String>) -> Self {
        self.channel_id = Some(channel_id.into());
        self
    }

    /// set channel variables
    pub fn variables(mut self, variables: HashMap<String, String>) -> Self {
        self.variables = Some(variables);
        self
    }
}

/// ari channel variable response
#[derive(Debug, Clone, serde::Deserialize)]
#[non_exhaustive]
pub struct Variable {
    pub value: String,
}

/// handle to an ari channel, bundling channel id with client reference
#[derive(Debug, Clone)]
pub struct ChannelHandle {
    id: String,
    client: AriClient,
}

impl ChannelHandle {
    /// create a channel handle for the given id
    pub fn new(id: impl Into<String>, client: AriClient) -> Self {
        Self {
            id: id.into(),
            client,
        }
    }

    /// channel id
    pub fn id(&self) -> &str {
        &self.id
    }

    /// answer the channel
    pub async fn answer(&self) -> Result<()> {
        self.client
            .post_empty(&format!("/channels/{}/answer", url_encode(&self.id)))
            .await
    }

    /// hang up the channel with an optional reason
    pub async fn hangup(&self, reason: Option<&str>) -> Result<()> {
        let path = match reason {
            Some(r) => format!(
                "/channels/{}?reason={}",
                url_encode(&self.id),
                url_encode(r)
            ),
            None => format!("/channels/{}", url_encode(&self.id)),
        };
        self.client.delete(&path).await
    }

    /// play media on the channel
    pub async fn play(&self, media: &str) -> Result<Playback> {
        self.client
            .post(
                &format!("/channels/{}/play", url_encode(&self.id)),
                &serde_json::json!({"media": media}),
            )
            .await
    }

    /// Play media and return the lifecycle handle for the created playback.
    pub async fn play_handle(&self, media: &str) -> Result<PlaybackHandle> {
        let playback = self.play(media).await?;
        Ok(PlaybackHandle::new(playback.id, self.client.clone()))
    }

    /// start recording on the channel
    pub async fn record(&self, name: &str, format: &str) -> Result<LiveRecording> {
        self.client
            .post(
                &format!("/channels/{}/record", url_encode(&self.id)),
                &serde_json::json!({"name": name, "format": format}),
            )
            .await
    }

    /// Start recording and return the lifecycle handle for the created recording.
    pub async fn record_handle(&self, name: &str, format: &str) -> Result<RecordingHandle> {
        let recording = self.record(name, format).await?;
        Ok(RecordingHandle::new(recording.name, self.client.clone()))
    }

    /// mute the channel, optionally specifying direction (both, in, out)
    pub async fn mute(&self, direction: Option<&str>) -> Result<()> {
        let path = match direction {
            Some(d) => format!(
                "/channels/{}/mute?direction={}",
                url_encode(&self.id),
                url_encode(d)
            ),
            None => format!("/channels/{}/mute", url_encode(&self.id)),
        };
        self.client.post_empty(&path).await
    }

    /// unmute the channel, optionally specifying direction
    pub async fn unmute(&self, direction: Option<&str>) -> Result<()> {
        let path = match direction {
            Some(d) => format!(
                "/channels/{}/mute?direction={}",
                url_encode(&self.id),
                url_encode(d)
            ),
            None => format!("/channels/{}/mute", url_encode(&self.id)),
        };
        self.client.delete(&path).await
    }

    /// place the channel on hold
    pub async fn hold(&self) -> Result<()> {
        self.client
            .post_empty(&format!("/channels/{}/hold", url_encode(&self.id)))
            .await
    }

    /// remove the channel from hold
    pub async fn unhold(&self) -> Result<()> {
        self.client
            .delete(&format!("/channels/{}/hold", url_encode(&self.id)))
            .await
    }

    /// send dtmf digits to the channel
    pub async fn send_dtmf(&self, dtmf: &str) -> Result<()> {
        self.client
            .post_empty(&format!(
                "/channels/{}/dtmf?dtmf={}",
                url_encode(&self.id),
                url_encode(dtmf)
            ))
            .await
    }

    /// get a channel variable
    pub async fn get_variable(&self, name: &str) -> Result<Variable> {
        self.client
            .get(&format!(
                "/channels/{}/variable?variable={}",
                url_encode(&self.id),
                url_encode(name)
            ))
            .await
    }

    /// set a channel variable
    pub async fn set_variable(&self, name: &str, value: &str) -> Result<()> {
        self.client
            .post_empty(&format!(
                "/channels/{}/variable?variable={}&value={}",
                url_encode(&self.id),
                url_encode(name),
                url_encode(value)
            ))
            .await
    }

    /// continue the channel in the dialplan
    pub async fn continue_in_dialplan(
        &self,
        context: Option<&str>,
        extension: Option<&str>,
        priority: Option<i64>,
    ) -> Result<()> {
        let mut path = format!("/channels/{}/continue", url_encode(&self.id));
        let mut params = Vec::new();
        if let Some(c) = context {
            params.push(format!("context={}", url_encode(c)));
        }
        if let Some(e) = extension {
            params.push(format!("extension={}", url_encode(e)));
        }
        if let Some(p) = priority {
            params.push(format!("priority={p}"));
        }
        if !params.is_empty() {
            path.push('?');
            path.push_str(&params.join("&"));
        }
        self.client.post_empty(&path).await
    }

    /// snoop on the channel — spy and/or whisper
    pub async fn snoop(
        &self,
        spy: Option<&str>,
        whisper: Option<&str>,
        app: &str,
    ) -> Result<Channel> {
        let mut params = vec![format!("app={}", url_encode(app))];
        if let Some(s) = spy {
            params.push(format!("spy={}", url_encode(s)));
        }
        if let Some(w) = whisper {
            params.push(format!("whisper={}", url_encode(w)));
        }
        let query = params.join("&");
        self.client
            .post(
                &format!("/channels/{}/snoop?{}", url_encode(&self.id), query),
                &serde_json::json!({}),
            )
            .await
    }

    /// Create a snoop channel and return its lifecycle handle.
    pub async fn snoop_handle(
        &self,
        spy: Option<&str>,
        whisper: Option<&str>,
        app: &str,
    ) -> Result<ChannelHandle> {
        let channel = self.snoop(spy, whisper, app).await?;
        Ok(ChannelHandle::new(channel.id, self.client.clone()))
    }

    /// redirect the channel to a new endpoint
    pub async fn redirect(&self, endpoint: &str) -> Result<()> {
        self.client
            .post_empty(&format!(
                "/channels/{}/redirect?endpoint={}",
                url_encode(&self.id),
                url_encode(endpoint),
            ))
            .await
    }

    /// start ringing on the channel
    pub async fn ring(&self) -> Result<()> {
        self.client
            .post_empty(&format!("/channels/{}/ring", url_encode(&self.id)))
            .await
    }

    /// stop ringing on the channel
    pub async fn ring_stop(&self) -> Result<()> {
        self.client
            .delete(&format!("/channels/{}/ring", url_encode(&self.id)))
            .await
    }

    /// start silence on the channel
    pub async fn start_silence(&self) -> Result<()> {
        self.client
            .post_empty(&format!("/channels/{}/silence", url_encode(&self.id)))
            .await
    }

    /// stop silence on the channel
    pub async fn stop_silence(&self) -> Result<()> {
        self.client
            .delete(&format!("/channels/{}/silence", url_encode(&self.id)))
            .await
    }

    /// play media on the channel with additional options
    pub async fn play_with_id(&self, playback_id: &str, media: &str) -> Result<Playback> {
        self.client
            .post(
                &format!(
                    "/channels/{}/play/{}",
                    url_encode(&self.id),
                    url_encode(playback_id)
                ),
                &serde_json::json!({"media": media}),
            )
            .await
    }

    /// Play media with a caller-selected ID and return its lifecycle handle.
    pub async fn play_with_id_handle(
        &self,
        playback_id: &str,
        media: &str,
    ) -> Result<PlaybackHandle> {
        self.play_with_id(playback_id, media).await?;
        Ok(PlaybackHandle::new(playback_id, self.client.clone()))
    }

    /// dial a created channel
    pub async fn dial(&self, caller: Option<&str>, timeout: Option<i32>) -> Result<()> {
        let mut params = Vec::new();
        if let Some(c) = caller {
            params.push(format!("caller={}", url_encode(c)));
        }
        if let Some(t) = timeout {
            params.push(format!("timeout={t}"));
        }
        let query = if params.is_empty() {
            String::new()
        } else {
            format!("?{}", params.join("&"))
        };
        self.client
            .post_empty(&format!("/channels/{}/dial{}", url_encode(&self.id), query))
            .await
    }

    /// get rtp statistics for the channel
    pub async fn rtp_statistics(&self) -> Result<serde_json::Value> {
        self.client
            .get(&format!(
                "/channels/{}/rtp_statistics",
                url_encode(&self.id)
            ))
            .await
    }

    /// start an external media session
    pub async fn external_media(&self, params: &ExternalMediaParams) -> Result<Channel> {
        self.client.post("/channels/externalMedia", params).await
    }

    /// Start external media and return its lifecycle handle.
    pub async fn external_media_handle(
        &self,
        params: &ExternalMediaParams,
    ) -> Result<ChannelHandle> {
        let channel = self.external_media(params).await?;
        Ok(ChannelHandle::new(channel.id, self.client.clone()))
    }
}
/// list all active channels
pub async fn list(client: &AriClient) -> Result<Vec<Channel>> {
    client.get("/channels").await
}

/// get details for a specific channel
pub async fn get(client: &AriClient, channel_id: &str) -> Result<Channel> {
    client
        .get(&format!("/channels/{}", url_encode(channel_id)))
        .await
}

/// originate a new channel
pub async fn originate(client: &AriClient, params: &OriginateParams) -> Result<Channel> {
    client.post("/channels", params).await
}

/// Originate a channel and return its lifecycle handle.
pub async fn originate_handle(
    client: &AriClient,
    params: &OriginateParams,
) -> Result<ChannelHandle> {
    let channel = originate(client, params).await?;
    Ok(ChannelHandle::new(channel.id, client.clone()))
}

/// create a channel without dialing it
pub async fn create(client: &AriClient, endpoint: &str, app: &str) -> Result<Channel> {
    client
        .post(
            "/channels/create",
            &serde_json::json!({
                "endpoint": endpoint,
                "app": app,
            }),
        )
        .await
}

/// Create an undialed channel and return its lifecycle handle.
pub async fn create_handle(client: &AriClient, endpoint: &str, app: &str) -> Result<ChannelHandle> {
    let channel = create(client, endpoint, app).await?;
    Ok(ChannelHandle::new(channel.id, client.clone()))
}

/// start an external media session
pub async fn external_media(client: &AriClient, params: &ExternalMediaParams) -> Result<Channel> {
    client.post("/channels/externalMedia", params).await
}

/// Start external media and return its lifecycle handle.
pub async fn external_media_handle(
    client: &AriClient,
    params: &ExternalMediaParams,
) -> Result<ChannelHandle> {
    let channel = external_media(client, params).await?;
    Ok(ChannelHandle::new(channel.id, client.clone()))
}