How to Use
Basically, you need two files to assemble your requests and put Base_URL, Protocol and Environment in your info.plist.
Info.plist
You have 3 types of Environments for you choose.
enum EnvironmentCase: String {
case production = "production"
case sandbox = "sandbox"
case mock = "mock"
}
In sandbox and mock you will see logs of the VFNetwork itself.
AppDelegate.swift
By default VFNetwork has some configuration but you can customize it in two ways.
Fast and Custom.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Fast way
VFNetwork.shared.configure([
.timeout(10.0)
])
// Custom Way
VFNetwork.shared.session {
let config = URLSessionConfiguration.default
config.urlCache = .shared
config.urlCredentialStorage = nil
config.httpCookieAcceptPolicy = .always
config.requestCachePolicy = .reloadRevalidatingCacheData
config.timeoutIntervalForRequest = .init(10.0)
if #available(iOS 11.0, *) {
config.waitsForConnectivity = false
}
return config
}
return true
}
and now you can create two files for use.
HomeAPI.swift
Here you will configure your requests.
import VFNetwork
enum HomeAPI {
case joke
case categories
}
extension HomeAPI: APIBuilder {
var path: URLPath {
switch self {
case .joke:
return .plain("jokes/random")
case .categories:
return .plain("jokes/categories")
}
}
var httpMethod: HTTPMethods {
switch self {
case .joke, .categories:
return .get
}
}
var headers: HTTPHeader {
.custom([
.bearer("yourToken"),
.basic("yourBase64"),
.header("custom", "header")
])
}
var task: HTTPTask {
switch self {
case .joke, .categories:
return .request
}
}
HomeService.swift
And here you will execute your requests.
import VFNetwork
class HomeService: RequestService<HomeAPI> {
func getJoke(completion: @escaping (Result<JokeModel, Error>) -> Void) {
execute(.joke, responseType: JokeModel.self, completion: completion)
}
func getCategories(completion: @escaping (Result<CategoryModel, Error>) -> Void) {
execute(.categories, responseType: CategoryModel.self, completion: completion)
}
}