From e6af77cedcc9d3723e85e4c1a81360cf5e404079 Mon Sep 17 00:00:00 2001 From: 417-72KI <417.72ki@gmail.com> Date: Mon, 3 Jul 2023 11:40:44 +0900 Subject: [PATCH] type for `load/post` should be just `Decodable` --- Sources/RequestKit/JSONPostRouter.swift | 23 ++++++++++---------- Sources/RequestKit/Router.swift | 29 +++++++++++++------------ 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/Sources/RequestKit/JSONPostRouter.swift b/Sources/RequestKit/JSONPostRouter.swift index 0223180..bdd0b10 100644 --- a/Sources/RequestKit/JSONPostRouter.swift +++ b/Sources/RequestKit/JSONPostRouter.swift @@ -5,18 +5,19 @@ import FoundationNetworking public protocol JSONPostRouter: Router { func postJSON(_ session: RequestKitURLSession, expectedResultType: T.Type, completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol? - func post(_ session: RequestKitURLSession, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy?, expectedResultType: T.Type, - completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol? - func post(_ session: RequestKitURLSession, decoder: JSONDecoder, expectedResultType: T.Type, completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol? + func post(_ session: RequestKitURLSession, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy?, expectedResultType: T.Type, + completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol? + func post(_ session: RequestKitURLSession, decoder: JSONDecoder, expectedResultType: T.Type, completion: @escaping (_ json: T?, _ error: Error?) -> Void) + -> URLSessionDataTaskProtocol? #if compiler(>=5.5.2) && canImport(_Concurrency) @available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *) func postJSON(_ session: RequestKitURLSession, expectedResultType: T.Type) async throws -> T? @available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *) - func post(_ session: RequestKitURLSession, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy?, expectedResultType: T.Type) async throws -> T + func post(_ session: RequestKitURLSession, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy?, expectedResultType: T.Type) async throws -> T @available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *) - func post(_ session: RequestKitURLSession, decoder: JSONDecoder, expectedResultType: T.Type) async throws -> T + func post(_ session: RequestKitURLSession, decoder: JSONDecoder, expectedResultType: T.Type) async throws -> T #endif } @@ -91,8 +92,8 @@ public extension JSONPostRouter { } #endif - func post(_ session: RequestKitURLSession = URLSession.shared, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy?, expectedResultType: T.Type, - completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol? + func post(_ session: RequestKitURLSession = URLSession.shared, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy?, expectedResultType: T.Type, + completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol? { let decoder = JSONDecoder() if let dateDecodingStrategy = dateDecodingStrategy { @@ -101,8 +102,8 @@ public extension JSONPostRouter { return post(session, decoder: decoder, expectedResultType: expectedResultType, completion: completion) } - func post(_ session: RequestKitURLSession = URLSession.shared, decoder: JSONDecoder = JSONDecoder(), expectedResultType _: T.Type, - completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol? + func post(_ session: RequestKitURLSession = URLSession.shared, decoder: JSONDecoder = JSONDecoder(), expectedResultType _: T.Type, + completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol? { guard let request = request() else { return nil @@ -148,7 +149,7 @@ public extension JSONPostRouter { #if compiler(>=5.5.2) && canImport(_Concurrency) @available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *) - func post(_ session: RequestKitURLSession = URLSession.shared, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy?, expectedResultType: T.Type) async throws -> T { + func post(_ session: RequestKitURLSession = URLSession.shared, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy?, expectedResultType: T.Type) async throws -> T { let decoder = JSONDecoder() if let dateDecodingStrategy = dateDecodingStrategy { decoder.dateDecodingStrategy = dateDecodingStrategy @@ -157,7 +158,7 @@ public extension JSONPostRouter { } @available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *) - func post(_ session: RequestKitURLSession, decoder: JSONDecoder = JSONDecoder(), expectedResultType _: T.Type) async throws -> T { + func post(_ session: RequestKitURLSession, decoder: JSONDecoder = JSONDecoder(), expectedResultType _: T.Type) async throws -> T { guard let request = request() else { throw NSError(domain: configuration.errorDomain, code: -876, userInfo: nil) } diff --git a/Sources/RequestKit/Router.swift b/Sources/RequestKit/Router.swift index f09a1f4..3f54582 100644 --- a/Sources/RequestKit/Router.swift +++ b/Sources/RequestKit/Router.swift @@ -58,17 +58,18 @@ public protocol Router { func urlQuery(_ parameters: [String: Any]) -> [URLQueryItem]? func request(_ urlComponents: URLComponents, parameters: [String: Any]) -> URLRequest? - func loadJSON(_ session: RequestKitURLSession, expectedResultType: T.Type, completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol? - func load(_ session: RequestKitURLSession, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy?, expectedResultType: T.Type, - completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol? - func load(_ session: RequestKitURLSession, decoder: JSONDecoder, expectedResultType: T.Type, completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol? + func loadJSON(_ session: RequestKitURLSession, expectedResultType: T.Type, completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol? + func load(_ session: RequestKitURLSession, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy?, expectedResultType: T.Type, + completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol? + func load(_ session: RequestKitURLSession, decoder: JSONDecoder, expectedResultType: T.Type, completion: @escaping (_ json: T?, _ error: Error?) -> Void) + -> URLSessionDataTaskProtocol? func request() -> URLRequest? #if compiler(>=5.5.2) && canImport(_Concurrency) @available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *) - func load(_ session: RequestKitURLSession, decoder: JSONDecoder, expectedResultType _: T.Type) async throws -> T + func load(_ session: RequestKitURLSession, decoder: JSONDecoder, expectedResultType _: T.Type) async throws -> T @available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *) - func load(_ session: RequestKitURLSession, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy?, expectedResultType: T.Type) async throws -> T + func load(_ session: RequestKitURLSession, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy?, expectedResultType: T.Type) async throws -> T @available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *) func load(_ session: RequestKitURLSession) async throws #endif @@ -150,14 +151,14 @@ public extension Router { } @available(*, deprecated, message: "Plase use `load` method instead") - func loadJSON(_ session: RequestKitURLSession = URLSession.shared, expectedResultType: T.Type, - completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol? + func loadJSON(_ session: RequestKitURLSession = URLSession.shared, expectedResultType: T.Type, + completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol? { return load(session, expectedResultType: expectedResultType, completion: completion) } - func load(_ session: RequestKitURLSession = URLSession.shared, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy?, expectedResultType: T.Type, - completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol? + func load(_ session: RequestKitURLSession = URLSession.shared, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy?, expectedResultType: T.Type, + completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol? { let decoder = JSONDecoder() if let dateDecodingStrategy = dateDecodingStrategy { @@ -166,8 +167,8 @@ public extension Router { return load(session, decoder: decoder, expectedResultType: expectedResultType, completion: completion) } - func load(_ session: RequestKitURLSession = URLSession.shared, decoder: JSONDecoder = JSONDecoder(), expectedResultType _: T.Type, - completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol? + func load(_ session: RequestKitURLSession = URLSession.shared, decoder: JSONDecoder = JSONDecoder(), expectedResultType _: T.Type, + completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol? { guard let request = request() else { return nil @@ -231,7 +232,7 @@ public extension Router { #if compiler(>=5.5.2) && canImport(_Concurrency) @available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *) public extension Router { - func load(_ session: RequestKitURLSession = URLSession.shared, decoder: JSONDecoder = JSONDecoder(), expectedResultType _: T.Type) async throws -> T { + func load(_ session: RequestKitURLSession = URLSession.shared, decoder: JSONDecoder = JSONDecoder(), expectedResultType _: T.Type) async throws -> T { guard let request = request() else { throw NSError(domain: configuration.errorDomain, code: -876, userInfo: nil) } @@ -251,7 +252,7 @@ public extension Router { return try decoder.decode(T.self, from: responseTuple.0) } - func load(_ session: RequestKitURLSession = URLSession.shared, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy?, expectedResultType: T.Type) async throws -> T { + func load(_ session: RequestKitURLSession = URLSession.shared, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy?, expectedResultType: T.Type) async throws -> T { let decoder = JSONDecoder() if let dateDecodingStrategy = dateDecodingStrategy { decoder.dateDecodingStrategy = dateDecodingStrategy