Skip to content

Instantly share code, notes, and snippets.

@ankittlp
Created May 7, 2022 12:52
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/5c5c8d7ac8ea3b7f37e01cb9c8800df7 to your computer and use it in GitHub Desktop.
Save ankittlp/5c5c8d7ac8ea3b7f37e01cb9c8800df7 to your computer and use it in GitHub Desktop.
NetworklayerProtocolsCombine
// 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?
}
// RequestExecutor for Combine
protocol RequestExecutor {
func executeRequest<R>(request: R) -> AnyPublisher<R.Response, Error> where R: RequestConvertable
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment