Skip to content

Commit ff3326e

Browse files
Refactor Operation
1 parent 070b204 commit ff3326e

File tree

3 files changed

+11
-19
lines changed

3 files changed

+11
-19
lines changed

Examples/LambdaFunctions/Sources/ProductAPI/ProductLambda.swift

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,9 @@ enum Operation: String {
2525
case update
2626
case delete
2727
case list
28-
case unknown
2928
}
3029

31-
struct EmptyResponse: Codable {
32-
33-
}
30+
struct EmptyResponse: Codable {}
3431

3532
struct ProductLambda: LambdaHandler {
3633

@@ -61,35 +58,34 @@ struct ProductLambda: LambdaHandler {
6158
return tableName
6259
}
6360

64-
init(context: Lambda.InitializationContext) {
65-
66-
let handler = Lambda.env("_HANDLER") ?? ""
67-
self.operation = Operation(rawValue: handler) ?? .unknown
61+
init(context: Lambda.InitializationContext) throws {
6862

63+
guard let handler = Lambda.env("_HANDLER"),
64+
let operation = Operation(rawValue: handler) else {
65+
throw APIError.invalidHandler
66+
}
67+
self.operation = operation
6968
self.region = Self.currentRegion()
70-
logger.info("\(Self.currentRegion())")
7169

7270
let lambdaRuntimeTimeout: TimeAmount = .seconds(dbTimeout)
7371
let timeout = HTTPClient.Configuration.Timeout(
7472
connect: lambdaRuntimeTimeout,
7573
read: lambdaRuntimeTimeout
7674
)
77-
75+
7876
let configuration = HTTPClient.Configuration(timeout: timeout)
7977
self.httpClient = HTTPClient(
8078
eventLoopGroupProvider: .shared(context.eventLoop),
8179
configuration: configuration
8280
)
8381

8482
self.db = AWSDynamoDB.DynamoDB(region: region, httpClientProvider: .shared(self.httpClient))
85-
logger.info("DynamoDB")
86-
self.tableName = (try? Self.tableName()) ?? ""
87-
83+
self.tableName = try Self.tableName()
84+
8885
self.service = ProductService(
8986
db: db,
9087
tableName: tableName
9188
)
92-
logger.info("ProductService")
9389
}
9490

9591
func handle(

Examples/LambdaFunctions/Sources/ProductAPI/ProductLambdaHandler.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,6 @@ struct ProductLambdaHandler: EventLoopLambdaHandler {
102102
}
103103
}
104104
return list
105-
case .unknown:
106-
logger.info("unknown")
107-
let value = APIGateway.V2.Response(with: APIError.handlerNotFound, statusCode: .forbidden)
108-
return context.eventLoop.makeSucceededFuture(value)
109105
}
110106
}
111107

Examples/LambdaFunctions/Sources/ProductAPI/ProductService.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public enum APIError: Error {
2020
case invalidItem
2121
case tableNameNotFound
2222
case invalidRequest
23-
case handlerNotFound
23+
case invalidHandler
2424
}
2525

2626
extension Date {

0 commit comments

Comments
 (0)