Make HTTPConditionalGetInfo serializable via a dictionary.

This commit is contained in:
Brent Simmons 2017-09-26 13:16:46 -07:00
parent 2978d4420b
commit 8d9c9217dc
1 changed files with 30 additions and 5 deletions

View File

@ -8,30 +8,55 @@
import Foundation
public struct HTTPConditionalGetInfo: Codable {
public struct HTTPConditionalGetInfo {
public let lastModified: String?
public let etag: String?
public var isEmpty: Bool {
get {
return lastModified == nil && etag == nil
}
}
public init(lastModified: String?, etag: String?) {
public init?(lastModified: String?, etag: String?) {
if lastModified == nil && etag == nil {
return nil
}
self.lastModified = lastModified
self.etag = etag
}
public init(urlResponse: HTTPURLResponse) {
public init?(urlResponse: HTTPURLResponse) {
let lastModified = urlResponse.valueForHTTPHeaderField(HTTPResponseHeader.lastModified)
let etag = urlResponse.valueForHTTPHeaderField(HTTPResponseHeader.etag)
self.init(lastModified: lastModified, etag: etag)
}
public var dictionary: [String: String]? {
get {
if lastModified == nil && etag == nil {
return nil
}
var d = [String: String]()
if let lastModified = lastModified {
d[HTTPResponseHeader.lastModified] = lastModified
}
if let etag = etag {
d[HTTPResponseHeader.etag] = etag
}
return d
}
}
public init?(dictionary: [String: String]) {
self.init(lastModified: dictionary[HTTPResponseHeader.lastModified], etag: dictionary[HTTPResponseHeader.etag])
}
public func addRequestHeadersToURLRequest(_ urlRequest: NSMutableURLRequest) {
if let lastModified = lastModified {