Skip to main content
Version: 1.4.2

Example

APIBuilder

With APIBuilder you can configure your requests. For Example.

enum HomeAPI {
case home
case categories(ExampleStruct)
}

extension HomeAPI: APIBuilder {

var path: URLPath {
switch self {
case .home:
return .plain("jokes/random")
case .categories:
return .plain("jokes/categories")
}
}

var httpMethod: HTTPMethods {
switch self {
case .home:
return .get
case .categories:
return .post
}
}

var headers: HTTPHeader {
.bearer("Bearer yourToken")
}

var task: HTTPTask {
switch self {
case .home:
return .request
case let .categories(model):
return .requestEncoder(model)
}
}
}

Configuration Variables.

URLPath

With URLPath you can configure two types of URLs.

.plain("jokes/random") // jokes/random
.composed("joke", pathId: "ID") // /joke/10

HTTPMethods

With HTTPMethods you can choose five types of methods.

.get
.post
.put
.patch
.delete

HTTPHeader

With HTTPHeader you can choose five types of headers.

.custom([
.header("key", "value"),
.bearer("YourToken")
]) // Array of HTTPHeader

// Only one option
.bearer("yourToken")
.basic("yourBase64")
.header("key", "value")

HTTPTask

With HTTPTask you can choose five types of tasks.

.request // Plain
.requestBody([
"name": "Victor",
"lastname": "Freitas"
]) // Body
.requestEncoder(model) // Body
.requestURLParameters(urlParameters: ["transactionId": "ID"]) // Query String
.requestParameters(bodyParameters: ["name": "Victor"], urlParameters: ["transactionId": "ID"]) // Body with Query String.

Request Service

With RequestService you can execute your requests. For Example.

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)
}
}