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
muslpy 3.10–3.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 32.5MB
glibcpy 3.10–3.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}")
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.