Latest 0.2.0
Homepage https://github.com/longkai/GoogleBatchHelper
License MIT
Platforms ios 8.0, osx 10.9
Authors

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

CI Status
Version
License
Platform

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: "https://www.googleapis.com/batch")!)
    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.

Latest podspec

{
    "name": "GoogleBatchHelper",
    "version": "0.2.0",
    "summary": "A Google Batch Decode/Encode Swift 3 implementation",
    "description": "A Google Batch API Decode/Encode Swift 3 implementation.",
    "homepage": "https://github.com/longkai/GoogleBatchHelper",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "longkai": "[email protected]"
    },
    "source": {
        "git": "https://github.com/longkai/GoogleBatchHelper.git",
        "tag": "0.2.0"
    },
    "platforms": {
        "ios": "8.0",
        "osx": "10.9"
    },
    "source_files": "GoogleBatchHelper/Classes/**/*",
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This