93 lines
2.4 KiB
Swift
93 lines
2.4 KiB
Swift
import TIFoundationUtils
|
|
import Foundation
|
|
|
|
enum APIDateFormat: String, DateFormat {
|
|
case yyyyMMddTHHmmssSZ = "yyyy-MM-dd'T'HH:mm:ss.SZ"
|
|
}
|
|
|
|
struct Token: Codable {
|
|
|
|
private enum CodingKeys: String, CodingKey {
|
|
case name
|
|
case value
|
|
case expirationDate = "expiration"
|
|
}
|
|
|
|
var name: String
|
|
var value: String
|
|
var expirationDate: Date
|
|
|
|
init(from decoder: Decoder) throws {
|
|
|
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
|
name = try container.decode(String.self, forKey: .name)
|
|
value = try container.decode(String.self, forKey: .value)
|
|
|
|
let dateFormatter = try decoder.userInfo.dateFormatter(for: APIDateFormat.yyyyMMddTHHmmssSZ)
|
|
|
|
expirationDate = try container.decodeDate(forKey: .expirationDate,
|
|
using: dateFormatter)
|
|
}
|
|
|
|
func encode(to encoder: Encoder) throws {
|
|
var container = encoder.container(keyedBy: CodingKeys.self)
|
|
|
|
try container.encode(name, forKey: .name)
|
|
try container.encode(value, forKey: .value)
|
|
|
|
let dateFormatter = try encoder.userInfo.dateFormatter(for: APIDateFormat.yyyyMMddTHHmmssSZ)
|
|
|
|
try container.encode(date: expirationDate,
|
|
forKey: .expirationDate,
|
|
using: dateFormatter)
|
|
}
|
|
}
|
|
|
|
let reusePool = DateFormattersReusePool()
|
|
|
|
let decoder = JSONDecoder()
|
|
decoder.userInfo[.dateFormattersReusePool!] = reusePool
|
|
|
|
let tokenJSON = """
|
|
{
|
|
"name": "access",
|
|
"value": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJwYXlsb2FkIjoibW9jayIsImlhdCI6MTYyNTU4NTU0MSwiZXhwIjoxNjI1NTg5MTQxfQ.zkdLwh6M7IPcTA8PY5gsfUzR-UTGzpWUtYqZur3RxqA",
|
|
"expiration": "2021-07-06T16:32:21.211Z"
|
|
}
|
|
"""
|
|
|
|
let token = try decoder.decode(Token.self, from: tokenJSON.data(using: .utf8)!)
|
|
|
|
debugPrint(token)
|
|
|
|
|
|
enum ProjectDateFormat: String, DateFormat {
|
|
|
|
case defaultDate = "MM/dd/yyyy"
|
|
|
|
case yyyyMMdd = "yyyy-MM-dd"
|
|
|
|
case MMMM = "MMMM"
|
|
|
|
case dMMMM = "d MMMM"
|
|
|
|
case dMMM = "d MMM"
|
|
|
|
func configure(dateFormatter: DateFormatter) {
|
|
dateFormatter.dateFormat = rawValue
|
|
|
|
switch self {
|
|
case .dMMM:
|
|
dateFormatter.dateStyle = .medium
|
|
default:
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
let dateFormattingService = DateFormattingService(reusePool: reusePool)
|
|
|
|
let formattedDate = dateFormattingService.string(from: Date(), using: ProjectDateFormat.dMMM)
|
|
|
|
debugPrint(formattedDate)
|