NetworkingKit 0.3.1

NetworkingKit 0.3.1

Bruno Miguens维护。



  • By
  • Bruno Miguens

NetworkingKit

CI Status codecov CocoaPods compatible Carthage compatible

这个库是为了创建一个简单且易于与 iOS App Extensions 接入的网络层而创建的。尽管有很多网络管理器,但这个类可能是一个创建新库和自定义库的机会。

需求

  • iOS 8.0+
  • Xcode 9.0+
  • Swift 4.0+

安装

CocoaPods

CocoaPods 是 Cocoa 项目的依赖管理工具。您可以按照此指南进行安装。

  use_frameworks!

  # Add the instruction below inside your targets on your Podfile.
  pod 'NetworkingKit'

请保存文件后,运行 pod install 命令以将框架下载并集成到您的项目中。

Carthage

Carthage 是一个去中心化的依赖管理器,构建您的依赖并提供您二进制框架。您可以通过遵循此指南来安装它。

  # Add the instruction below inside your Cartfile.
  github "BrunoMiguens/NetworkingKit"

请保存文件后,运行命令 carthage update 来下载和构建框架,完成后,将 NetworkingKit.framework 拷贝到您项目中进行。

使用方法

此框架在某程度上受到了 Moya(与 Alamofire 结合使用)的启发,请查看这些项目,因为它们稳定,拥有庞大的社区支持,并理应得到荣誉。

我们将通过创建一个 classenum(枚举类型通常更适合这种用途,因为它们更加灵活)来生成我们 API 的路由。

enum ApiService {

  case login(email: String, password: String)
  case fetchArticles(query: String, page: Int?)

  // Continue by adding as many cases as you want.

}

为了使您的 ApiService 能够被 NetworkingKit 使用,它必须满足协议 NetworkingTarget

extension ApiService: NetworkingTarget {

  // The base URL of your API goes here.
  var baseUrl: String {
    return "https://yourapiurl.com"
  }

  // Use this property to add specific endpoints depending on the service that you want (you can omit the parameters you don't need them)
  var endpoint: String {
    switch value:

      case login: return "/login"
      case fetchArticles: return "/fetch"

    }
  }

  // The HTTP method for each case (get, post, put or delete).
  var method: NetworkingMethod {
    switch value:

      case login: return .post
      case fetchArticles: return .get

    }
  }

  // Use the parameters of each case to construct the dictionary for your HTTP request (NKStringDictionary is a type alias for [String: Any]).
  var parameters: NKStringDictionary {
    switch value:

      case login(let email, let password):
        return ["email": email, "password": password]

      case fetchArticles(let query, let page):
        if let newPage = page {
          return ["query": query, "page": newPage]
        }
        return ["query": query]

    }
  }

  // Sometimes your API may need to encode some parameter, especially if is a get request, for instance, "search[query]=something" in this case the brackets need to be encoded.
  var encodeParameters: Bool {
      return true
  }

  // By requesting something using `NetworkingKit` and giving the parameter `runningTests: true`, the library will return and parse the `sampleData` property, if exists, that way you avoid using the internet and a real API for your Unit/UI tests.
  var sampleData: Any? {
    switch value:

      case login: return ["test": 123]

      // By returning nil the library will ignore your choice and make the HTTP request.
      case fetchArticles: return nil

    }
  }

}

现在,您只需使用 Networking 语句来执行调用。

  let login = ApiService.login(email: "[email protected]", password: "12345")

  Networking.perform(for: login) { result in

    // Check the `NKResult` to understand the type of property that `result` represents.

    if let value = result.value, result.isSuccess {
      // Your code
    }

  }

使用 Networking.perform(for: login, runningTests: true) 语句进行调用时,告诉网络执行器去获取 sampleData 属性(如果它不为 nil)。

高级选项

您可能想要在请求中添加自定义头信息,为此您可以使用 NetworkingHeaderDictionary(也称为 [NetworkingHeader: String])作为参数。

  let custom: NetworkingHeaderDictionary = [.custom(key: "your-header-key"): "abc123"]

  Networking.perform(for: login, headers: custom) { result in
      // Your code
  }

作者

请随时贡献,提出反馈或分享任何您觉得需要分享的内容。

GitHub | Twitter

许可证

NetworkingKit 遵循 MIT 许可协议。详细信息请查看 LICENSE 文件。