|
| 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 Queue |
| 5 | +// Service client to delete messages from an Amazon SQS queue. |
| 6 | + |
| 7 | +import ArgumentParser |
| 8 | +import AWSClientRuntime |
| 9 | +import AWSSQS |
| 10 | +import Foundation |
| 11 | + |
| 12 | +struct ExampleCommand: ParsableCommand { |
| 13 | + @Option(help: "The URL of the Amazon SQS queue from which to delete messages") |
| 14 | + var queue: String |
| 15 | + @Argument(help: "Receipt handle(s) of the message(s) to delete") |
| 16 | + var handles: [String] |
| 17 | + @Option(help: "Name of the Amazon Region to use (default: us-east-1)") |
| 18 | + var region = "us-east-1" |
| 19 | + |
| 20 | + static var configuration = CommandConfiguration( |
| 21 | + commandName: "deletemessages", |
| 22 | + abstract: """ |
| 23 | + This example shows how to delete a batch of messages from an Amazon SQS queue. |
| 24 | + """, |
| 25 | + discussion: """ |
| 26 | + """ |
| 27 | + ) |
| 28 | + |
| 29 | + /// Called by ``main()`` to run the bulk of the example. |
| 30 | + func runAsync() async throws { |
| 31 | + // snippet-start:[swift.sqs.DeleteMessageBatch] |
| 32 | + let config = try await SQSClient.SQSClientConfiguration(region: region) |
| 33 | + let sqsClient = SQSClient(config: config) |
| 34 | + |
| 35 | + // Create the list of message entries. |
| 36 | + |
| 37 | + var entries: [SQSClientTypes.DeleteMessageBatchRequestEntry] = [] |
| 38 | + var messageNumber = 1 |
| 39 | + |
| 40 | + for handle in handles { |
| 41 | + let entry = SQSClientTypes.DeleteMessageBatchRequestEntry( |
| 42 | + id: "\(messageNumber)", |
| 43 | + receiptHandle: handle |
| 44 | + ) |
| 45 | + entries.append(entry) |
| 46 | + messageNumber += 1 |
| 47 | + } |
| 48 | + |
| 49 | + // Delete the messages. |
| 50 | + |
| 51 | + let output = try await sqsClient.deleteMessageBatch( |
| 52 | + input: DeleteMessageBatchInput( |
| 53 | + entries: entries, |
| 54 | + queueUrl: queue |
| 55 | + ) |
| 56 | + ) |
| 57 | + |
| 58 | + // Get the lists of failed and successful deletions from the output. |
| 59 | + |
| 60 | + guard let failedEntries = output.failed else { |
| 61 | + print("Failed deletion list is missing!") |
| 62 | + return |
| 63 | + } |
| 64 | + guard let successfulEntries = output.successful else { |
| 65 | + print("Successful deletion list is missing!") |
| 66 | + return |
| 67 | + } |
| 68 | + |
| 69 | + // Display a list of the failed deletions along with their |
| 70 | + // corresponding explanation messages. |
| 71 | + |
| 72 | + if failedEntries.count != 0 { |
| 73 | + print("Failed deletions:") |
| 74 | + |
| 75 | + for entry in failedEntries { |
| 76 | + print("Message #\(entry.id ?? "<unknown>") failed: \(entry.message ?? "<unknown>")") |
| 77 | + } |
| 78 | + } else { |
| 79 | + print("No failed deletions.") |
| 80 | + } |
| 81 | + |
| 82 | + // Output a list of the message numbers that were successfully deleted. |
| 83 | + |
| 84 | + if successfulEntries.count != 0 { |
| 85 | + var successes = "" |
| 86 | + |
| 87 | + for entry in successfulEntries { |
| 88 | + if successes.count == 0 { |
| 89 | + successes = entry.id ?? "<unknown>" |
| 90 | + } else { |
| 91 | + successes = "\(successes), \(entry.id ?? "<unknown>")" |
| 92 | + } |
| 93 | + } |
| 94 | + print("Succeeded: ", successes) |
| 95 | + } else { |
| 96 | + print("No successful deletions.") |
| 97 | + } |
| 98 | + |
| 99 | + // snippet-end:[swift.sqs.DeleteMessageBatch] |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +/// The program's asynchronous entry point. |
| 104 | +@main |
| 105 | +struct Main { |
| 106 | + static func main() async { |
| 107 | + let args = Array(CommandLine.arguments.dropFirst()) |
| 108 | + |
| 109 | + do { |
| 110 | + let command = try ExampleCommand.parse(args) |
| 111 | + try await command.runAsync() |
| 112 | + } catch { |
| 113 | + ExampleCommand.exit(withError: error) |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments