Last active
May 6, 2022 19:30
-
-
Save ankittlp/7fa8ba187933b448321e4fcf428a1603 to your computer and use it in GitHub Desktop.
Networking LayerProtocols for CallBack type
This file contains 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
// An AssociatedType Protocol: Request type to be implemented by Concrete Requests | |
protocol Request { | |
associatedtype Response: Decodable // 1. A Response Type constrained to be decodable | |
associatedtype ResponseParser: ResponseParserType where ResponseParser.Response == Response // 2 Response parser contrained to be confirming to ResponseParserType | |
// 3 Basic properties required by a request | |
var baseUrl: String { get } | |
var path: String { get } | |
var method: String { get } | |
var header: [String: String]? { get } | |
var parser: ResponseParser? { get } // 3 | |
var errorParser: ErrorParserType? { get } // 4 Error Parsing type confirming to ErrorParserType | |
func parameter() -> [String: Any]? | |
} | |
// Convert the Request type to URLRequests | |
protocol RequestConvertable: Request { | |
func asURLRequest() throws -> URLRequest | |
} | |
// A generic type protocol that defines the abstraction for Parser. | |
protocol ResponseParserType { | |
associatedtype Response | |
func parse(data: Data) throws -> Response? | |
} | |
public typealias JSONDictionary = [String : Any] | |
// A protocol defining the abstraction for Error parser. | |
protocol ErrorParserType { | |
func parse(data: JSONDictionary) -> Error? | |
} | |
protocol RequestExecutor { | |
func executeRequest<R: RequestConvertable>(_ request: R, completion: @escaping (Result<R.Response, Error>) -> Void) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment