|
| 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