GoogleBatchHelper 0.2.0

GoogleBatchHelper 0.2.0

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Feb 2017
SwiftSwift Version 3.0
SPMSupports SPM

Maintained by xiaolong.



  • By
  • longkai

GoogleBatchHelper

A simple Google batch API request/response encode/decode helper for both iOS and macOS written in Swift 3.

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

/// - 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
        }
    }
}

Requirements

  • iOS 8.0+
  • macOS 10.9+

Installation

GoogleBatchHelper is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "GoogleBatchHelper"

Author

longkai, [email protected]

License

GoogleBatchHelper is available under the MIT license. See the LICENSE file for more info.