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
use crate::endpoint::Endpoint;
use crate::error::ClientError;
use crate::Result;
use chrono::{DateTime, Utc};
use futures::TryStreamExt;
use hyper::{self, Body, Request, StatusCode};
use serde::{Deserialize, Serialize};
use serde_json::{from_slice, to_string, Value};
use std::fmt::{Display, Formatter};
use url::Url;

/// Represents the runtime status of an orchestration.
#[derive(Debug, Clone, Deserialize, PartialEq)]
pub enum OrchestrationRuntimeStatus {
    /// The orchestration is running.
    Running,
    /// The orchestration is pending.
    Pending,
    /// The orchestration has failed.
    Failed,
    /// The orchestration was canceled.
    Canceled,
    /// The orchestration was terminated.
    Terminated,
    /// The orchestration completed successfully.
    Completed,
}

impl Display for OrchestrationRuntimeStatus {
    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
        write!(f, "{:?}", self)
    }
}

/// Represents an orchestration history event.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct OrchestrationHistoryEvent {
    /// The event type.
    pub event_type: String,
    /// The orchestration status for the event.
    pub orchestration_status: Option<OrchestrationRuntimeStatus>,
    /// The function name for the event.
    pub function_name: Option<String>,
    /// The result (output) for the event.
    pub result: Option<Value>,
    /// The scheduled time for the event.
    pub scheduled_time: Option<DateTime<Utc>>,
    /// The timestamp for the event.
    pub timestamp: DateTime<Utc>,
}

/// Represents an orchestration's status.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OrchestrationStatus {
    /// The runtime status of the orchestration.
    pub runtime_status: OrchestrationRuntimeStatus,
    /// The input of the orchestration.
    pub input: Option<Value>,
    /// The custom status of the orchestration.
    pub custom_status: Option<Value>,
    /// The output of the orchestration.
    pub output: Option<Value>,
    /// The created time of the orchestration.
    pub created_time: DateTime<Utc>,
    /// The event history of the orchestration.
    pub history_events: Option<Vec<OrchestrationHistoryEvent>>,
}

/// Represents new orchestration data.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OrchestrationData {
    /// The orchestration instance id.
    #[serde(rename = "id")]
    pub instance_id: String,
    /// The instance status query URI (GET).
    pub status_query_get_uri: String,
    /// The send event URI (POST).
    pub send_event_post_uri: String,
    /// The terminate instance URI (POST).
    pub terminate_post_uri: String,
    /// The purge history URI (DELETE).
    pub purge_history_delete_uri: String,
    /// The rewind URI (POST).
    pub rewind_post_uri: Option<String>,
}

/// Represents the Durable Functions HTTP client.
pub struct Client {
    endpoint: Endpoint,
    client: hyper::Client<hyper::client::HttpConnector>,
}

impl Client {
    /// Creates a new client from the given status query URL.
    pub fn new(status_query_url: &str) -> Self {
        Self {
            endpoint: Endpoint::new(
                Url::parse(status_query_url).expect("expected a valid query URL"),
            ),
            client: hyper::Client::builder().build_http(),
        }
    }

    /// Gets the task hub associated with the client.
    pub fn task_hub(&self) -> &str {
        self.endpoint.task_hub()
    }

    /// Gets the status of an orchestration instance.
    pub async fn instance_status(
        &self,
        instance_id: &str,
        show_history: bool,
        show_history_output: bool,
        show_input: bool,
    ) -> Result<OrchestrationStatus> {
        let mut url = self.endpoint.status_query_url(Some(instance_id));

        url.query_pairs_mut()
            .append_pair("showHistory", if show_history { "true" } else { "false " })
            .append_pair(
                "showHistoryOutput",
                if show_history_output {
                    "true"
                } else {
                    "false "
                },
            )
            .append_pair("showInput", if show_input { "true" } else { "false " });

        let req = Request::builder()
            .method("GET")
            .uri(url.into_string())
            .header("Content-Type", "application/json")
            .body(Body::empty())
            .unwrap();

        match self.client.request(req).await {
            Ok(res) => match res.status() {
                StatusCode::OK | StatusCode::ACCEPTED => {
                    let body = res.into_body().try_concat().await;
                    body.map(|b| {
                        from_slice(&b).map_err(|e| {
                            ClientError::Message(format!(
                                "failed to deserialize orchestration status: {}",
                                e
                            ))
                        })
                    })
                    .unwrap_or_else(|e| {
                        Err(ClientError::Message(format!(
                            "failed to read response: {}",
                            e
                        )))
                    })
                }
                StatusCode::BAD_REQUEST => Err(ClientError::InstanceFailedOrTerminated),
                StatusCode::NOT_FOUND => Err(ClientError::InstanceNotFound),
                StatusCode::INTERNAL_SERVER_ERROR => Err(ClientError::InternalServerError),
                _ => unreachable!("unexpected response from server"),
            },
            Err(e) => Err(ClientError::Message(format!(
                "failed to send request: {}",
                e
            ))),
        }
    }

