Skip to content

Latest commit

 

History

History
164 lines (129 loc) · 3.2 KB

FormsNetworking.md

File metadata and controls

164 lines (129 loc) · 3.2 KB

FormsNetworking

FormsNetworking is a HTTP micro service

Import

import FormsNetworking

Dependencies

FormsInjector.framework
FormsLogger.framework
FormsUtils.framework

Usage

Reachability

Check if has internet connection

NetworkReachability.isConnected

Request

Class should inheritance NetworkMethod class

class DemoNetworkMethod: NetworkMethod {
    var url: URL! = "https://postman-echo.com/get?foo1=bar1&foo2=bar2".url
}

DemoNetworkMethod()
    .call(
        request,
        onSuccess: { (_) in },
        onError: { (_) in },
        onCompletion: { (_, _) in })

Custom body and headers

class DemoNetworkMethod: NetworkMethod {
    class Content: NetworkMethodContent {
        var token: String?

        var parameters: [String: Any]? {
            var parameters: [String: Any?] = [:]
            parameters["token"] = token
            return parameters
        }
        var headers: [String: String?]? {
            var headers: [String: String] = [:]
            headers["language"] = "en"
            return headers
        }
    }

    var content: NetworkMethodContent?
    var url: URL! = "https://postman-echo.com".url
    var method: HTTPMethod = .POST

    init(_ content: Content) {
        self.content = content
    }
}

Custom request interceptor

class DemoNetworkMethod: NetworkMethod {
    var url: URL! = "https://postman-echo.com".url
    var requestInterceptor: NetworkRequestInterceptor? = AppNetworkRequestInterceptor()
} 
class AppNetworkRequestInterceptor: NetworkRequestInterceptor {
    override func setHeaders(_ request: Request) {
        let headers = request.headers
        /* set additional headers */
        request.request.allHTTPHeaderFields = headers
    }
}

Custom response interceptor

class DemoNetworkMethod: NetworkMethod {
    var url: URL! = "https://postman-echo.com".url
    var responseInterceptor: NetworkResponseInterceptor? = AppNetworkResponseInterceptor()
} 
class AppNetworkResponseInterceptor: NetworkResponseInterceptor {
    override func postProcess(_ response: URLResponse?) {
    }
}

Response parser

class DemoNetworkMethod: NetworkMethod {
    var url: URL! = "https://postman-echo.com".url
    var parser: NetworkResponseParser? = AppNetworkResponseParser()
} 
class AppNetworkResponseParser: NetworkResponseParser { 
    override func parseError(data: Data) -> NetworkError? {
        // parse error from success response
        return nil
    }
}

Cache and logger

DemoNetworkMethod()
    .with(logger: VoidLogger())
    .with(cache: NetworkTmpCache(ttl: 60 * 60))
    .call(
        request,
        onSuccess: { (_) in },
        onError: { (_) in },
        onCompletion: { (_, _) in })

Cancellation

let request = DemoNetworkMethod()
    .call(
        request,
        onSuccess: { (_) in },
        onError: { (_) in },
        onCompletion: { (_, _) in })
task.cancel()

SSL Pinning

NetworkPinning.isEnabled = true
NetworkPinning.certificates = [
    Bundle.main.url(forResource: "certificate", withExtension: "cer")
].compactMap { $0 }