Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ankittlp/7fa8ba187933b448321e4fcf428a1603 to your computer and use it in GitHub Desktop.
Save ankittlp/7fa8ba187933b448321e4fcf428a1603 to your computer and use it in GitHub Desktop.
Networking LayerProtocols for CallBack type
// 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