1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import Foundation
5
+ import AWSSFN
6
+
7
+ /// Describes errors that occur on Step Functions activities.
8
+ enum ActivityError : Error {
9
+ /// The ARN is missing from the returned activity.
10
+ case missingArnError
11
+ /// The activity list is missing from the response.
12
+ case missingActivityListError
13
+ /// No matching activity was found.
14
+ case activityNotFoundError
15
+
16
+ var errorDescription : String {
17
+ switch self {
18
+ case . missingArnError:
19
+ return " The ARN is missing from the returned activity "
20
+ case . missingActivityListError:
21
+ return " The activity list is missing from the response "
22
+ case . activityNotFoundError:
23
+ return " No activity with the specified name was found "
24
+ }
25
+ }
26
+ }
27
+
28
+ /// Manage a Step Functions activity.
29
+ class Activity {
30
+ let sfnClient : SFNClient
31
+ let activityName : String
32
+ var activityArn = " "
33
+
34
+ init ( client: SFNClient , name: String ) async throws {
35
+ sfnClient = client
36
+ self . activityName = name
37
+
38
+ try await self . findOrCreateActivity ( )
39
+ }
40
+
41
+ /// Create a new Step Functions activity.
42
+ ///
43
+ /// - Throws: `ActivityError` and appropriate AWS errors.
44
+ private func createActivity( ) async throws {
45
+ let output = try await sfnClient. createActivity (
46
+ input: CreateActivityInput ( name: activityName)
47
+ )
48
+
49
+ guard let arn = output. activityArn else {
50
+ throw ActivityError . missingArnError
51
+ }
52
+
53
+ activityArn = arn
54
+ }
55
+
56
+ /// Find an activity with the name specified when initializing the
57
+ /// `Activity` object.
58
+ ///
59
+ /// - Throws: `ActivityError` and appropriate AWS errors.
60
+ private func findActivity( ) async throws {
61
+ let pages = sfnClient. listActivitiesPaginated (
62
+ input: ListActivitiesInput ( )
63
+ )
64
+
65
+ for try await page in pages {
66
+ guard let activities = page. activities else {
67
+ throw ActivityError . missingActivityListError
68
+ }
69
+
70
+ for activity in activities {
71
+ if activity. name == activityName {
72
+ guard let arn = activity. activityArn else {
73
+ throw ActivityError . missingArnError
74
+ }
75
+ self . activityArn = arn
76
+ }
77
+ }
78
+ }
79
+
80
+ throw ActivityError . activityNotFoundError
81
+ }
82
+
83
+ /// Finds an existing activity with the name given when initializing
84
+ /// the `Activity`. If one isn't found, a new one is created.
85
+ ///
86
+ /// - Throws: `ActivityError` and appropriate AWS errors.
87
+ private func findOrCreateActivity( ) async throws {
88
+ do {
89
+ try await findActivity ( )
90
+ } catch {
91
+ try await createActivity ( )
92
+ }
93
+ }
94
+
95
+ /// Delete the activity described by this object.
96
+ public func delete( ) async {
97
+ do {
98
+ _ = try await sfnClient. deleteActivity (
99
+ input: DeleteActivityInput ( activityArn: activityArn)
100
+ )
101
+ } catch {
102
+ print ( " *** Error deleting the activity: \( error. localizedDescription) " )
103
+ }
104
+ }
105
+
106
+ /// Sends a task success notification to the activity.
107
+ ///
108
+ /// - Parameters:
109
+ /// - taskToken: The task's token.
110
+ /// - response: The task response.
111
+ ///
112
+ /// - Returns: `ActivityError` and appropriate AWS errors.
113
+ public func sendTaskSuccess( taskToken: String , response: String ) async -> Bool {
114
+ do {
115
+ _ = try await sfnClient. sendTaskSuccess (
116
+ input: SendTaskSuccessInput ( output: response, taskToken: taskToken)
117
+ )
118
+
119
+ return true
120
+ } catch {
121
+ print ( " *** Error sending task success: \( error. localizedDescription) " )
122
+ return false
123
+ }
124
+ }
125
+ }
0 commit comments