    /// Queries the status for instances in a given date range or with runtime status.
    #[allow(clippy::too_many_arguments)]
    pub async fn query_instances<I>(
        &self,
        created_time_from: Option<DateTime<Utc>>,
        created_time_to: Option<DateTime<Utc>>,
        runtime_statuses: Option<I>,
        top: Option<u32>,
        show_history: bool,
        show_history_output: bool,
        show_input: bool,
    ) -> Result<Vec<OrchestrationStatus>>
    where
        I: Iterator<Item = OrchestrationRuntimeStatus>,
    {
        let mut url = self.endpoint.status_query_url(None);

        {
            let mut query = url.query_pairs_mut();

            created_time_from.map(|t| query.append_pair("createdTimeFrom", &t.to_rfc3339()));
            created_time_to.map(|t| query.append_pair("createdTimeTo", &t.to_rfc3339()));
            runtime_statuses.map(|s| {
                query.append_pair(
                    "runtimeStatus",
                    &s.map(|s| s.to_string()).collect::<Vec<_>>().join(","),
                )
            });

            top.map(|t| query.append_pair("top", &t.to_string()));

            query
                .append_pair("showHistory", if show_history { "true" } else { "false " })
                .append_pair(
                    "showHistoryOutput",
                    if show_history_output {
                        "true"
                    } else {
                        "false "
                    },
                )
                .append_pair("showInput", if show_input { "true" } else { "false " });
        }

        let req = Request::builder()
            .method("GET")
            .uri(url.into_string())
            .header("Content-Type", "application/json")
            .body(Body::empty())
            .unwrap();

        match self.client.request(req).await {
            Ok(res) => match res.status() {
                StatusCode::OK | StatusCode::ACCEPTED => {
                    let body = res.into_body().try_concat().await;
                    body.map(|b| {
                        from_slice(&b).map_err(|e| {
                            ClientError::Message(format!(
                                "failed to deserialize orchestration status: {}",
                                e
                            ))
                        })
                    })
                    .unwrap_or_else(|e| {
                        Err(ClientError::Message(format!(
                            "failed to read response: {}",
                            e
                        )))
                    })
                }
                StatusCode::BAD_REQUEST => Err(ClientError::InstanceFailedOrTerminated),
                StatusCode::NOT_FOUND => Err(ClientError::InstanceNotFound),
                StatusCode::INTERNAL_SERVER_ERROR => Err(ClientError::InternalServerError),
                _ => unreachable!("unexpected response from server"),
            },
            Err(e) => Err(ClientError::Message(format!(
                "failed to send request: {}",
                e
            ))),
        }
    }

    /// Purges the history of the given orchestration instance.
    pub async fn purge_history(&self, instance_id: &str) -> Result<()> {
        let req = Request::builder()
            .method("DELETE")
            .uri(
                self.endpoint
                    .purge_history_url(Some(instance_id))
                    .into_string(),
            )
            .header("Content-Type", "application/json")
            .body(Body::empty())
            .unwrap();

        match self.client.request(req).await {
            Ok(res) => match res.status() {
                StatusCode::OK => Ok(()),
                StatusCode::NOT_FOUND => Err(ClientError::InstanceNotFound),
                _ => unreachable!("unexpected response from server"),
            },
            Err(e) => Err(ClientError::Message(format!(
                "failed to send request: {}",
                e
            ))),
        }
    }

