Skip to content

Commit 8fce8ff

Browse files
Update swift-aws-lambda-runtime.git to 1.0.0-alpha.1
1 parent 1cfd925 commit 8fce8ff

12 files changed

+65
-362
lines changed

Products/Package.resolved

Lines changed: 24 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Products/Package.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ let package = Package(
1717
),
1818
],
1919
dependencies: [
20-
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "0.5.0"),
20+
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0-alpha.1"),
21+
.package(url: "https://github.com/swift-server/swift-aws-lambda-events.git", branch: "main"),
2122
.package(url: "https://github.com/soto-project/soto.git", from: "6.0.0"),
2223
.package(url: "https://github.com/apple/swift-log.git", from: "1.0.0"),
2324
],
@@ -35,7 +36,7 @@ let package = Package(
3536
name: "Products",
3637
dependencies: [
3738
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
38-
.product(name: "AWSLambdaEvents", package: "swift-aws-lambda-runtime"),
39+
.product(name: "AWSLambdaEvents", package: "swift-aws-lambda-events"),
3940
"ProductService"
4041
]
4142
),

Products/Sources/ProductService/ProductService.swift

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ public class ProductService {
6161
}
6262
}
6363

64-
#if compiler(>=5.5) && canImport(_Concurrency)
65-
6664
public extension ProductService {
6765

6866
func createItem(product: Product) async throws -> Product {
@@ -131,80 +129,3 @@ public extension ProductService {
131129
return data.items ?? []
132130
}
133131
}
134-
135-
#else
136-
137-
public extension ProductService {
138-
139-
func createItem(product: Product) -> EventLoopFuture<Product> {
140-
141-
var product = product
142-
let date = Date()
143-
product.createdAt = date.iso8601
144-
product.updatedAt = date.iso8601
145-
let input = DynamoDB.PutItemCodableInput(item: product, tableName: tableName)
146-
147-
return db.putItem(input).flatMap { _ -> EventLoopFuture<Product> in
148-
return self.readItem(key: product.sku)
149-
}
150-
}
151-
152-
func readItem(key: String) -> EventLoopFuture<Product> {
153-
let input = DynamoDB.GetItemInput(
154-
key: [Product.Field.sku: DynamoDB.AttributeValue.s(key)],
155-
tableName: tableName
156-
)
157-
return db.getItem(input, type: Product.self).flatMapThrowing { data -> Product in
158-
guard let product = data.item else {
159-
throw ProductError.notFound
160-
}
161-
return product
162-
}
163-
}
164-
165-
func updateItem(product: Product) -> EventLoopFuture<Product> {
166-
var product = product
167-
let date = Date()
168-
let updatedAt = date.iso8601
169-
product.updatedAt = date.iso8601
170-
171-
let input = DynamoDB.UpdateItemInput(
172-
conditionExpression: "attribute_exists(#createdAt)",
173-
expressionAttributeNames: [
174-
"#name": Product.Field.name,
175-
"#description": Product.Field.description,
176-
"#updatedAt": Product.Field.updatedAt,
177-
"#createdAt": Product.Field.createdAt
178-
],
179-
expressionAttributeValues: [
180-
":name": DynamoDB.AttributeValue.s(product.name),
181-
":description": DynamoDB.AttributeValue.s(product.description),
182-
":updatedAt": DynamoDB.AttributeValue.s(updatedAt)
183-
],
184-
key: [Product.Field.sku: DynamoDB.AttributeValue.s(product.sku)],
185-
returnValues: DynamoDB.ReturnValue.allNew,
186-
tableName: tableName,
187-
updateExpression: "SET #name = :name, #description = :description, #updatedAt = :updatedAt"
188-
)
189-
return db.updateItem(input).flatMap { _ -> EventLoopFuture<Product> in
190-
return self.readItem(key: product.sku)
191-
}
192-
}
193-
194-
func deleteItem(key: String) -> EventLoopFuture<Void> {
195-
let input = DynamoDB.DeleteItemInput(
196-
key: [Product.Field.sku: DynamoDB.AttributeValue.s(key)],
197-
tableName: tableName
198-
)
199-
return db.deleteItem(input).map { _ in Void() }
200-
}
201-
202-
func listItems() -> EventLoopFuture<[Product]> {
203-
let input = DynamoDB.ScanInput(tableName: tableName)
204-
return db.scan(input, type: Product.self).flatMapThrowing { data -> [Product] in
205-
return data.items ?? []
206-
}
207-
}
208-
}
209-
210-
#endif

Products/Sources/Products/APIGateway+Extensions.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public enum APIError: Error {
2323
case invalidHandler
2424
}
2525

26-
extension APIGateway.V2.Request {
26+
extension APIGatewayV2Request {
2727

2828
static private let decoder = JSONDecoder()
2929

@@ -37,7 +37,7 @@ extension APIGateway.V2.Request {
3737
}
3838
}
3939

40-
extension APIGateway.V2.Response {
40+
extension APIGatewayV2Response {
4141

4242
private static let encoder = JSONEncoder()
4343

@@ -48,24 +48,24 @@ extension APIGateway.V2.Response {
4848
"Access-Control-Allow-Methods": "OPTIONS,GET,POST,PUT,DELETE",
4949
"Access-Control-Allow-Credentials": "true",
5050
]
51-
51+
5252
public init(with error: Error, statusCode: AWSLambdaEvents.HTTPResponseStatus) {
5353
self.init(
5454
statusCode: statusCode,
55-
headers: APIGateway.V2.Response.defaultHeaders,
55+
headers: APIGatewayV2Response.defaultHeaders,
5656
body: "{\"message\":\"\(String(describing: error))\"}",
5757
isBase64Encoded: false
5858
)
5959
}
6060

61-
public init<Out: Encodable>(with object: Out, statusCode: AWSLambdaEvents.HTTPResponseStatus) {
61+
public init<Output: Encodable>(with object: Output, statusCode: AWSLambdaEvents.HTTPResponseStatus) {
6262
var body: String = "{}"
6363
if let data = try? Self.encoder.encode(object) {
6464
body = String(data: data, encoding: .utf8) ?? body
6565
}
6666
self.init(
6767
statusCode: statusCode,
68-
headers: APIGateway.V2.Response.defaultHeaders,
68+
headers: APIGatewayV2Response.defaultHeaders,
6969
body: body,
7070
isBase64Encoded: false
7171
)

Products/Sources/Products/AsyncProductLambda.swift

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import SotoDynamoDB
1616
import AWSLambdaEvents
1717
import AWSLambdaRuntime
18+
import AWSLambdaRuntimeCore
1819
import AsyncHTTPClient
1920
import Logging
2021
import NIO
@@ -29,13 +30,10 @@ enum Operation: String {
2930
}
3031

3132
struct EmptyResponse: Codable {}
32-
33-
#if compiler(>=5.5) && canImport(_Concurrency)
34-
35-
struct AsyncProductLambda: AsyncLambdaHandler {
33+
struct AsyncProductLambda: LambdaHandler {
3634

37-
typealias In = APIGateway.V2.Request
38-
typealias Out = APIGateway.V2.Response
35+
typealias Event = APIGatewayV2Request
36+
typealias Output = APIGatewayV2Response
3937

4038
let dbTimeout: Int64 = 30
4139
let region: Region
@@ -61,7 +59,7 @@ struct AsyncProductLambda: AsyncLambdaHandler {
6159
return tableName
6260
}
6361

64-
init(context: Lambda.InitializationContext) throws {
62+
init(context: LambdaInitializationContext) async throws {
6563

6664
guard let handler = Lambda.env("_HANDLER"),
6765
let operation = Operation(rawValue: handler) else {
@@ -92,9 +90,7 @@ struct AsyncProductLambda: AsyncLambdaHandler {
9290
)
9391
}
9492

95-
func handle(event: AWSLambdaEvents.APIGateway.V2.Request, context: AWSLambdaRuntimeCore.Lambda.Context) async throws -> AWSLambdaEvents.APIGateway.V2.Response {
96-
return await AsyncProductLambdaHandler(service: service, operation: operation).handle(context: context, event: event)
93+
func handle(_ event: AWSLambdaEvents.APIGatewayV2Request, context: AWSLambdaRuntimeCore.LambdaContext) async throws -> AWSLambdaEvents.APIGatewayV2Response {
94+
return await AsyncProductLambdaHandler(service: service, operation: operation).handle(context: context, event: event)
9795
}
9896
}
99-
100-
#endif

Products/Sources/Products/AsyncProductLambdaHandler.swift

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,15 @@ import AWSLambdaRuntime
2727
import Logging
2828
import NIO
2929

30-
#if compiler(>=5.5) && canImport(_Concurrency)
31-
3230
struct AsyncProductLambdaHandler {
3331

34-
typealias In = APIGateway.V2.Request
35-
typealias Out = APIGateway.V2.Response
32+
typealias Event = APIGatewayV2Request
33+
typealias Output = APIGatewayV2Response
3634

3735
let service: ProductService
3836
let operation: Operation
3937

40-
func handle(context: Lambda.Context, event: APIGateway.V2.Request) async -> APIGateway.V2.Response {
38+
func handle(context: AWSLambdaRuntimeCore.LambdaContext, event: APIGatewayV2Request) async -> APIGatewayV2Response {
4139

4240
switch self.operation {
4341
case .create:
@@ -53,66 +51,64 @@ struct AsyncProductLambdaHandler {
5351
}
5452
}
5553

56-
func createLambdaHandler(context: Lambda.Context, event: APIGateway.V2.Request) async -> APIGateway.V2.Response {
54+
func createLambdaHandler(context: AWSLambdaRuntimeCore.LambdaContext, event: APIGatewayV2Request) async -> APIGatewayV2Response {
5755
guard let product: Product = try? event.bodyObject() else {
5856
let error = APIError.invalidRequest
59-
return APIGateway.V2.Response(with: error, statusCode: .forbidden)
57+
return APIGatewayV2Response(with: error, statusCode: .forbidden)
6058
}
6159
do {
6260
let result = try await service.createItem(product: product)
63-
return APIGateway.V2.Response(with: result, statusCode: .created)
61+
return APIGatewayV2Response(with: result, statusCode: .created)
6462
} catch {
65-
return APIGateway.V2.Response(with: error, statusCode: .forbidden)
63+
return APIGatewayV2Response(with: error, statusCode: .forbidden)
6664
}
6765
}
6866

69-
func readLambdaHandler(context: Lambda.Context, event: APIGateway.V2.Request) async -> APIGateway.V2.Response {
67+
func readLambdaHandler(context: AWSLambdaRuntimeCore.LambdaContext, event: APIGatewayV2Request) async -> APIGatewayV2Response {
7068
guard let sku = event.pathParameters?["sku"] else {
7169
let error = APIError.invalidRequest
72-
return APIGateway.V2.Response(with: error, statusCode: .forbidden)
70+
return APIGatewayV2Response(with: error, statusCode: .forbidden)
7371
}
7472
do {
7573
let result = try await service.readItem(key: sku)
76-
return APIGateway.V2.Response(with: result, statusCode: .ok)
74+
return APIGatewayV2Response(with: result, statusCode: .ok)
7775
} catch {
78-
return APIGateway.V2.Response(with: error, statusCode: .notFound)
76+
return APIGatewayV2Response(with: error, statusCode: .notFound)
7977
}
8078
}
8179

82-
func updateLambdaHandler(context: Lambda.Context, event: APIGateway.V2.Request) async -> APIGateway.V2.Response {
80+
func updateLambdaHandler(context: AWSLambdaRuntimeCore.LambdaContext, event: APIGatewayV2Request) async -> APIGatewayV2Response {
8381
guard let product: Product = try? event.bodyObject() else {
8482
let error = APIError.invalidRequest
85-
return APIGateway.V2.Response(with: error, statusCode: .forbidden)
83+
return APIGatewayV2Response(with: error, statusCode: .forbidden)
8684
}
8785
do {
8886
let result = try await service.updateItem(product: product)
89-
return APIGateway.V2.Response(with: result, statusCode: .ok)
87+
return APIGatewayV2Response(with: result, statusCode: .ok)
9088
} catch {
91-
return APIGateway.V2.Response(with: error, statusCode: .notFound)
89+
return APIGatewayV2Response(with: error, statusCode: .notFound)
9290
}
9391
}
9492

95-
func deleteUpdateLambdaHandler(context: Lambda.Context, event: APIGateway.V2.Request) async -> APIGateway.V2.Response {
93+
func deleteUpdateLambdaHandler(context: AWSLambdaRuntimeCore.LambdaContext, event: APIGatewayV2Request) async -> APIGatewayV2Response {
9694
guard let sku = event.pathParameters?["sku"] else {
9795
let error = APIError.invalidRequest
98-
return APIGateway.V2.Response(with: error, statusCode: .forbidden)
96+
return APIGatewayV2Response(with: error, statusCode: .forbidden)
9997
}
10098
do {
10199
try await service.deleteItem(key: sku)
102-
return APIGateway.V2.Response(with: EmptyResponse(), statusCode: .ok)
100+
return APIGatewayV2Response(with: EmptyResponse(), statusCode: .ok)
103101
} catch {
104-
return APIGateway.V2.Response(with: error, statusCode: .notFound)
102+
return APIGatewayV2Response(with: error, statusCode: .notFound)
105103
}
106104
}
107105

108-
func listUpdateLambdaHandler(context: Lambda.Context, event: APIGateway.V2.Request) async -> APIGateway.V2.Response {
106+
func listUpdateLambdaHandler(context: AWSLambdaRuntimeCore.LambdaContext, event: APIGatewayV2Request) async -> APIGatewayV2Response {
109107
do {
110108
let result = try await service.listItems()
111-
return APIGateway.V2.Response(with: result, statusCode: .ok)
109+
return APIGatewayV2Response(with: result, statusCode: .ok)
112110
} catch {
113-
return APIGateway.V2.Response(with: error, statusCode: .forbidden)
111+
return APIGatewayV2Response(with: error, statusCode: .forbidden)
114112
}
115113
}
116114
}
117-
118-
#endif

0 commit comments

Comments
 (0)