Extending Vapor Application and Request to support AWS SDK Swift
import AWSS3
import Vapor
public extension Application {
var aws: AWS {
.init(application: self)
}
import Vapor | |
/// Middleware for outputting all http requests and their responses | |
final class LoggingMiddleware : Middleware, ServiceType { | |
static func makeService(for container: Container) throws -> Self { | |
return try .init(environment: container.environment, log: container.make()) | |
} | |
init(environment: Environment, log: Logger) { | |
self.environment = environment |
import AWSSDKSwiftCore | |
import CryptoSwift | |
import Foundation | |
extension Array where Element: FixedWidthInteger { | |
static func random(count: Int) -> [Element] { | |
var array = self.init() | |
for _ in 0..<count { | |
array.append(.random(in: Element.min..<Element.max)) | |
} |
import Network | |
import NIO | |
import NIOHTTP1 | |
import NIOTransportServices | |
/// Channel Handler for serializing request header and data | |
class HTTPClientRequestSerializer : ChannelOutboundHandler { | |
typealias OutboundIn = Void | |
typealias OutboundOut = HTTPClientRequestPart |
```swift | |
let cloudTrail = CloudTrail(region:.euwest1) | |
let ses = SES(region:.euwest1) | |
let request = CloudTrail.LookupEventsRequest( | |
endTime:TimeStamp(Date()), | |
lookupAttributes: [.init(attributeKey: .username, attributeValue: "adamfowler")], | |
startTime: TimeStamp(Date(timeIntervalSinceNow: -24*60*60)) | |
) | |
// generate report |
Extending Vapor Application and Request to support AWS SDK Swift
import AWSS3
import Vapor
public extension Application {
var aws: AWS {
.init(application: self)
}
enum RequestProvider<Request> {
case `static`(Request)
case dynamic((EventLoop)->EventLoopFuture<Request>)
func getRequest(on eventLoop: EventLoop) -> EventLoopFuture<Request> {
switch self {
case .static(let request):
return eventLoop.makeSucceededFuture(request)
case .dynamic(let requestFunction):
Using MQTTNIO to connect to a AWS IoT Broker with WebSockets
import SotoSignerV4
let host = "MY_AWS_IOT_ENDPOINT"
let port = 443
let headers = HTTPHeaders([("host", host)])
let signer = AWSSigner(
credentials: StaticCredential(accessKeyId: "MYACCESSKEY", secretAccessKey: "MYSECRETKEY"),
/// For a job to be decodable, it has to be registered. Call `MyJob.register()` to register a job.
public protocol HBJob: Codable {
/// Unique Job name
static var name: String { get }
}
extension HBJob {
/// register job
public static func register() {
Implement Sequence.forEach
and Sequence.map
that takes an async closure.
@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)
extension Sequence {
// named `asyncForEach` as naming it `forEach` confuses the compiler
func asyncForEach(_ body: @escaping (Element) async throws -> Void) async rethrows {
try await withThrowingTaskGroup(of: Void.self) { group in
self.forEach { element in
group.addTask {
For this to work you need swift-sh installed.
#!/usr/bin/env swift-sh
import ArgumentParser // apple/swift-argument-parser
import HummingbirdFoundation // hummingbird-project/hummingbird
struct WebServer: ParsableCommand {
@Option(name: .shortAndLong)
var port: Int = 8001