2022-02-21 00:01:53 +01:00
|
|
|
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
// MARK: - Decoding
|
|
|
|
|
|
|
|
extension OpenGroupAPI.Dependencies {
|
|
|
|
static let userInfoKey: CodingUserInfoKey = CodingUserInfoKey(rawValue: "io.oxen.dependencies.codingOptions")!
|
|
|
|
}
|
|
|
|
|
|
|
|
public extension Data {
|
2022-03-04 06:17:03 +01:00
|
|
|
func decoded<T: Decodable>(as type: T.Type, using dependencies: OpenGroupAPI.Dependencies = OpenGroupAPI.Dependencies()) throws -> T {
|
2022-02-21 00:01:53 +01:00
|
|
|
do {
|
|
|
|
let decoder: JSONDecoder = JSONDecoder()
|
|
|
|
decoder.userInfo = [ OpenGroupAPI.Dependencies.userInfoKey: dependencies ]
|
|
|
|
|
|
|
|
return try decoder.decode(type, from: self)
|
|
|
|
}
|
2022-03-04 06:17:03 +01:00
|
|
|
catch {
|
|
|
|
throw HTTP.Error.parsingFailed
|
2022-02-21 00:01:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|