Swiftpack.co - oliveroneill/BigQuerySwift as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by oliveroneill.
oliveroneill/BigQuerySwift 0.0.1
BigQuery client for Swift
⭐️ 11
🕓 4 years ago
.package(url: "https://github.com/oliveroneill/BigQuerySwift.git", from: "0.0.1")

BigQuerySwift

Build Status Platform Swift Package Manager macOS Linux

A BigQuery client for Swift.

Installation

Add this to your Package.swift:

.package(url: "https://github.com/oliveroneill/BigQuerySwift.git", .upToNextMajor(from: "0.0.1")),

Usage

To get an authentication from your service account:

let provider = BigQueryAuthProvider()
try! provider.getAuthenticationToken { response in
    switch response {
    case .token(let token):
        // Your token to be passed into BigQueryClient
        print(token)
    case .error(_):
        fatalError("Something went wrong.")
    }
}

You'll need to copy your credentials file at $GOOGLE_APPLICATION_CREDENTIALS to credentials.json in the directory that the binary is run in.

To insert:

let client = BigQueryClient<YourEncodableType>(
    authenticationToken: "<ENTER-AUTHENTICATION-TOKEN>",
    projectID: "<ENTER-PROJECT-ID>",
    datasetID: "<ENTER-DATASET-ID>",
    tableName: "<ENTER-TABLE-NAME>"
)
try client.insert(rows: rows) { response in
    switch response {
    case .error(let error):
        fatalError("Error: " + error.localizedDescription)
    case .bigQueryResponse(let response):
        checkErrors(response.insertErrors)
    }
}

To query:

let query = "SELECT * FROM users"
try db.query(query) { (r: QueryCallResponse<YourDecodableType>) in
    switch r {
    case .error(let e):
        fatalError("Something went wrong.")
    case .queryResponse(let result):
        guard let result = result.rows else {
            guard let errors = result.errors else {
                fatalError("No errors and no rows.")
            }
            print("BigQuery errors: \(errors)")
            return
        }
        printQueryResult(result)
    }
}

Examples

For a working example, see here.

Contributing

Feel free to help out. Currently only insertAll is supported.

GitHub

link
Stars: 11
Last commit: 4 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics