CryptoCompareAPI
Swifty cryptocompare api 客户端
☑ TODO
- 价格 API
- 其他信息 API
- 历史数据 API
- 排行榜 API
- 流式 API
- 新闻 API
✏️ Pod install
将 pod 'CryptoCompareAPI'
添加到您的 Podfile 中。运行 pod install
💡 示例
let api = CryptoCompareAPI(applicationName: "MyApp")
// Retrieve all coins (actually this is not the whole coin list
// from cryptocompare API, but this endpoint is useful anyway
api.send(GetCoinListRequest()) {
switch $0 {
case .success(let coinList):
// Process coinList here
case .failure(let error):
// Handle error state
print(error.description)
}
}
let request = GetSymbolsPriceRequest(fsyms: "BTC,ETH",
tsyms: "USD",
e: .binance)
api.send(request) {
switch $0 {
case .success(let prices):
// Process prices here
case .failure(let error):
// Handle error state
print(error.description)
}
}
有时候,简单的 'result' 封装不足以处理复杂的异步代码,所以您可以编写一个扩展,并使用承诺的力量
import Promises
import CryptoCompareAPI
extension CryptoCompareAPI {
func send<T: APIRequest>(_ request: T) -> Promise<T.Response> {
return Promise { fulfill, reject in
self.send(request) {
switch $0 {
case .success(let value):
fulfill(value)
case .failure(let error):
reject(error)
}
}
}
}
}
有时候,控制 URLSessionTask 非常有用,例如取消长时间请求或恢复它们。在这种情况下,send 方法返回一个数据任务对象。
let dataTask = api.send(request)
...
if userWantsToCancelUpdate {
dataTask.cancel()
}
📝 许可
版权 2018 Eugene Velizhenkov
在 MIT 许可证下授权;除非在法律要求或在书面协议中同意,否则不得使用此文件。您可以从以下位置获取许可证的副本:
https://open-source.org.cn/licenses/MIT 在法律要求或书面协议要求下,分发在本许可证下的软件基于“现状”基础上进行,不提供任何明示或暗示的保证或条件。请参阅许可证了解对许可的权限和限制。