5x-bigquery-auth 0.1.2
A Python library for authenticating with Google BigQuery.
License: MIT
Requires Python: >=3.8

# BigQuery Client Authentication Library

This Python library provides a utility function to create a **BigQuery client** using **either an access token** or a **service account JSON file**.

- **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-bigquery-auth

---

## **Environment Variables**

This function relies on **environment variables** to retrieve authentication details.

| 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**

```bash
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**

```python
from bigquery_auth import create_bigquery_client

try:
    client = create_bigquery_client()
    print("\U00002705 BigQuery Client Created Successfully!")
except Exception as e:
    print(f"\U0000274C Error: {e}")
```

---

## **Function Details**

### **`create_bigquery_client()`**

Creates a **BigQuery client** using either:

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

### **Function Logic**

✔ **Tries to use the access token (`FIVEX_BIGQUERY_ACCESS_TOKEN`)**
✔ **If access token is expired or invalid, it raises an exception**
✔ **If no access token is available, it tries to load service account JSON**
✔ **Proper logging and error handling included**

---

## **Error Handling**

The function will **raise exceptions** in case of failure:

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

### **Example Handling in Python**

```python
try:
    client = create_bigquery_client()
except RefreshError as e:
    print(f"\U0000274C Token Error: {e}")
except ValueError as e:
    print(f"\U0000274C Configuration Error: {e}")
except GoogleAPICallError as e:
    print(f"\U0000274C BigQuery API Error: {e}")
except BadRequest as e:
    print(f"\U0000274C 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** if authentication fails

---

---

### **🔹 Why Is This Better?**
✅ **Avoids Redundant Installation Steps** – Dependencies are already in `install_requires` in `setup.py`.
✅ **Keeps the README Cleaner** – Users only need to install the package itself.
✅ **Ensures Correct Dependency Versions** – PyPI will resolve and install the correct versions automatically.

Thanks for catching that! I'll update the **`README.md`** file accordingly. 🚀

````