HTTPLayer
Swift语言中为JSON服务提供直接的HTTP网络
功能
- 易于使用和阅读的请求/响应方法
- JSON响应
- 只需设置一次主机、上下文和头信息,并通过您定义的键使用
- 带查询和路径参数的GET方法
- 带JSON主体的POST方法
- 带JSON主体以及查询和路径参数的PUT方法
- 带JSON主体以及查询和路径参数的DELETE方法
- 单元和集成测试覆盖率 - (需要更多测试覆盖率)
- HTTP响应验证
- 自定义错误响应对象
- TLS证书和公钥固定(使用证书文件和/或Base64编码的抗散列键的SHA256编码)
- OPTION和PATCH方法
要求
- iOS 10.0+
- Xcode 10.2+
- Swift 5+
安装
Cocoapods
Cocoapods 是一个项目管理器。有关其用法和安装的文档,请访问此 网站。
在您的 Podfile 中添加
pod 'HTTPLayer'
在您的终端中,导航到您的项目文件夹,并运行此命令
$ pod install
用法
配置
要使用 HTTPLayer,只需将其导入到您可能想要的任何类中。
import HTTPLayer
建议在 AppDelegate 文件中配置所有主机、上下文、头部和安全选项,例如下面的示例
- 对于主机、上下文和头部配置,在 " func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {} " 中添加配置
//For adding Hosts and Context or Headers
HTTP.Config.add
//For setting multiplus Hosts and Context or Headers at once
HTTP.Config.set
//For retrieving Hosts and Context or Headers that has already been added
HTTP.Config.get
//For changing already added Headers
HTTP.Config.change
//For removing any Header that has already been added
HTTP.Config.remove
- 对于安全选项,也同样在 " func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {} " 中添加配置
//For enabling SSL Pinning, Debug Mode, Continue Without Pinning and setting SHA256 encoded with Base64 Hash Keys and/or Certificate files .cer and .crt to expecific Hosts
HTTP.Security.set
//For retrieving the any configuration made with the HTTP.Security.set
HTTP.Security.get
这些是已经实现的方法,您可以选择。所有这些方法都可以通过 "HTTP.Request" 访问,并且都已在 Xcode 自动完成快捷方式中进行了文档说明。(E 和 D 分别代表 E 为 Encodable 的泛型对象,D 为 Decodable 的泛型对象)
- GET - (使用 "receivingAsError: T.self" 参数添加,以使用自定义的错误响应对象)
//Without Parameters:
HTTP.Request.get(from: String, withHostAndContext: String, andHeaders: String, receivingObjectType: D.self, completion: (Result<ResponseObject<D>, ErrorObject<ER>>) -> ())
//Path Parameters:
HTTP.Request.get(from: String, usingPathParameters: [String]?, fromHostAndContext: String, andHeaders: String?, receivingObjectType: D.self, completion: (Result<ResponseObject<D>, ErrorObject<ER>>) -> ())
//Query Parameters:
HTTP.Request.get(from: String, usingQueryParameters: [String : String]?, fromHostAndContext: String, andHeaders: String?, receivingObjectType: D.self, completion: (Result<ResponseObject<D>, ErrorObject<ER>>) -> ())
- POST - (使用 "receivingAsError: T.self" 参数添加,以使用自定义的错误响应对象)
HTTP.Request.post(to: String, withBody: Encodable, fromHostAndContext: String, andHeaders: String?, receivingObjectType: D.self, completion: (Result<ResponseObject<D>, ErrorObject<ER>>) -> ())
- PUT - (使用 "receivingAsError: T.self" 参数添加,以使用自定义的错误响应对象)
//Path Parameters:
HTTP.Request.put(on: String, withBody: E, andPathParameters: [String]?, fromHostAndContext: String, andHeaders: String?, receivingObjectType: D.self, completion: (Result<ResponseObject<D>, ErrorObject<ER>>) -> ())
//Query Parameters:
HTTP.Request.put(on: String, withBody: E, andQueryParameters: [String : String]?, fromHostAndContext: String, andHeaders: String?, receivingObjectType: D.self, completion: (Result<ResponseObject<D>, ErrorObject<ER>>) -> ())
- DELETE - (使用 "receivingAsError: T.self" 参数添加,以使用自定义的错误响应对象)
//Path Parameters
HTTP.Request.delete(from: String, withPathParameters: [String]?, fromHostAndContext: String, andHeaders: String?, receivingObjectType: D.self, completion: (Result<ResponseObject<D>, ErrorObject<ER>>) -> ())
//Query Parameters
HTTP.Request.delete(from: String, withQueryParameters: [String : String]?, fromHostAndContext: String, andHeaders: String?, receivingObjectType: D.self, completion: (Result<ResponseObject<D>, ErrorObject<ER>>) -> ())
所有请求方法返回一个 Result<ResponseObject, ErrorObject>。推荐使用返回结果 Result<>的方式是
switch response {
case .success(let success):
//To retrieve the parsed Response
success.response
//To retrieve the response Headers
success.headers
case .failure(let failure):
//To retrieve the custom Error Response
failure.response
//To retrieve the error Object
failure.error
}
致谢
https://github.com/arcadekenan
披露
如果您认为您发现了任何问题或漏洞,请随时联系 [email protected] 或者提交拉动请求。
许可证
HTTPLayer 在MIT许可证下发布。