Skip to content

Commit

Permalink
Merge pull request #97 from 417-72KI/only-decodable
Browse files Browse the repository at this point in the history
Make type for `load/post` just `Decodable`
  • Loading branch information
pietbrauer authored Jul 10, 2023
2 parents 3eddc76 + e6af77c commit e4d905f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
23 changes: 12 additions & 11 deletions Sources/RequestKit/JSONPostRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ import FoundationNetworking

public protocol JSONPostRouter: Router {
func postJSON<T>(_ session: RequestKitURLSession, expectedResultType: T.Type, completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol?
func post<T: Codable>(_ session: RequestKitURLSession, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy?, expectedResultType: T.Type,
completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol?
func post<T: Codable>(_ session: RequestKitURLSession, decoder: JSONDecoder, expectedResultType: T.Type, completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol?
func post<T: Decodable>(_ session: RequestKitURLSession, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy?, expectedResultType: T.Type,
completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol?
func post<T: Decodable>(_ 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<T>(_ session: RequestKitURLSession, expectedResultType: T.Type) async throws -> T?

@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
func post<T: Codable>(_ session: RequestKitURLSession, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy?, expectedResultType: T.Type) async throws -> T
func post<T: Decodable>(_ 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<T: Codable>(_ session: RequestKitURLSession, decoder: JSONDecoder, expectedResultType: T.Type) async throws -> T
func post<T: Decodable>(_ session: RequestKitURLSession, decoder: JSONDecoder, expectedResultType: T.Type) async throws -> T
#endif
}

Expand Down Expand Up @@ -91,8 +92,8 @@ public extension JSONPostRouter {
}
#endif

func post<T: Codable>(_ session: RequestKitURLSession = URLSession.shared, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy?, expectedResultType: T.Type,
completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol?
func post<T: Decodable>(_ 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 {
Expand All @@ -101,8 +102,8 @@ public extension JSONPostRouter {
return post(session, decoder: decoder, expectedResultType: expectedResultType, completion: completion)
}

func post<T: Codable>(_ session: RequestKitURLSession = URLSession.shared, decoder: JSONDecoder = JSONDecoder(), expectedResultType _: T.Type,
completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol?
func post<T: Decodable>(_ 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
Expand Down Expand Up @@ -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<T: Codable>(_ session: RequestKitURLSession = URLSession.shared, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy?, expectedResultType: T.Type) async throws -> T {
func post<T: Decodable>(_ session: RequestKitURLSession = URLSession.shared, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy?, expectedResultType: T.Type) async throws -> T {
let decoder = JSONDecoder()
if let dateDecodingStrategy = dateDecodingStrategy {
decoder.dateDecodingStrategy = dateDecodingStrategy
Expand All @@ -157,7 +158,7 @@ public extension JSONPostRouter {
}

@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
func post<T: Codable>(_ session: RequestKitURLSession, decoder: JSONDecoder = JSONDecoder(), expectedResultType _: T.Type) async throws -> T {
func post<T: Decodable>(_ 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)
}
Expand Down
29 changes: 15 additions & 14 deletions Sources/RequestKit/Router.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,18 @@ public protocol Router {

func urlQuery(_ parameters: [String: Any]) -> [URLQueryItem]?
func request(_ urlComponents: URLComponents, parameters: [String: Any]) -> URLRequest?
func loadJSON<T: Codable>(_ session: RequestKitURLSession, expectedResultType: T.Type, completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol?
func load<T: Codable>(_ session: RequestKitURLSession, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy?, expectedResultType: T.Type,
completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol?
func load<T: Codable>(_ session: RequestKitURLSession, decoder: JSONDecoder, expectedResultType: T.Type, completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol?
func loadJSON<T: Decodable>(_ session: RequestKitURLSession, expectedResultType: T.Type, completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol?
func load<T: Decodable>(_ session: RequestKitURLSession, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy?, expectedResultType: T.Type,
completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol?
func load<T: Decodable>(_ 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<T: Codable>(_ session: RequestKitURLSession, decoder: JSONDecoder, expectedResultType _: T.Type) async throws -> T
func load<T: Decodable>(_ 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<T: Codable>(_ session: RequestKitURLSession, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy?, expectedResultType: T.Type) async throws -> T
func load<T: Decodable>(_ 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
Expand Down Expand Up @@ -150,14 +151,14 @@ public extension Router {
}

@available(*, deprecated, message: "Plase use `load` method instead")
func loadJSON<T: Codable>(_ session: RequestKitURLSession = URLSession.shared, expectedResultType: T.Type,
completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol?
func loadJSON<T: Decodable>(_ session: RequestKitURLSession = URLSession.shared, expectedResultType: T.Type,
completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol?
{
return load(session, expectedResultType: expectedResultType, completion: completion)
}

func load<T: Codable>(_ session: RequestKitURLSession = URLSession.shared, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy?, expectedResultType: T.Type,
completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol?
func load<T: Decodable>(_ 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 {
Expand All @@ -166,8 +167,8 @@ public extension Router {
return load(session, decoder: decoder, expectedResultType: expectedResultType, completion: completion)
}

func load<T: Codable>(_ session: RequestKitURLSession = URLSession.shared, decoder: JSONDecoder = JSONDecoder(), expectedResultType _: T.Type,
completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol?
func load<T: Decodable>(_ 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
Expand Down Expand Up @@ -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<T: Codable>(_ session: RequestKitURLSession = URLSession.shared, decoder: JSONDecoder = JSONDecoder(), expectedResultType _: T.Type) async throws -> T {
func load<T: Decodable>(_ 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)
}
Expand All @@ -251,7 +252,7 @@ public extension Router {
return try decoder.decode(T.self, from: responseTuple.0)
}

func load<T: Codable>(_ session: RequestKitURLSession = URLSession.shared, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy?, expectedResultType: T.Type) async throws -> T {
func load<T: Decodable>(_ session: RequestKitURLSession = URLSession.shared, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy?, expectedResultType: T.Type) async throws -> T {
let decoder = JSONDecoder()
if let dateDecodingStrategy = dateDecodingStrategy {
decoder.dateDecodingStrategy = dateDecodingStrategy
Expand Down

0 comments on commit e4d905f

Please sign in to comment.