    /// Purges the history of orchestrations matching the given date range or runtime statuses.
    pub async fn purge_history_by_query<I>(
        &self,
        created_time_from: Option<DateTime<Utc>>,
        created_time_to: Option<DateTime<Utc>>,
        runtime_statuses: Option<I>,
    ) -> Result<u32>
    where
        I: Iterator<Item = OrchestrationRuntimeStatus>,
    {
        let mut url = self.endpoint.purge_history_url(None);

        {
            let mut query = url.query_pairs_mut();

            created_time_from.map(|t| query.append_pair("createdTimeFrom", &t.to_rfc3339()));
            created_time_to.map(|t| query.append_pair("createdTimeTo", &t.to_rfc3339()));
            runtime_statuses.map(|s| {
                query.append_pair(
                    "runtimeStatus",
                    &s.map(|s| s.to_string()).collect::<Vec<_>>().join(","),
                )
            });
        }

        let req = Request::builder()
            .method("DELETE")
            .uri(url.into_string())
            .header("Content-Type", "application/json")
            .body(Body::empty())
            .unwrap();

        #[derive(Debug, Clone, Deserialize)]
        #[serde(rename_all = "camelCase")]
        struct PurgeHistoryResult {
            instances_deleted: u32,
        }

        match self.client.request(req).await {
            Ok(res) => match res.status() {
                StatusCode::OK => {
                    let body = res.into_body().try_concat().await;
                    let result: PurgeHistoryResult = body
                        .map(|b| {
                            from_slice(&b).map_err(|e| {
                                ClientError::Message(format!(
                                    "failed to deserialize orchestration status: {}",
                                    e
                                ))
                            })
                        })
                        .unwrap_or_else(|e| {
                            Err(ClientError::Message(format!(
                                "failed to read response: {}",
                                e
                            )))
                        })?;

                    Ok(result.instances_deleted)
                }
                StatusCode::NOT_FOUND => Err(ClientError::InstanceNotFound),
                _ => unreachable!("unexpected response from server"),
            },
            Err(e) => Err(ClientError::Message(format!(
                "failed to send request: {}",
                e
            ))),
        }
    }

    /// Raises an event for the given orchestration instance.
    pub async fn raise_event<D>(
        &self,
        instance_id: &str,
        event_name: &str,
        event_data: D,
    ) -> Result<()>
    where
        D: Into<Value>,
    {
        let req = Request::builder()
            .method("POST")
            .uri(
                self.endpoint
                    .raise_event_url(instance_id, event_name)
                    .into_string(),
            )
            .header("Content-Type", "application/json")
            .body(Body::from(to_string(&event_data.into()).unwrap()))
            .unwrap();

        match self.client.request(req).await {
            Ok(res) => match res.status() {
                StatusCode::ACCEPTED => Ok(()),
                StatusCode::NOT_FOUND => Err(ClientError::InstanceNotFound),
                StatusCode::BAD_REQUEST => Err(ClientError::BadRequest),
                StatusCode::GONE => Err(ClientError::InstanceCompletedOrFailed),
                _ => unreachable!("unexpected response from server"),
            },
            Err(e) => Err(ClientError::Message(format!(
                "failed to send request: {}",
                e
            ))),
        }
    }

    /// Restores a failed orchestration instance into a running state by replaying the most recent failed operations.
    pub async fn rewind(&self, instance_id: &str, reason: &str) -> Result<()> {
        let req = Request::builder()
            .method("POST")
            .uri(self.endpoint.rewind_url(instance_id, reason).into_string())
            .header("Content-Type", "application/json")
            .body(Body::empty())
            .unwrap();

        match self.client.request(req).await {
            Ok(res) => match res.status() {
                StatusCode::ACCEPTED => Ok(()),
                StatusCode::NOT_FOUND => Err(ClientError::InstanceNotFound),
                StatusCode::BAD_REQUEST => Err(ClientError::BadRequest),
                StatusCode::GONE => Err(ClientError::InstanceCompletedOrFailed),
                _ => unreachable!("unexpected response from server"),
            },
            Err(e) => Err(ClientError::Message(format!(
                "failed to send request: {}",
                e
            ))),
        }
    }

    /// Starts a new orchestration by calling the given orchestration function.
    pub async fn start_new<D>(
        &self,
        function_name: &str,
        instance_id: Option<&str>,
        input: D,
    ) -> Result<OrchestrationData>
    where
        D: Into<Value>,
    {
        let req = Request::builder()
            .method("POST")
            .uri(
                self.endpoint
                    .create_new_instance_url(function_name, instance_id)
                    .into_string(),
            )
            .header("Content-Type", "application/json")
            .body(Body::from(input.into().to_string()))
            .unwrap();

        match self.client.request(req).await {
            Ok(res) => match res.status() {
                StatusCode::ACCEPTED => {
                    let body = res.into_body().try_concat().await;
                    body.map(|b| {
                        from_slice(&b).map_err(|e| {
                            ClientError::Message(format!(
                                "failed to deserialize orchestration data: {}",
                                e
                            ))
                        })
                    })
                    .unwrap_or_else(|e| {
                        Err(ClientError::Message(format!(
                            "failed to read response: {}",
                            e
                        )))
                    })
                }
                StatusCode::BAD_REQUEST => Err(ClientError::BadCreateRequest),
                _ => unreachable!("unexpected response from server"),
            },
            Err(e) => Err(ClientError::Message(format!(
                "failed to send request: {}",
                e
            ))),
        }
    }

