Skip to content

Commit bb39ac7

Browse files
authored
SNS examples for Swift SDK (awsdocs#7233)
1 parent 6a93cf0 commit bb39ac7

File tree

16 files changed

+891
-0
lines changed

16 files changed

+891
-0
lines changed

.doc_gen/metadata/sns_metadata.yaml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ sns_Hello:
5656
- description: Initialize an SNS client and and list topics in your account.
5757
snippet_tags:
5858
- javascript.v3.sns.hello
59+
Swift:
60+
versions:
61+
- sdk_version: 1
62+
github: swift/example_code/sns/basics
63+
excerpts:
64+
- description: The Package.swift file.
65+
snippet_tags:
66+
- swift.sns.basics.package
67+
- description: The main Swift program.
68+
snippet_tags:
69+
- swift.sns.basics.hello
5970
services:
6071
sns: {ListTopics}
6172
sns_GetTopicAttributes:
@@ -294,6 +305,13 @@ sns_ListTopics:
294305
excerpts:
295306
- snippet_tags:
296307
- sns.rust.list-topics
308+
Swift:
309+
versions:
310+
- sdk_version: 1
311+
github: swift/example_code/sns/basics
312+
excerpts:
313+
- snippet_tags:
314+
- swift.sns.ListTopics
297315
SAP ABAP:
298316
versions:
299317
- sdk_version: 1
@@ -525,6 +543,13 @@ sns_CreateTopic:
525543
excerpts:
526544
- snippet_tags:
527545
- sns.rust.create-topic
546+
Swift:
547+
versions:
548+
- sdk_version: 1
549+
github: swift/example_code/sns
550+
excerpts:
551+
- snippet_tags:
552+
- swift.sns.CreateTopic
528553
SAP ABAP:
529554
versions:
530555
- sdk_version: 1
@@ -607,6 +632,13 @@ sns_DeleteTopic:
607632
- snippet_tags:
608633
- python.example_code.sns.SnsWrapper
609634
- python.example_code.sns.DeleteTopic
635+
Swift:
636+
versions:
637+
- sdk_version: 1
638+
github: swift/example_code/sns
639+
excerpts:
640+
- snippet_tags:
641+
- swift.sns.DeleteTopic
610642
SAP ABAP:
611643
versions:
612644
- sdk_version: 1
@@ -745,6 +777,13 @@ sns_Publish:
745777
excerpts:
746778
- snippet_tags:
747779
- sns.rust.sns-hello-world
780+
Swift:
781+
versions:
782+
- sdk_version: 1
783+
github: swift/example_code/sns
784+
excerpts:
785+
- snippet_tags:
786+
- swift.sns.Publish
748787
SAP ABAP:
749788
versions:
750789
- sdk_version: 1
@@ -1067,6 +1106,17 @@ sns_Subscribe:
10671106
- description: Subscribe an email address to a topic.
10681107
snippet_tags:
10691108
- sns.rust.sns-hello-world
1109+
Swift:
1110+
versions:
1111+
- sdk_version: 1
1112+
github: swift/example_code/sns
1113+
excerpts:
1114+
- description: Subscribe an email address to a topic.
1115+
snippet_tags:
1116+
- swift.sns.SubscribeEmail
1117+
- description: Subscribe a phone number to a topic to receive notifications by SMS.
1118+
snippet_tags:
1119+
- swift.sns.SubscribeSMS
10701120
SAP ABAP:
10711121
versions:
10721122
- sdk_version: 1
@@ -1140,6 +1190,13 @@ sns_Unsubscribe:
11401190
- snippet_tags:
11411191
- python.example_code.sns.SnsWrapper
11421192
- python.example_code.sns.Unsubscribe
1193+
Swift:
1194+
versions:
1195+
- sdk_version: 1
1196+
github: swift/example_code/sns
1197+
excerpts:
1198+
- snippet_tags:
1199+
- swift.sns.Unsubscribe
11431200
SAP ABAP:
11441201
versions:
11451202
- sdk_version: 1
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// swift-tools-version: 5.9
2+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
// The swift-tools-version declares the minimum version of Swift required to
6+
// build this package.
7+
8+
import PackageDescription
9+
10+
let package = Package(
11+
name: "createtopic",
12+
// Let Xcode know the minimum Apple platforms supported.
13+
platforms: [
14+
.macOS(.v13),
15+
.iOS(.v15)
16+
],
17+
dependencies: [
18+
// Dependencies declare other packages that this package depends on.
19+
.package(
20+
url: "https://github.com/awslabs/aws-sdk-swift",
21+
from: "1.0.0"),
22+
.package(
23+
url: "https://github.com/apple/swift-argument-parser.git",
24+
branch: "main"
25+
)
26+
],
27+
targets: [
28+
// Targets are the basic building blocks of a package, defining a module or a test suite.
29+
// Targets can depend on other targets in this package and products
30+
// from dependencies.
31+
.executableTarget(
32+
name: "createtopic",
33+
dependencies: [
34+
.product(name: "AWSSNS", package: "aws-sdk-swift"),
35+
.product(name: "ArgumentParser", package: "swift-argument-parser")
36+
],
37+
path: "Sources")
38+
39+
]
40+
)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
//
4+
// An example demonstrating how to set up and use an Amazon Simple Notification
5+
// Service (SNS) client to create a new topic.
6+
7+
import ArgumentParser
8+
import AWSClientRuntime
9+
import AWSSNS
10+
import Foundation
11+
12+
struct ExampleCommand: ParsableCommand {
13+
@Argument(help: "Name to give the new Amazon SNS topic")
14+
var name: String
15+
@Option(help: "Name of the Amazon Region to use (default: us-east-1)")
16+
var region = "us-east-1"
17+
18+
static var configuration = CommandConfiguration(
19+
commandName: "createtopic",
20+
abstract: """
21+
This example shows how to create an Amazon SNS topic.
22+
""",
23+
discussion: """
24+
"""
25+
)
26+
27+
/// Called by ``main()`` to run the bulk of the example.
28+
func runAsync() async throws {
29+
// snippet-start:[swift.sns.CreateTopic]
30+
let config = try await SNSClient.SNSClientConfiguration(region: region)
31+
let snsClient = SNSClient(config: config)
32+
33+
let output = try await snsClient.createTopic(
34+
input: CreateTopicInput(name: name)
35+
)
36+
37+
guard let arn = output.topicArn else {
38+
print("No topic ARN returned by Amazon SNS.")
39+
return
40+
}
41+
// snippet-end:[swift.sns.CreateTopic]
42+
43+
print("New topic created with ARN: \(arn)")
44+
}
45+
}
46+
47+
/// The program's asynchronous entry point.
48+
@main
49+
struct Main {
50+
static func main() async {
51+
let args = Array(CommandLine.arguments.dropFirst())
52+
53+
do {
54+
let command = try ExampleCommand.parse(args)
55+
try await command.runAsync()
56+
} catch {
57+
ExampleCommand.exit(withError: error)
58+
}
59+
}
60+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// swift-tools-version: 5.9
2+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
// The swift-tools-version declares the minimum version of Swift required to
6+
// build this package.
7+
8+
import PackageDescription
9+
10+
let package = Package(
11+
name: "deletetopic",
12+
// Let Xcode know the minimum Apple platforms supported.
13+
platforms: [
14+
.macOS(.v13),
15+
.iOS(.v15)
16+
],
17+
dependencies: [
18+
// Dependencies declare other packages that this package depends on.
19+
.package(
20+
url: "https://github.com/awslabs/aws-sdk-swift",
21+
from: "1.0.0"),
22+
.package(
23+
url: "https://github.com/apple/swift-argument-parser.git",
24+
branch: "main"
25+
)
26+
],
27+
targets: [
28+
// Targets are the basic building blocks of a package, defining a module or a test suite.
29+
// Targets can depend on other targets in this package and products
30+
// from dependencies.
31+
.executableTarget(
32+
name: "deletetopic",
33+
dependencies: [
34+
.product(name: "AWSSNS", package: "aws-sdk-swift"),
35+
.product(name: "ArgumentParser", package: "swift-argument-parser")
36+
],
37+
path: "Sources")
38+
39+
]
40+
)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
//
4+
// An example demonstrating how to set up and use an Amazon Simple Notification
5+
// Service (SNS) client to delete a topic.
6+
7+
import ArgumentParser
8+
import AWSClientRuntime
9+
import AWSSNS
10+
import Foundation
11+
12+
struct ExampleCommand: ParsableCommand {
13+
@Argument(help: "The ARN of the Amazon SNS topic to delete")
14+
var arn: String
15+
@Option(help: "Name of the Amazon Region to use (default: us-east-1)")
16+
var region = "us-east-1"
17+
18+
static var configuration = CommandConfiguration(
19+
commandName: "deletetopic",
20+
abstract: """
21+
This example shows how to delete an Amazon SNS topic.
22+
""",
23+
discussion: """
24+
"""
25+
)
26+
27+
/// Called by ``main()`` to run the bulk of the example.
28+
func runAsync() async throws {
29+
// snippet-start:[swift.sns.DeleteTopic]
30+
let config = try await SNSClient.SNSClientConfiguration(region: region)
31+
let snsClient = SNSClient(config: config)
32+
33+
_ = try await snsClient.deleteTopic(
34+
input: DeleteTopicInput(topicArn: arn)
35+
)
36+
// snippet-end:[swift.sns.DeleteTopic]
37+
38+
print("Topic deleted.")
39+
}
40+
}
41+
42+
/// The program's asynchronous entry point.
43+
@main
44+
struct Main {
45+
static func main() async {
46+
let args = Array(CommandLine.arguments.dropFirst())
47+
48+
do {
49+
let command = try ExampleCommand.parse(args)
50+
try await command.runAsync()
51+
} catch {
52+
ExampleCommand.exit(withError: error)
53+
}
54+
}
55+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// swift-tools-version: 5.9
2+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
// The swift-tools-version declares the minimum version of Swift required to
6+
// build this package.
7+
8+
import PackageDescription
9+
10+
let package = Package(
11+
name: "publish",
12+
// Let Xcode know the minimum Apple platforms supported.
13+
platforms: [
14+
.macOS(.v13),
15+
.iOS(.v15)
16+
],
17+
dependencies: [
18+
// Dependencies declare other packages that this package depends on.
19+
.package(
20+
url: "https://github.com/awslabs/aws-sdk-swift",
21+
from: "1.0.0"),
22+
.package(
23+
url: "https://github.com/apple/swift-argument-parser.git",
24+
branch: "main"
25+
)
26+
],
27+
targets: [
28+
// Targets are the basic building blocks of a package, defining a module or a test suite.
29+
// Targets can depend on other targets in this package and products
30+
// from dependencies.
31+
.executableTarget(
32+
name: "publish",
33+
dependencies: [
34+
.product(name: "AWSSNS", package: "aws-sdk-swift"),
35+
.product(name: "ArgumentParser", package: "swift-argument-parser")
36+
],
37+
path: "Sources")
38+
39+
]
40+
)

0 commit comments

Comments
 (0)