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
import Foundation | |
final class BaseService<T: Decodable>: Service { | |
typealias ResultTypeValue = T | |
var responseHandler: HTTPResponseHandler<T>? = HTTPResponseHandler<T>() | |
var request: HTTPRequestRepresentable? | |
var successHandler: SuccessHandlerBlock? | |
var failureHandler: FailureHandlerBlock? |
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
import Foundation | |
class HTTPResponseHandler<T: Decodable>: ResponseHandler { | |
typealias ResultValueType = T | |
private var isResponseRepresentSimpleType: Bool { | |
return | |
T.self == Int.self || | |
T.self == String.self || | |
T.self == Double.self || |
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
import Foundation | |
struct BaseErrorHandler: ErrorHandler { | |
var errorCodeHandler: ErrorCodeHandler = BaseErrorCodeHandler() | |
var errorCodeGetter: ErrorCodeGetter = BaseErrorCodeGetter() | |
var errorMessageGetter: ErrorMessageGetter = BaseErrorMessageGetter() | |
func handleErrorResponse(_ response: ResponseRepresentable, completion: (ErrorRepresentable) -> ()) { | |
guard let httpResponse = response.response as? HTTPURLResponse else { | |
return |
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
import Foundation | |
struct GitHubRepo: Decodable { | |
var id: Int | |
var name: String | |
var `private`: Bool | |
var description: String | |
var url: URL | |
var hasIssues: Bool | |
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
protocol APIBuilder { | |
associatedtype ResultType: Decodable | |
func buildAPI(for request: HTTPRequestRepresentable, | |
decodingProcessor: ModelDecodingProcessor<ResultType>?, | |
nestedModelGetter: NestedModelGetter?) -> BaseService<ResultType> | |
} |
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
import Foundation | |
class GitHubAPIBuilder<T: Decodable>: APIBuilder { | |
typealias ResultType = T | |
func buildAPI(for request: HTTPRequestRepresentable, | |
decodingProcessor: ModelDecodingProcessor<T>? = nil, | |
nestedModelGetter: NestedModelGetter? = nil) -> BaseService<T> { | |
let service = BaseService<T>() | |
service.request = request |
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
import Foundation | |
protocol ReposGettable { | |
func getRepos(completion: @escaping ([GitHubRepo]) -> ()) | |
} |
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
import Foundation | |
class ReposAPI: ReposGettable { | |
lazy var getReposService: BaseService<[GitHubRepo]> = { | |
let apiBuilder = GitHubAPIBuilder<[GitHubRepo]>() | |
return apiBuilder.buildAPI(for: GETReposRequest()) | |
}() | |
func getRepos(completion: @escaping ([GitHubRepo]) -> ()) { |
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
import UIKit | |
class ViewController: UIViewController { | |
let ghReposService: ReposGettable = ReposAPI() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
ghReposService.getRepos { (repos) in | |
print(repos) |
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
import Foundation | |
protocol GitHubUsersAPI { | |
func getLocationFor(_ user: String, completion: @escaping (String) -> ()) | |
} |