Registry / gcp / nsj-gcf-utils

nsj-gcf-utils

JSON →
library1.1.0pypypiunverified

nsj-gcf-utils (v1.1.0) is a Python library providing utilities for building Google Cloud Functions, primarily designed for applications leveraging Django and Django REST Framework. It offers tools for handling function context, interacting with Google Cloud services like Firestore, Storage, and Secret Manager, and managing tasks. The library is actively maintained with a focus on specific enterprise use cases rather than general-purpose GCF development, and updates are released as needed.

pip install nsj-gcf-utils
INSTALL
IMPORT
SIG · NSJ-GCF-UTILS
N
nsj-gcf-utils
gcppythonv1.1.0
Install
4.0s avg
Import
Disk
31MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.1.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 32.5MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 4.0s · import 0.000s · 32MB
31MB installed
● package 31MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

get_current_function_context
from nsj_gcf_utils import get_current_function_context
from nsj_gcf_utils import get_current_function_context

This quickstart demonstrates how to retrieve the current function context within a GCF handler. It uses `get_current_function_context` to access request-specific metadata, such as request ID, user ID, and entity ID, which are typically passed via HTTP headers in a Google Cloud Function environment. A mock request object is included to make the example runnable locally, simulating a GCF invocation. Note that Google Cloud service clients (like Firestore) require proper GCP authentication and project setup to function.

import json from nsj_gcf_utils.objects.function_context import get_current_function_context from nsj_gcf_utils.exception import GcfException # Simulate a Flask-like request object for local testing # In a real GCF environment, 'request' is provided by the framework. class MockRequest: def __init__(self, json_data, headers=None): self._json_data = json_data self.headers = headers if headers is not None else {} @property def json(self): return self._json_data def get_json(self): return self._json_data def get_data(self): return json.dumps(self._json_data).encode('utf-8') def my_gcf_handler(request): try: # Get the current function context # In GCF, 'request' would be the actual Flask request object. context = get_current_function_context(request) # Access context properties (these would be populated by GCF headers) print(f"Request ID: {context.request_id}") print(f"User ID: {context.user_id}") print(f"Entity ID: {context.entity_id}") return "OK", 200 except GcfException as e: print(f"Error: {e}") return str(e), 500 except Exception as e: print(f"Unexpected error: {e}") return str(e), 500 # Example usage with a mock request (simulating a GCF invocation) mock_headers = { 'X-Request-ID': 'mock-req-123', 'X-User-ID': 'mock-user-456', 'X-Entity-ID': 'mock-entity-789' } mock_json_payload = {'data': 'some_data'} mock_request = MockRequest(mock_json_payload, headers=mock_headers) response, status_code = my_gcf_handler(mock_request) print(f"\nHandler Response: {response}, Status: {status_code}") # Example of using Firestore client (requires actual GCP setup) try: # from nsj_gcf_utils.firestore import get_firestore_client # firestore_client = get_firestore_client() # print("Firestore client initialized successfully.") print("\nUncomment Firestore client example for actual GCP interaction.") except Exception as e: print(f"Could not initialize Firestore client (expected outside GCF): {e}")
Debug
Known issues
gotchaThis library is not a general-purpose Google Cloud Functions utility. It has strong dependencies on Django and Django REST Framework, implying it's intended for specific architectural patterns where a Django/DRF application is deployed as a GCF. Attempting to use it in a pure, minimalist GCF environment without these frameworks will likely lead to `ModuleNotFoundError` or unexpected behavior.
fix
Ensure your GCF project includes Django and Django REST Framework in its `requirements.txt` if using this library. Reconsider if `nsj-gcf-utils` is the right tool for pure Python/minimalist GCFs without a Django/DRF backend.
affects: All versions
gotchaGoogle Cloud service clients (e.g., Firestore, Storage, Secret Manager) initialized by this library will rely on the default Google Cloud authentication mechanism, typically the GCF's associated service account. Incorrect IAM permissions for this service account will lead to `PermissionDenied` errors.
fix
Verify that the Google Cloud Function's service account has the necessary IAM roles and permissions (e.g., `roles/datastore.user` for Firestore, `roles/storage.objectViewer` for Storage) for the resources it attempts to access.
affects: All versions
gotchaThe `FunctionContext` object populated by `get_current_function_context` relies on specific HTTP headers (e.g., `X-Request-ID`, `X-User-ID`, `X-Entity-ID`) being present in the incoming GCF request. If these headers are missing or not propagated, context properties might be `None` or raise `AttributeError`.
fix
Ensure that upstream callers (e.g., API Gateway, other services) are correctly populating these headers. For local testing, mock the `request` object with appropriate headers, as shown in the quickstart.
affects: All versions
Upgrade
Version history
1.1.0latest on PyPI · released Nov 17, 2025
Audit
Dependencies
DjangorequiredRequired for core functionality, as the library integrates heavily with Django/DRF applications deployed to GCF.
djangorestframeworkrequiredEssential for handling API requests and responses within a Django/DRF context in GCF.
google-cloud-storagerequiredFor interacting with Google Cloud Storage services.
google-cloud-tasksrequiredFor interacting with Google Cloud Tasks services.
google-cloud-secret-managerrequiredFor accessing secrets managed by Google Cloud Secret Manager.
cryptographyrequiredA core dependency for secure operations, often pulled in by other Google Cloud client libraries.
Agent activity
31 hits · last 30 days
node
28
Amazon
1
OpenAI (training)
1
Resources
nsj-gcf-utils — pip install nsj-gcf-utils · libregistry