-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from glific/26-organization-api-keys
some cleanup; middlewares, proper api and error response. Refactoring…
- Loading branch information
Showing
9 changed files
with
155 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Generated by Django 4.2.6 on 2024-03-09 05:44 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('llm', '0010_organization_examples_text'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='organization', | ||
name='openai_key', | ||
field=models.CharField(max_length=255, null=True, unique=True), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import string, secrets | ||
|
||
|
||
def generate_session_id(length=6): | ||
alphanumeric = string.ascii_letters + string.digits | ||
return "".join(secrets.choice(alphanumeric) for _ in range(length)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from django.http import Http404 | ||
from django.http import JsonResponse | ||
from rest_framework.response import Response | ||
from rest_framework import status | ||
from llm.models import Organization | ||
from logging import basicConfig, INFO, getLogger | ||
|
||
basicConfig(level=INFO) | ||
logger = getLogger() | ||
|
||
|
||
class CustomMiddleware: | ||
@staticmethod | ||
def current_organization(request): | ||
api_key = request.headers.get("Authorization") | ||
if not api_key: | ||
return None | ||
|
||
try: | ||
return Organization.objects.get(api_key=api_key) | ||
except Organization.DoesNotExist: | ||
return None | ||
|
||
def __init__(self, get_response): | ||
self.get_response = get_response | ||
|
||
def __call__(self, request): | ||
# Code to be executed for each request before | ||
# the view (and later middleware) are called. | ||
logger.info("routing request via the middleware") | ||
|
||
org = CustomMiddleware.current_organization(request) | ||
|
||
if not org: | ||
return JsonResponse( | ||
{"error": "Invalid API key"}, | ||
status=status.HTTP_404_NOT_FOUND, | ||
) | ||
|
||
request.org = org | ||
response = self.get_response(request) | ||
|
||
# Code to be executed for each request/response after | ||
# the view is called. | ||
|
||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters