I'm trying to do some an experiment to make a simple URLSession & URLSessionTask abstraction that intended to make a HTTP call so easy than before. Inspired by Moya
Just define the your request type that conforms to RequestProtocol
enum ProfileRequest {
case profileByID(id: Int)
}
extension ProfileRequest: RequestProtocol {
var baseURL: URL {
return URL(string: "http://yourapi.com")
}
var path: String {
switch self {
case .profileByID:
return "/profile"
}
}
var parameters: Parameters {
switch self {
case .profileByID(let id):
return ["id" : id]
}
}
var headers: [String : String] {
return [:]
}
var parameterEncoder: ParameterEncoder {
switch self {
case .profileByID:
return URLParameterEncoder.defaultInstance
}
}
var method: RequestMethod {
switch self {
case .profileByID:
return .get
}
}
}
Then make the provider & do some request!
let provider = NetworkProvider<ProfileRequest>()
provider.request(.profileByID(id: 1)) { result in
// handle your result
}
Currently manual yet, just add this project to your workspace & don't forget to link the NetworkKit.framework
at your app build phase.
Feedbacks is appreciated, so we can learn together :D