{"library":"nsj-gcf-utils","title":"Nasajon GCF Utilities","description":"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.","language":"python","status":"active","last_verified":"Fri Apr 17","install":{"commands":["pip install nsj-gcf-utils"],"cli":null},"imports":["from nsj_gcf_utils.objects.function_context import get_current_function_context","from nsj_gcf_utils.objects.function_context import FunctionContext","from nsj_gcf_utils.firestore import get_firestore_client","from nsj_gcf_utils.exception import GcfException"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import json\nfrom nsj_gcf_utils.objects.function_context import get_current_function_context\nfrom nsj_gcf_utils.exception import GcfException\n\n# Simulate a Flask-like request object for local testing\n# In a real GCF environment, 'request' is provided by the framework.\nclass MockRequest:\n    def __init__(self, json_data, headers=None):\n        self._json_data = json_data\n        self.headers = headers if headers is not None else {}\n\n    @property\n    def json(self):\n        return self._json_data\n\n    def get_json(self):\n        return self._json_data\n\n    def get_data(self):\n        return json.dumps(self._json_data).encode('utf-8')\n\n\ndef my_gcf_handler(request):\n    try:\n        # Get the current function context\n        # In GCF, 'request' would be the actual Flask request object.\n        context = get_current_function_context(request)\n\n        # Access context properties (these would be populated by GCF headers)\n        print(f\"Request ID: {context.request_id}\")\n        print(f\"User ID: {context.user_id}\")\n        print(f\"Entity ID: {context.entity_id}\")\n\n        return \"OK\", 200\n    except GcfException as e:\n        print(f\"Error: {e}\")\n        return str(e), 500\n    except Exception as e:\n        print(f\"Unexpected error: {e}\")\n        return str(e), 500\n\n# Example usage with a mock request (simulating a GCF invocation)\nmock_headers = {\n    'X-Request-ID': 'mock-req-123',\n    'X-User-ID': 'mock-user-456',\n    'X-Entity-ID': 'mock-entity-789'\n}\nmock_json_payload = {'data': 'some_data'}\nmock_request = MockRequest(mock_json_payload, headers=mock_headers)\n\nresponse, status_code = my_gcf_handler(mock_request)\nprint(f\"\\nHandler Response: {response}, Status: {status_code}\")\n\n# Example of using Firestore client (requires actual GCP setup)\ntry:\n    # from nsj_gcf_utils.firestore import get_firestore_client\n    # firestore_client = get_firestore_client()\n    # print(\"Firestore client initialized successfully.\")\n    print(\"\\nUncomment Firestore client example for actual GCP interaction.\")\nexcept Exception as e:\n    print(f\"Could not initialize Firestore client (expected outside GCF): {e}\")","lang":"python","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}