YTKNetwork








Introduction

YTKNetwork is a high-level request utility based on AFNetworking. It aims to simplify network request handling for business development. It provides a more intuitive way to define network request interfaces and handle request callbacks.

Features

  • Clear request interfaces definition.
  • Support for easy chaining of request operations with dependencies.
  • Custom retry mechanism for failed network requests.
  • SSL pinning for enhanced security.
  • Block-based response and error handling.
  • Automatic URL encoding for parameters.
  • Batch request handling with success/failure callbacks.

Installation

You can use CocoaPods to install YTKNetwork by adding it to your Podfile:


pod 'YTKNetwork', '~> 2.0.4'

Then, run the following command:


$ pod install

To use YTKNetwork, import the framework:

import YTKNetwork

Usage

Creating a Request

To create a request, subclass the YTKBaseRequest class and override necessary methods:

class MyRequest: YTKBaseRequest {
  override func requestUrl() -> String {
    return "https://api.example.com/v1/example"
  }

  override func requestMethod() -> YTKRequestMethod {
    return .GET
  }
}

Sending a Request

To send a request, create an instance of your custom request class and use a YTKRequestAgent method:

let request = MyRequest()
request.startWithCompletionBlock { (request) in
  if request.isSuccess {
    // Handle successful response
  } else {
    // Handle error
  }
}

Request Callbacks

The completion block provided in startWithCompletionBlock is called upon request completion. You can handle the response and error by checking request.isSuccess in the completion block:

if request.isSuccess {
  // Handle successful response
} else {
  // Handle error
}

Chaining Requests

YTKNetwork supports easy chaining of request operations with dependencies. To chain requests, override the requestCompleteFilter method in your custom request class:

class MyChainRequest: YTKChainRequest {
  override func requestCompleteFilter() -> String {
    let responseDict = self.responseJSONObject as? [String: Any]
    
    // Check responseDict for necessary data to determine the next request in the chain
    
    return ""
  }
}

SSL Pinning

YTKNetwork supports SSL pinning, which enhances security by ensuring the server certificate matches a pre-defined public key. To enable SSL pinning, override the securityPolicy method in your custom request class:

class MyRequest: YTKBaseRequest {
  override func securityPolicy() -> YTKNetworkSecurityPolicy {
    let policy = YTKNetworkSecurityPolicy(pinningMode: .certificate)
    policy.allowInvalidCertificates = false
    policy.validatesDomainName = true
    policy.pinnedCertificates = YTKNetworkSecurityPolicy.certificatesInBundle()
    return policy
  }
}

Documentation

For detailed documentation and examples, please visit the official YTKNetwork GitHub repository.

License

YTKNetwork is released under the MIT License.