Skip to content

Commit

Permalink
Add django_auth to ninja API
Browse files Browse the repository at this point in the history
  • Loading branch information
pamella committed Jun 17, 2024
1 parent d0cf088 commit f8105b0
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 17 deletions.
9 changes: 8 additions & 1 deletion django_ai_assistant/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from langchain_core.messages import message_to_dict
from ninja import NinjaAPI
from ninja.operation import Operation
from ninja.security import django_auth

from django_ai_assistant import package_name, version
from django_ai_assistant.api.schemas import (
Expand All @@ -26,7 +27,13 @@ def get_openapi_operation_id(self, operation: Operation) -> str:
return (package_name + "_" + name).replace(".", "_")


api = API(title=package_name, version=version, urls_namespace="django_ai_assistant")
api = API(
title=package_name,
version=version,
urls_namespace="django_ai_assistant",
# Add auth to all endpoints
auth=django_auth,
)


@api.exception_handler(AIUserNotAllowedError)
Expand Down
7 changes: 1 addition & 6 deletions example/assets/js/components/Chat/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,7 @@ function ChatMessageList({
deleteMessage,
}: {
messages: ThreadMessagesSchemaOut[];
deleteMessage: ({
threadId,
messageId,
}: {
messageId: string;
}) => Promise<void>;
deleteMessage: ({ messageId }: { messageId: string }) => Promise<void>;
}) {
if (messages.length === 0) {
return <Text c="dimmed">No messages.</Text>;
Expand Down
77 changes: 67 additions & 10 deletions frontend/openapi_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@
}
}
}
}
},
"security": [
{
"SessionAuth": []
}
]
}
},
"/assistants/{assistant_id}/": {
Expand Down Expand Up @@ -55,7 +60,12 @@
}
}
}
}
},
"security": [
{
"SessionAuth": []
}
]
}
},
"/threads/": {
Expand All @@ -78,7 +88,12 @@
}
}
}
}
},
"security": [
{
"SessionAuth": []
}
]
},
"post": {
"operationId": "django_ai_assistant_create_thread",
Expand All @@ -105,7 +120,12 @@
}
},
"required": true
}
},
"security": [
{
"SessionAuth": []
}
]
}
},
"/threads/{thread_id}/": {
Expand Down Expand Up @@ -134,7 +154,12 @@
}
}
}
}
},
"security": [
{
"SessionAuth": []
}
]
},
"patch": {
"operationId": "django_ai_assistant_update_thread",
Expand Down Expand Up @@ -171,7 +196,12 @@
}
},
"required": true
}
},
"security": [
{
"SessionAuth": []
}
]
},
"delete": {
"operationId": "django_ai_assistant_delete_thread",
Expand All @@ -191,7 +221,12 @@
"204": {
"description": "No Content"
}
}
},
"security": [
{
"SessionAuth": []
}
]
}
},
"/threads/{thread_id}/messages/": {
Expand Down Expand Up @@ -224,7 +259,12 @@
}
}
}
}
},
"security": [
{
"SessionAuth": []
}
]
},
"post": {
"operationId": "django_ai_assistant_create_thread_message",
Expand Down Expand Up @@ -254,7 +294,12 @@
}
},
"required": true
}
},
"security": [
{
"SessionAuth": []
}
]
}
},
"/threads/{thread_id}/messages/{message_id}/": {
Expand Down Expand Up @@ -285,7 +330,12 @@
"204": {
"description": "No Content"
}
}
},
"security": [
{
"SessionAuth": []
}
]
}
}
},
Expand Down Expand Up @@ -414,6 +464,13 @@
"title": "ThreadMessagesSchemaIn",
"type": "object"
}
},
"securitySchemes": {
"SessionAuth": {
"type": "apiKey",
"in": "cookie",
"name": "sessionid"
}
}
},
"servers": []
Expand Down

0 comments on commit f8105b0

Please sign in to comment.