From 0dd9beb91a49c6cc291a1a2fd77d0394edcadae3 Mon Sep 17 00:00:00 2001 From: Pamella Bezerra Date: Wed, 19 Jun 2024 14:46:33 -0300 Subject: [PATCH] Add support for INIT_API_FN --- django_ai_assistant/api/views.py | 30 +++++++++++++++++------------- django_ai_assistant/conf.py | 1 + docs/tutorial.md | 25 +++++++++++++++++++++++++ example/example/settings.py | 1 + tests/settings.py | 1 + 5 files changed, 45 insertions(+), 13 deletions(-) diff --git a/django_ai_assistant/api/views.py b/django_ai_assistant/api/views.py index b5a19bb..8cdb874 100644 --- a/django_ai_assistant/api/views.py +++ b/django_ai_assistant/api/views.py @@ -16,26 +16,30 @@ ThreadSchema, ThreadSchemaIn, ) +from django_ai_assistant.conf import app_settings from django_ai_assistant.exceptions import AIUserNotAllowedError from django_ai_assistant.helpers import use_cases from django_ai_assistant.models import Message, Thread -class API(NinjaAPI): - # Force "operationId" to be like "django_ai_assistant_delete_thread" - def get_openapi_operation_id(self, operation: Operation) -> str: - name = operation.view_func.__name__ - return (package_name + "_" + name).replace(".", "_") +def init_api(): + class API(NinjaAPI): + # Force "operationId" to be like "django_ai_assistant_delete_thread" + def get_openapi_operation_id(self, operation: Operation) -> str: + name = operation.view_func.__name__ + return (package_name + "_" + name).replace(".", "_") + + return API( + title=package_name, + version=version, + urls_namespace="django_ai_assistant", + # Add auth to all endpoints + auth=django_auth, + csrf=True, + ) -api = API( - title=package_name, - version=version, - urls_namespace="django_ai_assistant", - # Add auth to all endpoints - auth=django_auth, - csrf=True, -) +api = app_settings.call_fn("INIT_API_FN") @api.exception_handler(AIUserNotAllowedError) diff --git a/django_ai_assistant/conf.py b/django_ai_assistant/conf.py index 6d25e77..7ebe069 100644 --- a/django_ai_assistant/conf.py +++ b/django_ai_assistant/conf.py @@ -9,6 +9,7 @@ DEFAULTS = { + "INIT_API_FN": "django_ai_assistant.api.views.init_api", "CAN_CREATE_THREAD_FN": "django_ai_assistant.permissions.allow_all", "CAN_VIEW_THREAD_FN": "django_ai_assistant.permissions.owns_thread", "CAN_UPDATE_THREAD_FN": "django_ai_assistant.permissions.owns_thread", diff --git a/docs/tutorial.md b/docs/tutorial.md index e1ab309..2e15669 100644 --- a/docs/tutorial.md +++ b/docs/tutorial.md @@ -243,6 +243,31 @@ urlpatterns = [ The built-in API supports retrieval of Assistants info, as well as CRUD for Threads and Messages. It has a OpenAPI schema that you can explore at `ai-assistant/docs/`. + +#### Configuring the API + +The built-in API is implemented using [Django Ninja](https://django-ninja.dev/reference/api/). By default, it is initialized with the following setting: + +```python title="myproject/settings.py" +AI_ASSISTANT_INIT_API_FN = "django_ai_assistant.api.views.init_api" +``` + +You can override this setting in your Django project's `settings.py` to customize the API, such as using a different authentication method or modifying other configurations. + +The method signature for `AI_ASSISTANT_INIT_API_FN` is as follows: + +```python +from ninja import NinjaAPI + + +def init_api(): + return NinjaAPI( + ... + ) +``` + +By providing your own implementation of init_api, you can tailor the API setup to better fit your project's requirements. + ### Configuring permissions The API uses the helpers from the `django_ai_assistant.use_cases` module, which have permission checks diff --git a/example/example/settings.py b/example/example/settings.py index 5116f69..eb58af8 100644 --- a/example/example/settings.py +++ b/example/example/settings.py @@ -157,6 +157,7 @@ # django-ai-assistant +AI_ASSISTANT_INIT_API_FN = "django_ai_assistant.api.views.init_api" AI_ASSISTANT_CAN_CREATE_THREAD_FN = "django_ai_assistant.permissions.allow_all" AI_ASSISTANT_CAN_VIEW_THREAD_FN = "django_ai_assistant.permissions.owns_thread" AI_ASSISTANT_CAN_UPDATE_THREAD_FN = "django_ai_assistant.permissions.owns_thread" diff --git a/tests/settings.py b/tests/settings.py index cef44a6..47aa14c 100644 --- a/tests/settings.py +++ b/tests/settings.py @@ -107,6 +107,7 @@ # django-ai-assistant # NOTE: set a OPENAI_API_KEY on .env.tests file at root when updating the VCRs. +AI_ASSISTANT_INIT_API_FN = "django_ai_assistant.api.views.init_api" AI_ASSISTANT_CAN_CREATE_THREAD_FN = "django_ai_assistant.permissions.allow_all" AI_ASSISTANT_CAN_VIEW_THREAD_FN = "django_ai_assistant.permissions.owns_thread" AI_ASSISTANT_CAN_UPDATE_THREAD_FN = "django_ai_assistant.permissions.owns_thread"