GenericAPIClient
用于异步Web请求和解析通用JSON数据的通用客户端基类
安装
安装有两种选择:您要么克隆/分支此仓库并使用源代码,要么使用cocoapods。
Cocoapods
如果您想使用Cocoapods将其包含到您的xcode项目中,必须执行以下操作
- 在终端中运行以下命令安装cocoapods到您的计算机上
sudo gem install cocoapods
您可以在以下位置找到更多帮助:[https://guides.cocoapods.org.cn/using/getting-started.html#toc_3](https://guides.cocoapods.org.cn/using/getting-started.html#toc_3)
- 使用
pod init
命令创建Podfile。有关Podfile的更多信息,请在这里查看:[https://guides.cocoapods.org.cn/using/the-podfile.html](https://guides.cocoapods.org.cn/using/the-podfile.html) - 将Podfile指向GenericAPIClient,如下所示
target 'MyApp' do
use_frameworks!
pod 'GenericAPIClient'
end
- 有了相应的Podfile后,使用
pod install
命令将GenericAPIClient依赖项安装到您的xcode项目中。然后进入项目并构建它。
此过程的相关说明请见以下链接:[https://guides.cocoapods.org.cn/using/using-cocoapods](https://guides.cocoapods.org.cn/using/using-cocoapods)
使用
这里有例子描述了为您的客户端创建API端点时所有选项。一旦您阅读并理解了这一点,过程就很简单
只需创建一个APIClient对象,其API基础URL如下
let exampleClient: APIClient = APIClient("https://api.example.com/version")
为其创建端点,您可以这样调用端点
_ = exampleClient.send(request: ExampleEndpoint(exampleParameter: 5, exampleExtraPathComponent: "json")) { response in
switch response {
case .success(let data):
// Handle your data here
case .failure(let error):
//Handle your error here
}
}