diff --git a/Package.resolved b/Package.resolved index 351b90a..6c425ce 100644 --- a/Package.resolved +++ b/Package.resolved @@ -6,8 +6,8 @@ "repositoryURL": "https://github.com/swift-aws/aws-sdk-swift.git", "state": { "branch": null, - "revision": "b66a8ad7f949ef80a503069603ad1abd240e7f7d", - "version": "2.0.2" + "revision": "bc557114b6c16f63780d35ac2c5c21bf5c9b8f12", + "version": "3.0.0" } }, { @@ -15,8 +15,8 @@ "repositoryURL": "https://github.com/swift-aws/aws-sdk-swift-core.git", "state": { "branch": null, - "revision": "e1d6f9515f95dacd21758e033c27b15a715aeb21", - "version": "2.0.0-rc.3" + "revision": "254c1e3f85414bf0f529f47cc081be2bd0c12c1c", + "version": "3.0.1" } }, { @@ -24,8 +24,8 @@ "repositoryURL": "https://github.com/noppoMan/HexavilleFramework.git", "state": { "branch": null, - "revision": "6edcc3a4e0f4a37e21846b817e27a4f26513d840", - "version": "1.0.0-rc.1" + "revision": "9430c81699849c065b41b1f6b74ef704fa64247b", + "version": "1.0.0-rc.3" } }, { @@ -51,8 +51,8 @@ "repositoryURL": "https://github.com/apple/swift-nio.git", "state": { "branch": null, - "revision": "a20e129c22ad00a51c902dca54a5456f90664780", - "version": "1.12.0" + "revision": "ba7970fe396e8198b84c6c1b44b38a1d4e2eb6bd", + "version": "1.14.1" } }, { @@ -60,8 +60,8 @@ "repositoryURL": "https://github.com/apple/swift-nio-ssl.git", "state": { "branch": null, - "revision": "db16c3a90b101bb53b26a58867a344ad428072e0", - "version": "1.3.2" + "revision": "0f3999f3e3c359cc74480c292644c3419e44a12f", + "version": "1.4.0" } }, { @@ -87,8 +87,8 @@ "repositoryURL": "https://github.com/jakeheis/SwiftCLI.git", "state": { "branch": null, - "revision": "fb076cba39c679da4e27813518d8860d8815a25b", - "version": "5.2.1" + "revision": "2f9325de7dcaa368ce5a3710b04d9df572725b14", + "version": "5.3.0" } }, { @@ -96,8 +96,8 @@ "repositoryURL": "https://github.com/IBM-Swift/SwiftyJSON.git", "state": { "branch": null, - "revision": "693d2d28fe2f91aedf02c3754baade625fd97c46", - "version": "17.0.2" + "revision": "f2612ea3ac29996ae9601bdcb00cc1c29e26f104", + "version": "17.0.4" } } ] diff --git a/Package.swift b/Package.swift index 5a14e0a..6e926b7 100644 --- a/Package.swift +++ b/Package.swift @@ -10,7 +10,7 @@ let package = Package( .executable(name: "dynamodb-session-store-example", targets: ["DynamodbSessionStoreExample"]), ], dependencies: [ - .package(url: "https://github.com/swift-aws/aws-sdk-swift.git", .upToNextMajor(from: "2.0.2")), + .package(url: "https://github.com/swift-aws/aws-sdk-swift.git", .upToNextMajor(from: "3.0.0")), .package(url: "https://github.com/noppoMan/HexavilleFramework.git", .upToNextMajor(from: "1.0.0-rc.1")) ], targets: [ diff --git a/Sources/DynamodbSessionStore/DynamodbSessionStore.swift b/Sources/DynamodbSessionStore/DynamodbSessionStore.swift index aaef821..fbf6fe2 100644 --- a/Sources/DynamodbSessionStore/DynamodbSessionStore.swift +++ b/Sources/DynamodbSessionStore/DynamodbSessionStore.swift @@ -24,11 +24,21 @@ public struct DynamodbSessionStore: SessionStoreProvider { public func read(forKey: String) throws -> [String : Any]? { let input = DynamoDB.GetItemInput( - key: ["session_id": DynamoDB.AttributeValue(s: forKey)], consistentRead: true, + key: ["session_id": DynamoDB.AttributeValue(s: forKey)], tableName: tableName ) - let result = try dynamodb.getItem(input) + + let result: DynamoDB.GetItemOutput = try executeSync { done in + do { + try self.dynamodb.getItem(input).whenSuccess { response in + done(nil, response) + } + } catch { + done(error, nil) + } + } + guard let item = result.item?["value"], let jsonStr = item.s else { throw DynamodbSessionStoreError.couldNotFindItem } @@ -57,7 +67,16 @@ public struct DynamodbSessionStore: SessionStoreProvider { tableName: tableName ) - _ = try dynamodb.putItem(input) + + try executeSyncWithoutReturnValue { done in + do { + try self.dynamodb.putItem(input).whenSuccess { response in + done(nil) + } + } catch { + done(error) + } + } } public func delete(forKey: String) throws { @@ -65,7 +84,66 @@ public struct DynamodbSessionStore: SessionStoreProvider { key: ["session_id" : DynamoDB.AttributeValue(s: forKey)], tableName: tableName ) - _ = try dynamodb.deleteItem(input) + + try executeSyncWithoutReturnValue { done in + do { + try self.dynamodb.deleteItem(input).whenSuccess { response in + done(nil) + } + } catch { + done(error) + } + } + } +} + + +func executeSync(_ fn: (@escaping (Error?, T?) -> Void) -> Void) throws -> T { + let group = DispatchGroup() + group.enter() + + var _error: Error? + var _result: T? + + fn { error, result in + if error != nil { + _error = error + return + } + + _result = result + + group.leave() + } + + group.wait() + + if let error = _error { + throw error + } + + return _result! +} + + +func executeSyncWithoutReturnValue(_ fn: (@escaping (Error?) -> Void) -> Void) throws { + let group = DispatchGroup() + group.enter() + + var _error: Error? + + fn { error in + if error != nil { + _error = error + return + } + + group.leave() } + group.wait() + + if let error = _error { + throw error + } } diff --git a/Sources/DynamodbSessionStoreExample/main.swift b/Sources/DynamodbSessionStoreExample/main.swift index 840c3bc..90766f5 100644 --- a/Sources/DynamodbSessionStoreExample/main.swift +++ b/Sources/DynamodbSessionStoreExample/main.swift @@ -1,7 +1,7 @@ import Foundation import HexavilleFramework import DynamodbSessionStore -import SwiftAWSDynamodb +import DynamoDB let app = HexavilleFramework() @@ -9,7 +9,7 @@ let session = SessionMiddleware( cookieAttribute: CookieAttribute(expiration: 3600, httpOnly: true, secure: false), store: DynamodbSessionStore( tableName: ProcessInfo.processInfo.environment["DYNAMODB_SESSION_TABLE_NAME"] ?? "test-table", - dynamodb: Dynamodb() + dynamodb: DynamoDB() ) ) @@ -22,7 +22,7 @@ app.use { req, context in var router = Router() -router.use(.get, "/") { req, context in +router.use(.GET, "/") { req, context in if let now = context.session?["now"] { return Response(body: "current time is: \(now)") } else { diff --git a/Sources/DynamodbSessionStoreTableManager/main.swift b/Sources/DynamodbSessionStoreTableManager/main.swift index a3690bd..28ec974 100644 --- a/Sources/DynamodbSessionStoreTableManager/main.swift +++ b/Sources/DynamodbSessionStoreTableManager/main.swift @@ -17,17 +17,17 @@ class CreateCommand: Command { func execute() throws { let dynamodb = DynamoDB(endpoint: endpoint.value) let input = DynamoDB.CreateTableInput( - provisionedThroughput: DynamoDB.ProvisionedThroughput( - readCapacityUnits: Int64(readCapacityUnits.value ?? 10), - writeCapacityUnits: Int64(writeCapacityUnits.value ?? 10) - ), - tableName: tableName.value, attributeDefinitions: [ DynamoDB.AttributeDefinition(attributeName: "session_id", attributeType: .s), ], keySchema: [ - DynamoDB.KeySchemaElement(keyType: .hash, attributeName: "session_id") - ] + DynamoDB.KeySchemaElement(attributeName: "session_id", keyType: .hash) + ], + provisionedThroughput: DynamoDB.ProvisionedThroughput( + readCapacityUnits: Int64(readCapacityUnits.value ?? 10), + writeCapacityUnits: Int64(writeCapacityUnits.value ?? 10) + ), + tableName: tableName.value ) do { @@ -76,8 +76,8 @@ class CreateCommand: Command { print("Applying updateTimeToLive configuration to \(tableName.value)....") let updateTimeToLiveInput = DynamoDB.UpdateTimeToLiveInput( - timeToLiveSpecification: timeToLiveSpecificationInput, - tableName: tableName.value + tableName: tableName.value, + timeToLiveSpecification: timeToLiveSpecificationInput ) _ = try dynamodb.updateTimeToLive(updateTimeToLiveInput)