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,23 +8,27 @@
import Foundation import Foundation
public struct HTTPConditionalGetInfo: Codable { public struct HTTPConditionalGetInfo {
public let lastModified: String? public let lastModified: String?
public let etag: String? public let etag: String?
public var isEmpty: Bool { public var isEmpty: Bool {
get { get {
return lastModified == nil && etag == nil 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.lastModified = lastModified
self.etag = etag self.etag = etag
} }
public init(urlResponse: HTTPURLResponse) { public init?(urlResponse: HTTPURLResponse) {
let lastModified = urlResponse.valueForHTTPHeaderField(HTTPResponseHeader.lastModified) let lastModified = urlResponse.valueForHTTPHeaderField(HTTPResponseHeader.lastModified)
let etag = urlResponse.valueForHTTPHeaderField(HTTPResponseHeader.etag) let etag = urlResponse.valueForHTTPHeaderField(HTTPResponseHeader.etag)
@ -32,6 +36,27 @@ public struct HTTPConditionalGetInfo: Codable {
self.init(lastModified: lastModified, etag: 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) { public func addRequestHeadersToURLRequest(_ urlRequest: NSMutableURLRequest) {
if let lastModified = lastModified { if let lastModified = lastModified {