Skip to content

Commit 49aedc7

Browse files
Improve formatting and remove unused import
1 parent 46ba794 commit 49aedc7

File tree

6 files changed

+198
-186
lines changed

6 files changed

+198
-186
lines changed

Examples/LambdaFunctions/Sources/ProductAPI/APIGateway+Extensions.swift

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,37 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
import Foundation
1615
import AWSLambdaEvents
16+
import Foundation
1717

18-
public extension APIGateway.V2.Request {
19-
func object<T: Codable>() throws -> T {
18+
extension APIGateway.V2.Request {
19+
public func object<T: Codable>() throws -> T {
2020
let decoder = JSONDecoder()
2121
guard let body = self.body,
22-
let dataBody = body.data(using: .utf8) else {
22+
let dataBody = body.data(using: .utf8)
23+
else {
2324
throw APIError.invalidRequest
2425
}
2526
return try decoder.decode(T.self, from: dataBody)
2627
}
2728
}
2829

29-
let defaultHeaders = ["Content-Type": "application/json",
30-
"Access-Control-Allow-Origin": "*",
31-
"Access-Control-Allow-Methods": "OPTIONS,GET,POST,PUT,DELETE",
32-
"Access-Control-Allow-Credentials": "true"]
33-
3430
extension APIGateway.V2.Response {
3531

32+
static let defaultHeaders = [
33+
"Content-Type": "application/json",
34+
"Access-Control-Allow-Origin": "*",
35+
"Access-Control-Allow-Methods": "OPTIONS,GET,POST,PUT,DELETE",
36+
"Access-Control-Allow-Credentials": "true",
37+
]
38+
3639
init(with error: Error, statusCode: AWSLambdaEvents.HTTPResponseStatus) {
3740

3841
self.init(
3942
statusCode: statusCode,
40-
headers: defaultHeaders,
43+
headers: APIGateway.V2.Response.defaultHeaders,
4144
multiValueHeaders: nil,
42-
body: "{\"message\":\"\(error.localizedDescription)\"}",
45+
body: "{\"message\":\"\(String(describing: error))\"}",
4346
isBase64Encoded: false
4447
)
4548
}
@@ -53,7 +56,7 @@ extension APIGateway.V2.Response {
5356
}
5457
self.init(
5558
statusCode: statusCode,
56-
headers: defaultHeaders,
59+
headers: APIGateway.V2.Response.defaultHeaders,
5760
multiValueHeaders: nil,
5861
body: body,
5962
isBase64Encoded: false
@@ -62,7 +65,6 @@ extension APIGateway.V2.Response {
6265
}
6366

6467
init<Out: Encodable>(with result: Result<Out, Error>, statusCode: AWSLambdaEvents.HTTPResponseStatus) {
65-
6668
switch result {
6769
case .success(let value):
6870
self.init(with: value, statusCode: statusCode)

Examples/LambdaFunctions/Sources/ProductAPI/Product+DynamoDB.swift

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
import Foundation
1615
import AWSDynamoDB
1716

1817
public struct ProductField {
@@ -23,25 +22,28 @@ public struct ProductField {
2322
static let updatedAt = "updatedAt"
2423
}
2524

26-
public extension Product {
27-
var dynamoDictionary: [String : DynamoDB.AttributeValue] {
28-
var dictionary = [ProductField.sku: DynamoDB.AttributeValue(s:sku),
29-
ProductField.name: DynamoDB.AttributeValue(s:name),
30-
ProductField.description: DynamoDB.AttributeValue(s:description)]
25+
extension Product {
26+
public var dynamoDictionary: [String: DynamoDB.AttributeValue] {
27+
var dictionary = [
28+
ProductField.sku: DynamoDB.AttributeValue(s: sku),
29+
ProductField.name: DynamoDB.AttributeValue(s: name),
30+
ProductField.description: DynamoDB.AttributeValue(s: description),
31+
]
3132
if let createdAt = createdAt {
32-
dictionary[ProductField.createdAt] = DynamoDB.AttributeValue(s:createdAt)
33+
dictionary[ProductField.createdAt] = DynamoDB.AttributeValue(s: createdAt)
3334
}
3435

3536
if let updatedAt = updatedAt {
36-
dictionary[ProductField.updatedAt] = DynamoDB.AttributeValue(s:updatedAt)
37+
dictionary[ProductField.updatedAt] = DynamoDB.AttributeValue(s: updatedAt)
3738
}
3839
return dictionary
3940
}
4041

41-
init(dictionary: [String: DynamoDB.AttributeValue]) throws {
42+
public init(dictionary: [String: DynamoDB.AttributeValue]) throws {
4243
guard let name = dictionary[ProductField.name]?.s,
4344
let sku = dictionary[ProductField.sku]?.s,
44-
let description = dictionary[ProductField.description]?.s else {
45+
let description = dictionary[ProductField.description]?.s
46+
else {
4547
throw APIError.invalidItem
4648
}
4749
self.name = name

Examples/LambdaFunctions/Sources/ProductAPI/Product.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,10 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
import Foundation
16-
1715
public struct Product: Codable {
1816
public let sku: String
1917
public let name: String
2018
public let description: String
2119
public var createdAt: String?
2220
public var updatedAt: String?
2321
}
24-

Examples/LambdaFunctions/Sources/ProductAPI/ProductLambda.swift

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,12 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
import Foundation
16-
#if canImport(FoundationNetworking)
17-
import FoundationNetworking
18-
#endif
19-
import AWSLambdaRuntime
2015
import AWSDynamoDB
21-
import NIO
22-
import Logging
23-
import AsyncHTTPClient
2416
import AWSLambdaEvents
17+
import AWSLambdaRuntime
18+
import AsyncHTTPClient
19+
import Logging
20+
import NIO
2521

2622
enum Operation: String {
2723
case create
@@ -42,19 +38,19 @@ struct ProductLambda: LambdaHandler {
4238
typealias In = APIGateway.V2.Request
4339
typealias Out = APIGateway.V2.Response
4440

45-
let dbTimeout:Int64 = 30
41+
let dbTimeout: Int64 = 30
4642

4743
let region: Region
4844
let db: AWSDynamoDB.DynamoDB
4945
let service: ProductService
5046
let tableName: String
5147
let operation: Operation
52-
48+
5349
var httpClient: HTTPClient
5450

5551
static func currentRegion() -> Region {
5652

57-
if let awsRegion = ProcessInfo.processInfo.environment["AWS_REGION"] {
53+
if let awsRegion = Lambda.env("AWS_REGION") {
5854
let value = Region(rawValue: awsRegion)
5955
return value
6056

@@ -78,7 +74,7 @@ struct ProductLambda: LambdaHandler {
7874

7975
self.region = Self.currentRegion()
8076
logger.info("\(Self.currentRegion())")
81-
77+
8278
let lambdaRuntimeTimeout: TimeAmount = .seconds(dbTimeout)
8379
let timeout = HTTPClient.Configuration.Timeout(
8480
connect: lambdaRuntimeTimeout,
@@ -102,9 +98,14 @@ struct ProductLambda: LambdaHandler {
10298
)
10399
logger.info("ProductService")
104100
}
105-
106-
func handle(context: Lambda.Context, event: APIGateway.V2.Request, callback: @escaping (Result<APIGateway.V2.Response, Error>) -> Void) {
107-
let _ = ProductLambdaHandler(service: service, operation: operation).handle(context: context, event: event)
101+
102+
func handle(
103+
context: Lambda.Context, event: APIGateway.V2.Request,
104+
callback: @escaping (Result<APIGateway.V2.Response, Error>) -> Void
105+
) {
106+
let _ = ProductLambdaHandler(service: service, operation: operation).handle(
107+
context: context, event: event
108+
)
108109
.always { (result) in
109110
callback(result)
110111
}

0 commit comments

Comments
 (0)