测试测试 | ✓ |
语言语言 | SwiftSwift |
许可证 | MIT |
发布最新版本 | 2017年2月 |
SwiftSwift 版本 | 3.0 |
SPM支持 SPM | ✗ |
由 xiaolong 维护。
一个简单的 Google 批量 API 请求/响应编解码辅助器,适用于 iOS 和 macOS,用 Swift 3 编写。
要运行示例项目,先克隆仓库,然后从 Example 目录运行 pod install
/// - ids: support your requested entity id array
func encodeBatchRequest(_ ids: [String]) {
let boundary = "\(Date().timeIntervalSince1970)"
var req = URLRequest(url: URL(string: "")!)
req.addValue("multipart/mixed; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
req.httpMethod = "POST"
var reqs = [URLRequest]()
for idx in ids.indices {
let req = URLRequest(url: URL(string: "/gmail/v1/users/me/threads/\(ids[idx])?fields=id,historyId,messages(id,threadId,labelIds,internalDate,historyId,payload(headers))")!)
reqs.append(req)
}
let encoder = BatchRequestEncoder(reqs, contentIDs: [String](ids), boundary: boundary)
req.httpBody = encoder.body
// perform the requst...
}
/// this is exactly like you do in `func dataTask(with request: URLRequest, completionHandler: @escaping (Data?, URLResponse?, Error?)` in a array way.
func decodeBatchResponse(_ respBoundary: String, _ data: Data) {
guard let batch = BatchResponseDecoder(data: data, boundary: respBoundary) else {
fatalError("fail")
}
// a lot work to do
for idx in batch.resp.indices {
if let data = batch.data[idx],
let resp = batch.resp[idx],
resp.statusCode == 200 {
// do your job...
} else {
let str = String(data: batch.data[idx]!, encoding: String.Encoding.utf8)
// handle error
}
}
}
GoogleBatchHelper 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中:
pod "GoogleBatchHelper"
longkai, [email protected]
GoogleBatchHelper 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。