Skip to content

Commit

Permalink
Remove all DateFormatters
Browse files Browse the repository at this point in the history
  • Loading branch information
fpseverino committed Aug 20, 2024
1 parent a52e732 commit a4e5312
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 89 deletions.
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ let package = Package(
.library(name: "Orders", targets: ["Orders"]),
],
dependencies: [
.package(url: "https://github.com/vapor/vapor.git", from: "4.102.1"),
.package(url: "https://github.com/vapor/vapor.git", from: "4.103.1"),
.package(url: "https://github.com/vapor/fluent.git", from: "4.11.0"),
.package(url: "https://github.com/vapor/apns.git", from: "4.1.0"),
.package(url: "https://github.com/vapor/apns.git", from: "4.2.0"),
.package(url: "https://github.com/vapor-community/Zip.git", from: "2.2.0"),
.package(url: "https://github.com/apple/swift-certificates.git", from: "1.5.0"),
// used in tests
Expand Down
6 changes: 1 addition & 5 deletions Sources/Orders/DTOs/OrdersForDeviceDTO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ struct OrdersForDeviceDTO: Content {

init(with orderIdentifiers: [String], maxDate: Date) {
self.orderIdentifiers = orderIdentifiers
self.lastModified = ISO8601DateFormatter.string(
from: maxDate,
timeZone: .init(secondsFromGMT: 0)!,
formatOptions: .withInternetDateTime
)
self.lastModified = String(maxDate.timeIntervalSince1970)
}
}
7 changes: 1 addition & 6 deletions Sources/Orders/Orders.docc/GettingStarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,17 +292,12 @@ fileprivate func orderHandler(_ req: Request) async throws -> Response {
throw Abort(.notFound)
}

let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss zzz"

let bundle = try await ordersService.generateOrderContent(for: orderData.order, on: req.db)
let body = Response.Body(data: bundle)
var headers = HTTPHeaders()
headers.add(name: .contentType, value: "application/vnd.apple.order")
headers.add(name: .contentDisposition, value: "attachment; filename=name.order")
headers.add(name: .lastModified, value: dateFormatter.string(from: order.updatedAt ?? Date.distantPast))
headers.lastModified = HTTPHeaders.LastModified(order.updatedAt ?? Date.distantPast)
headers.add(name: .contentTransferEncoding, value: "binary")
return Response(status: .ok, headers: headers, body: body)
}
Expand Down
12 changes: 6 additions & 6 deletions Sources/Orders/OrdersServiceCustom.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ extension OrdersServiceCustom {
func latestVersionOfOrder(req: Request) async throws -> Response {
logger?.debug("Called latestVersionOfOrder")

var ifModifiedSince = Date.distantPast
if let header = req.headers[.ifModifiedSince].first, let ims = app.dateFormatters.posix.date(from: header) {
var ifModifiedSince: TimeInterval = 0
if let header = req.headers[.ifModifiedSince].first, let ims = TimeInterval(header) {
ifModifiedSince = ims
}

Expand All @@ -121,13 +121,13 @@ extension OrdersServiceCustom {
throw Abort(.notFound)
}

guard ifModifiedSince < order.updatedAt ?? Date.distantPast else {
guard ifModifiedSince < order.updatedAt?.timeIntervalSince1970 ?? 0 else {
throw Abort(.notModified)
}

var headers = HTTPHeaders()
headers.add(name: .contentType, value: "application/vnd.apple.order")
headers.add(name: .lastModified, value: app.dateFormatters.posix.string(from: order.updatedAt ?? Date.distantPast))
headers.lastModified = HTTPHeaders.LastModified(order.updatedAt ?? Date.distantPast)
headers.add(name: .contentTransferEncoding, value: "binary")
return try await Response(
status: .ok,
Expand Down Expand Up @@ -193,8 +193,8 @@ extension OrdersServiceCustom {
let deviceIdentifier = req.parameters.get("deviceIdentifier")!

var query = R.for(deviceLibraryIdentifier: deviceIdentifier, orderTypeIdentifier: orderTypeIdentifier, on: req.db)
if let since: String = req.query["ordersModifiedSince"] {
let when = app.dateFormatters.iso8601.date(from: since) ?? Date.distantPast
if let since: TimeInterval = req.query["ordersModifiedSince"] {
let when = Date(timeIntervalSince1970: since)
query = query.filter(O.self, \._$updatedAt > when)
}

Expand Down
50 changes: 0 additions & 50 deletions Sources/PassKit/Application+DateFormatter.swift

This file was deleted.

14 changes: 2 additions & 12 deletions Sources/Passes/Passes.docc/GettingStarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,17 +316,12 @@ fileprivate func passHandler(_ req: Request) async throws -> Response {
throw Abort(.notFound)
}

let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss zzz"

let bundle = try await passesService.generatePassContent(for: passData.pass, on: req.db)
let body = Response.Body(data: bundle)
var headers = HTTPHeaders()
headers.add(name: .contentType, value: "application/vnd.apple.pkpass")
headers.add(name: .contentDisposition, value: "attachment; filename=name.pkpass")
headers.add(name: .lastModified, value: dateFormatter.string(from: pass.updatedAt ?? Date.distantPast))
headers.lastModified = HTTPHeaders.LastModified(pass.updatedAt ?? Date.distantPast)
headers.add(name: .contentTransferEncoding, value: "binary")
return Response(status: .ok, headers: headers, body: body)
}
Expand All @@ -346,17 +341,12 @@ fileprivate func passesHandler(_ req: Request) async throws -> Response {
let passesData = try await PassData.query(on: req.db).with(\.$pass).all()
let passes = passesData.map { $0.pass }

let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss zzz"

let bundle = try await passesService.generatePassesContent(for: passes, on: req.db)
let body = Response.Body(data: bundle)
var headers = HTTPHeaders()
headers.add(name: .contentType, value: "application/vnd.apple.pkpasses")
headers.add(name: .contentDisposition, value: "attachment; filename=name.pkpasses")
headers.add(name: .lastModified, value: dateFormatter.string(from: Date()))
headers.lastModified = HTTPHeaders.LastModified(Date())
headers.add(name: .contentTransferEncoding, value: "binary")
return Response(status: .ok, headers: headers, body: body)
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/Passes/PassesServiceCustom.swift
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ extension PassesServiceCustom {
func latestVersionOfPass(req: Request) async throws -> Response {
logger?.debug("Called latestVersionOfPass")

var ifModifiedSince = Date.distantPast
if let header = req.headers[.ifModifiedSince].first, let ims = app.dateFormatters.posix.date(from: header) {
var ifModifiedSince: TimeInterval = 0
if let header = req.headers[.ifModifiedSince].first, let ims = TimeInterval(header) {
ifModifiedSince = ims
}

Expand All @@ -203,13 +203,13 @@ extension PassesServiceCustom {
throw Abort(.notFound)
}

guard ifModifiedSince < pass.updatedAt ?? Date.distantPast else {
guard ifModifiedSince < pass.updatedAt?.timeIntervalSince1970 ?? 0 else {
throw Abort(.notModified)
}

var headers = HTTPHeaders()
headers.add(name: .contentType, value: "application/vnd.apple.pkpass")
headers.add(name: .lastModified, value: app.dateFormatters.posix.string(from: pass.updatedAt ?? Date.distantPast))
headers.lastModified = HTTPHeaders.LastModified(pass.updatedAt ?? Date.distantPast)
headers.add(name: .contentTransferEncoding, value: "binary")
return try await Response(
status: .ok,
Expand Down
6 changes: 3 additions & 3 deletions Tests/OrdersTests/OrdersTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ final class OrdersTests: XCTestCase {
"\(ordersURI)orders/\(order.orderTypeIdentifier)/\(order.requireID())",
headers: [
"Authorization": "AppleOrder \(order.authenticationToken)",
"If-Modified-Since": app.dateFormatters.posix.string(from: Date.distantPast)
"If-Modified-Since": "0"
],
afterResponse: { res async throws in
XCTAssertEqual(res.status, .ok)
Expand Down Expand Up @@ -112,13 +112,13 @@ final class OrdersTests: XCTestCase {

try await app.test(
.GET,
"\(ordersURI)devices/\(deviceLibraryIdentifier)/registrations/\(order.orderTypeIdentifier)?ordersModifiedSince=\(app.dateFormatters.iso8601.string(from: Date.distantPast))",
"\(ordersURI)devices/\(deviceLibraryIdentifier)/registrations/\(order.orderTypeIdentifier)?ordersModifiedSince=0",
afterResponse: { res async throws in
let orders = try res.content.decode(OrdersForDeviceDTO.self)
XCTAssertEqual(orders.orderIdentifiers.count, 1)
let orderID = try order.requireID()
XCTAssertEqual(orders.orderIdentifiers[0], orderID.uuidString)
XCTAssertEqual(orders.lastModified, app.dateFormatters.iso8601.string(from: order.updatedAt!))
XCTAssertEqual(orders.lastModified, String(order.updatedAt!.timeIntervalSince1970))
}
)

Expand Down
2 changes: 1 addition & 1 deletion Tests/PassesTests/PassesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ final class PassesTests: XCTestCase {
"\(passesURI)passes/\(pass.passTypeIdentifier)/\(pass.requireID())",
headers: [
"Authorization": "ApplePass \(pass.authenticationToken)",
"If-Modified-Since": app.dateFormatters.posix.string(from: Date.distantPast)
"If-Modified-Since": "0"
],
afterResponse: { res async throws in
XCTAssertEqual(res.status, .ok)
Expand Down

0 comments on commit a4e5312

Please sign in to comment.