    /// Terminates a running orchestration instance.
    pub async fn terminate(&self, instance_id: &str, reason: &str) -> Result<()> {
        let req = Request::builder()
            .method("POST")
            .uri(
                self.endpoint
                    .terminate_url(instance_id, reason)
                    .into_string(),
            )
            .header("Content-Type", "application/json")
            .body(Body::empty())
            .unwrap();

        match self.client.request(req).await {
            Ok(res) => match res.status() {
                StatusCode::ACCEPTED => Ok(()),
                StatusCode::NOT_FOUND => Err(ClientError::InstanceNotFound),
                StatusCode::GONE => Err(ClientError::InstanceCompletedOrFailed),
                _ => unreachable!("unexpected response from server"),
            },
            Err(e) => Err(ClientError::Message(format!(
                "failed to send request: {}",
                e
            ))),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::offset::TimeZone;
    use serde_json::from_str;

    #[test]
    fn test_instance_history() {
        let h1: String = r#"{"EventType": "ExecutionStarted",
          "FunctionName": "E1_HelloSequence",
          "Timestamp": "2018-02-28T05:18:49Z"
        }"#
        .to_owned();

        let compare_dt = Utc.ymd(2018, 2, 28).and_hms(5, 18, 49);

        let h1_obj: OrchestrationHistoryEvent = from_str(&h1).unwrap();
        assert_eq!(h1_obj.event_type, "ExecutionStarted");
        assert_eq!(h1_obj.timestamp, compare_dt);

        let h2: String = r#"{
          "EventType": "ExecutionCompleted",
          "OrchestrationStatus": "Completed",
          "Result": [
              "Hello Tokyo!",
              "Hello Seattle!",
              "Hello London!"
          ],
          "Timestamp": "2018-02-28T05:18:54.3660895Z"
        }"#
        .to_owned();

        let h2_obj: OrchestrationHistoryEvent = from_str(&h2).unwrap();
        assert_eq!(h2_obj.orchestration_status.is_some(), true);
        assert_eq!(
            h2_obj.orchestration_status.unwrap(),
            OrchestrationRuntimeStatus::Completed
        );
    }

    #[test]
    fn test_instance_status() {
        let example: String = r#"{
            "createdTime": "2018-02-28T05:18:49Z",
            "historyEvents": [
            {
                "EventType": "ExecutionStarted",
                "FunctionName": "E1_HelloSequence",
                "Timestamp": "2018-02-28T05:18:49.3452372Z"
            },
            {
                "EventType": "TaskCompleted",
                "FunctionName": "E1_SayHello",
                "Result": "Hello Tokyo!",
                "ScheduledTime": "2018-02-28T05:18:51.3939873Z",
                "Timestamp": "2018-02-28T05:18:52.2895622Z"
            },
            {
                "EventType": "TaskCompleted",
                "FunctionName": "E1_SayHello",
                "Result": "Hello Seattle!",
                "ScheduledTime": "2018-02-28T05:18:52.8755705Z",
                "Timestamp": "2018-02-28T05:18:53.1765771Z"
            },
            {
                "EventType": "TaskCompleted",
                "FunctionName": "E1_SayHello",
                "Result": "Hello London!",
                "ScheduledTime": "2018-02-28T05:18:53.5170791Z",
                "Timestamp": "2018-02-28T05:18:53.891081Z"
            },
            {
                "EventType": "ExecutionCompleted",
                "OrchestrationStatus": "Completed",
                "Result": [
                    "Hello Tokyo!",
                    "Hello Seattle!",
                    "Hello London!"
                ],
                "Timestamp": "2018-02-28T05:18:54.3660895Z"
            }
        ],
        "input": null,
        "customStatus": { "nextActions": ["A", "B", "C"], "foo": 2 },
        "lastUpdatedTime": "2018-02-28T05:18:54Z",
        "output": [
            "Hello Tokyo!",
            "Hello Seattle!",
            "Hello London!"
        ],
        "runtimeStatus": "Completed"
        }"#
        .to_owned();

        let instance_status: OrchestrationStatus = from_str(&example).unwrap();
        assert_eq!(instance_status.history_events.is_some(), true);
        assert_eq!(instance_status.history_events.unwrap().len(), 5);

        assert_eq!(instance_status.custom_status.is_some(), true);
        assert_eq!(instance_status.custom_status.unwrap().is_object(), true);
    }
}