To run the example project, clone the repo, and run pod install
from the Example directory first.
SwiftlyNetworker is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'SwiftlyNetworker'
enum SojuAPI {
case list
}
extension SojuAPI: APIable {
var params: [String : Any]? {
nil
}
var path: String {
"/TestJSONfile/main/JSON/Test.json"
}
var method: HttpMethod {
.get
}
var log: Bool { return false }
}
import SwiftlyNetworker
// ViewModel
class ViewModel {
init(
networker: NetworkerLogic = SwiftlyNetworker(componet: NetworkerLogicDependency)
) {
self.networker = networker
}
func request() -> AnyPublisher<Model, Error> {
return networker.request(SojuAPI.list).eraseToAnyPublisher()
}
func request(complete: @escaping ((Result<Model, Error>) -> Void)) {
networker.request(SojuAPI.list, complete: complete)
}
}
// UIViewController
class ViewController: UIViewController {
private let viewModel = ViewModel()
override func viewDidLoad() {
super.viewDidLoad()
// MARK: - Combine
viewModel.request()
.sink { completion in
switch completion {
case .finished:
print("finish")
case .failure(let error):
print("error: \(error.localizedDescription)")
}
} receiveValue: { (model: Model) in
print("model \(model)")
}.store(in: &cancellables)
// MARK: - Closure
viewModel.request { (result: Result<Model, Error>) in
switch result {
case .success(let model):
print("model \(model)")
case .failure(let error):
print("error: \(error.localizedDescription)")
}
}
}
}
linsaeng, [email protected]
SwiftlyNetworker is available under the MIT license. See the LICENSE file for more info.