Skip to content
This repository has been archived by the owner on Nov 23, 2024. It is now read-only.

Commit

Permalink
properly decode date objects for codable structs
Browse files Browse the repository at this point in the history
  • Loading branch information
radmakr committed Nov 23, 2023
1 parent eaa89ae commit 227588a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Sources/AlbyKit/Models/AccountService/AccountSummary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public struct AccountSummary: Codable, Sendable {
public let boostagramsCount: Int
public let currency: Currency
public let invoicesCount: Int
public let lastInvoiceAt: Date // "2022-06-02T08:40:08.000Z", // TODO: properly support Date format with Codable
public let lastInvoiceAt: Date
public let transactionsCount: Int
public let unit: Unit
}
4 changes: 3 additions & 1 deletion Sources/AlbyKit/Models/InvoicesService/Bolt11Invoice.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Foundation

public struct Bolt11Invoice: Codable, Sendable {
public let currency: Currency
public let createdAt: Int // 1693210330, // TODO: should this be a date?
public let createdAt: Date
public let expiry: Int
public let payee: String
public let msatoshi: Int
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation

public struct CreatedInvoice: Codable, Sendable {
public let expires_at: Date // "2022-06-02T08:31:15Z", // TODO: add proper codable date support
public let expires_at: Date
public let paymentHash: String
public let paymentRequest: String
}
8 changes: 4 additions & 4 deletions Sources/AlbyKit/Models/InvoicesService/Invoice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ public struct Invoice: Codable, Sendable {
public let amount: Int
public let boostagram: Boostagram?
public let comment: String?
public let createdAt: Date // "2022-07-05T17:02:20.343Z", TODO: date set properly in codable
public let creationDate: Int // 1657040540, TODO: this is a date but isn't the same format, can we make this work with Codabale
public let createdAt: Date
public let creationDate: Date
public let currency: Currency
public let customRecords: [String : String]?
public let descriptionHash: String?
public let expiresAt: Date // "2022-07-05T17:17:20.000Z", TODO: more date stuff
public let expiresAt: Date
public let expiry: Int
public let identifier: String
public let keysendMessage: String?
Expand All @@ -23,7 +23,7 @@ public struct Invoice: Codable, Sendable {
public let paymentRequest: String
public let rHashStr: String
public let settled: Bool?
public let settledAt: Date? // TODO: more date stuff "2022-07-05T17:02:20.000Z",
public let settledAt: Date?
public let state: InvoiceState
public let type: String
public let value: Int
Expand Down
44 changes: 20 additions & 24 deletions Sources/AlbyKit/Networking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,26 @@ extension JSONDecoder {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase

decoder.dateDecodingStrategy = .custom { decoder in
let container = try decoder.singleValueContainer()

if let dateStr = try? container.decode(String.self) {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ" ////// 2022-06-02T08:40:08.000Z 2022-06-02T08:31:15Z
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)

if let date = dateFormatter.date(from: dateStr) {
return date
}

} else if let dateInt = try? container.decode(Int.self) {
return Date(timeIntervalSince1970: TimeInterval(dateInt))
}

throw DecodingError.dataCorruptedError(in: container, debugDescription: "Invalid date format")
}

return decoder
}
}
Expand All @@ -24,30 +44,6 @@ let routerDelegate = AlbyRouterDelegate()
class AlbyRouterDelegate: NetworkRouterDelegate {
func intercept(_ request: inout URLRequest) async {
// TODO: add any headers here
// let errorMessage = """
//PODCASTINDEXKIT Error: your apiKey, secretKey, and userAgent were not set.
//Please follow the intructions in the README for setting up the PodcastIndexKit framework
//Hint: You must call the static setup(apiKey: String, apiSecret: String, userAgent: String) method before using the framework
//"""
// guard let apiKey = PodcastIndexKit.apiKey, let apiSecret = PodcastIndexKit.apiSecret, let userAgent = PodcastIndexKit.userAgent else { fatalError(errorMessage) }
//
// // prep for crypto
// let apiHeaderTime = String(Int(Date().timeIntervalSince1970))
// let data4Hash = apiKey + apiSecret + "\(apiHeaderTime)"
//
// // ======== Hash them to get the Authorization token ========
// let inputData = Data(data4Hash.utf8)
// let hashed = Insecure.SHA1.hash(data: inputData)
// let hashString = hashed.compactMap { String(format: "%02x", $0) }.joined()
//
// // set Headers
// request.addValue(apiHeaderTime, forHTTPHeaderField: "X-Auth-Date")
// request.addValue(apiKey, forHTTPHeaderField: "X-Auth-Key")
// request.addValue(hashString, forHTTPHeaderField: "Authorization")
// request.addValue(userAgent, forHTTPHeaderField: "User-Agent")


// request.addValue("application/json", forHTTPHeaderField: "Content-Type") // Doesn't need to be set here/ netweorking code sets this elsewhere
}

func shouldRetry(error: Error, attempts: Int) async throws -> Bool {
Expand Down

0 comments on commit 227588a

Please sign in to comment.