Skip to content

Commit cd3ca5a

Browse files
committed
Added 'Hello' example - needs testing
1 parent 708c376 commit cd3ca5a

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
// (swift-tools-version has two lines here because it needs to be the first
6+
// line in the file, but it should also appear in the snippet below)
7+
//
8+
// snippet-start:[swift.support.hello.package]
9+
// swift-tools-version: 5.9
10+
//
11+
// The swift-tools-version declares the minimum version of Swift required to
12+
// build this package.
13+
14+
import PackageDescription
15+
16+
let package = Package(
17+
name: "hello-support",
18+
// Let Xcode know the minimum Apple platforms supported.
19+
platforms: [
20+
.macOS(.v13),
21+
.iOS(.v15)
22+
],
23+
dependencies: [
24+
// Dependencies declare other packages that this package depends on.
25+
.package(
26+
url: "https://github.com/awslabs/aws-sdk-swift",
27+
from: "1.0.0"),
28+
.package(
29+
url: "https://github.com/apple/swift-argument-parser.git",
30+
branch: "main"
31+
)
32+
],
33+
targets: [
34+
// Targets are the basic building blocks of a package, defining a module or a test suite.
35+
// Targets can depend on other targets in this package and products
36+
// from dependencies.
37+
.executableTarget(
38+
name: "hello-support",
39+
dependencies: [
40+
.product(name: "AWSSupport", package: "aws-sdk-swift"),
41+
.product(name: "ArgumentParser", package: "swift-argument-parser")
42+
],
43+
path: "Sources")
44+
45+
]
46+
)
47+
// snippet-end:[swift.support.hello.package]
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
//
4+
// snippet-start:[swift.support.hello]
5+
// An example that shows how to use the AWS SDK for Swift to perform a simple
6+
// operation using Amazon Elastic Compute Cloud (EC2).
7+
//
8+
9+
import ArgumentParser
10+
import AWSClientRuntime
11+
import Foundation
12+
13+
// snippet-start:[swift.support.import]
14+
import AWSSupport
15+
// snippet-end:[swift.support.import]
16+
17+
struct ExampleCommand: ParsableCommand {
18+
@Option(help: "The AWS Region to run AWS API calls in.")
19+
var awsRegion = "us-east-1"
20+
21+
static var configuration = CommandConfiguration(
22+
commandName: "hello-support",
23+
abstract: """
24+
Demonstrates a simple operation using Amazon Support.
25+
""",
26+
discussion: """
27+
An example showing how to make a call to Amazon Support using the AWS
28+
SDK for Swift.
29+
"""
30+
)
31+
32+
/// Return an array of the user's services.
33+
///
34+
/// - Parameter supportClient: The `SupportClient` to use when calling
35+
/// `describeServices()`.
36+
///
37+
/// - Returns: An array of services.
38+
func getSupportServices(supportClient: SupportClient) async -> [SupportClientTypes.Service] {
39+
do {
40+
let output = try await supportClient.describeServices(
41+
input: DescribeServicesInput()
42+
)
43+
44+
guard let services = output.services else {
45+
return []
46+
}
47+
48+
return services
49+
} catch let error as AWSServiceError {
50+
// SubscriptionRequiredException isn't a modeled error, so we
51+
// have to catch AWSServiceError and then look at its errorCode to
52+
// see if it's SubscriptionRequiredException.
53+
if error.errorCode == "SubscriptionRequiredException" {
54+
print("*** You need a subscription to use AWS Support.")
55+
return []
56+
} else {
57+
print("*** An unknown error occurred getting support information.")
58+
return []
59+
}
60+
} catch {
61+
print("*** Error getting service information: \(error.localizedDescription)")
62+
return []
63+
}
64+
}
65+
66+
/// Called by ``main()`` to run the bulk of the example.
67+
func runAsync() async throws {
68+
let supportConfig = try await SupportClient.SupportClientConfiguration(region: awsRegion)
69+
let supportClient = SupportClient(config: supportConfig)
70+
71+
let services = await getSupportServices(supportClient: supportClient)
72+
73+
print("Found \(services.count) security services")
74+
}
75+
}
76+
77+
/// The program's asynchronous entry point.
78+
@main
79+
struct Main {
80+
static func main() async {
81+
let args = Array(CommandLine.arguments.dropFirst())
82+
83+
do {
84+
let command = try ExampleCommand.parse(args)
85+
try await command.runAsync()
86+
} catch {
87+
ExampleCommand.exit(withError: error)
88+
}
89+
}
90+
}
91+
// snippet-end:[swift.support.hello]

0 commit comments

Comments
 (0)