Skip to main content

A simple Python library for creating a Google BigQuery client using either an access token or a service account JSON file.

Project description

5X Google BigQuery Client Authentication Library

This Python library provides a simple way to create a Google BigQuery client using either an access token or a service account JSON file. It handles authentication seamlessly, allowing developers to focus on querying BigQuery.

  • Preferred Authentication: Access Token (FIVEX_BIGQUERY_ACCESS_TOKEN)
  • Fallback Authentication: Service Account JSON (FIVEX_BIGQUERY_SERVICE_ACCOUNT_KEY)

Installation

Install the package from PyPI using:

pip install 5x-google-bigquery-auth-manager

Environment Variables

This function relies on environment variables for authentication.

Variable Description Example
FIVEX_BIGQUERY_ACCESS_TOKEN Access token for authentication ya29.a0AfH6SMA...
FIVEX_BIGQUERY_DEFAULT_PROJECT_ID Your BigQuery project ID your-project-id
FIVEX_BIGQUERY_SERVICE_ACCOUNT_KEY Path to the service account JSON key file /path/to/service_account.json

Example: Setting Environment Variables

export FIVEX_BIGQUERY_ACCESS_TOKEN="your-access-token"
export FIVEX_BIGQUERY_DEFAULT_PROJECT_ID="your-project-id"
export FIVEX_BIGQUERY_SERVICE_ACCOUNT_KEY="/path/to/service_account.json"

Usage

Import and Use in Python

from bigquery_auth import create_bigquery_client

try:
    client = create_bigquery_client()
    print("✅ BigQuery Client Created Successfully!")
except Exception as e:
    print(f"❌ Error: {e}")

Function Overview

create_bigquery_client()

Creates a BigQuery client using either:

  1. Access Token (Preferred)
  2. Service Account JSON File (Fallback)

How It Works

First, tries to authenticate using the access token (FIVEX_BIGQUERY_ACCESS_TOKEN).
If the access token is expired or invalid, it raises an exception.
If no access token is available, it attempts authentication via service account JSON.
Implements logging and error handling for seamless debugging.


Error Handling

This function will raise exceptions in case of failure:

Exception Cause
ValueError No authentication method found
RefreshError Access token is expired or invalid
GoogleAPICallError BigQuery API request failure
BadRequest Insufficient permissions to access BigQuery

Example Handling in Python

try:
    client = create_bigquery_client()
except RefreshError as e:
    print(f"❌ Token Error: {e}")
except ValueError as e:
    print(f"❌ Configuration Error: {e}")
except GoogleAPICallError as e:
    print(f"❌ BigQuery API Error: {e}")
except BadRequest as e:
    print(f"❌ Permission Error: {e}")

Logging

This function uses Python’s built-in logging module to track:

  • Which authentication method is used (Access Token or Service Account)
  • Errors and exceptions for easier debugging

Why Use This Library?

Simplifies Google BigQuery authentication – No need to manage authentication manually.
Supports both Access Tokens & Service Account authentication – Provides flexibility.
Ensures proper error handling – Avoids silent failures with meaningful error messages.
Easy to integrate into any project – Just install and use.


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

5x_google_bigquery_auth_manager-0.1.2.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file 5x_google_bigquery_auth_manager-0.1.2.tar.gz.

File metadata

File hashes

Hashes for 5x_google_bigquery_auth_manager-0.1.2.tar.gz
Algorithm Hash digest
SHA256 7f821cd3e706dc4f62fa015715bdca1103fc2b941fd1bbedc276aa733ec666a5
MD5 e5edf19ccf4c884233b0abc8c0a5ae3f
BLAKE2b-256 ab3e2505c75c1dad8e25768b188e9e901c34f1ea1e1a4e3ab30fe6a6363095c7

See more details on using hashes here.

File details

Details for the file 5x_google_bigquery_auth_manager-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for 5x_google_bigquery_auth_manager-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8944d0915d5586c474d96c14560ecbf05e705e484441806befa6d31fbaf2002d
MD5 e26a4dfdc2eb74ccc04e878956146f8d
BLAKE2b-256 bd39c64bff81a2b974160db1652e4fba695125d826169c86ce2051d008fafa31

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page