No dendencies, just a plain URLSession
Swift 5+
- as carthage dependency: add in cartfile
github "SergeMaslyakov/Network-iOS"
- as submodule in the project: to add in your workspace as a sub-project
How to:
let configuration = NetworkLayer.Configuration(timeout: 10,
baseURL: "https://github.com",
sessionDelegate: sessionDelegateProvider,
authProvider: DefaultAuthorizationProvider(),
responseDecoder: JSONResponseDecoder(),
requestEncoder: JSONResponseEncoder(),
defaultBehaviors: [DebugBehavior(logger: logger)])
let networkClient = NetworkClientImpl(configuration: configuration,
sessionConfiguration: config.sessionConfiguration)
struct SignUpEndpoint: EndpointDescriptor {
private let data: SignUpDataModel
private let encoder = WWWFormRequestEncoder()
init(data: SignUpDataModel) {
self.data = data
}
// MARK: - EndpointDescriptor
let path: String = "auth/sign_up"
let method: HTTPMethod = .post
var headers: HTTPHeaders {
return ["Content-Type": "application/x-www-form-urlencoded"]
}
var customEncoder: NetworkRequestEncoding? {
return encoder
}
var params: [String: Any]? {
return data.toJSON()
}
var authRequired: Bool {
return false
}
}
let endpoint = SignUpEndpoint(data: data)
var task: URLSessionDataTask?
let completion: ((Result<T, NetworkError>) -> Void) = { result in
switch result {
case .success(let model):
// do smth with model
print(model)
case .failure(let error):
// do smth with error
print(error)
}
}
do {
task = try networkClient.sendRequest(endpoint: endpoint, completion: completion)
} catch {
// catch validation, coding errors
print(error)
}