Created
August 16, 2019 12:36
-
-
Save manishkkatoch/2ae182a0558c878e6a720319af829dd7 to your computer and use it in GitHub Desktop.
HATEOAS Link
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Link : Decodable, Equatable { | |
let url: String | |
let rel: String | |
let method: HttpMethod | |
let bodyJson: [String: DecodableValueType]? | |
enum CodingKeys: String,CodingKey { | |
case url = "href" | |
case rel, method | |
case bodyJson = "parameters" | |
} | |
init(from decoder:Decoder) throws { | |
let container = try decoder.container(keyedBy: CodingKeys.self) | |
self.url = try container.decode(String.self, forKey: .url) | |
self.rel = try container.decode(String.self, forKey: .rel) | |
self.bodyJson = try? container.decode([String:DecodableValueType].self, forKey: .bodyJson) | |
if let methodUnwrapped = try container.decodeIfPresent(String.self, forKey: .method) { | |
self.method = HttpMethod(rawValue: methodUnwrapped) ?? HttpMethod.GET | |
} else { | |
self.method = HttpMethod.GET | |
} | |
} | |
init(withUrl url: String) { | |
self.url = url | |
self.method = HttpMethod.GET | |
self.rel = "self" | |
self.bodyJson = nil | |
} | |
static func == (lhs: Link, rhs: Link) -> Bool { | |
return lhs.url == rhs.url && lhs.rel == rhs.rel && lhs.method == rhs.method | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment