Skip to content

Commit

Permalink
feat: Swagger schema for environment document (#3789)
Browse files Browse the repository at this point in the history
  • Loading branch information
khvn26 authored Apr 17, 2024
1 parent ff2ce2e commit dd89326
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 5 deletions.
38 changes: 38 additions & 0 deletions api/api/openapi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from copy import deepcopy
from functools import lru_cache

import jsonref
from drf_yasg.openapi import Schema
from flag_engine.environments.models import EnvironmentModel

SKIP_PROPERTIES = [
"amplitude_config",
"dynatrace_config",
"heap_config",
"mixpanel_config",
"rudderstack_config",
"segment_config",
"webhook_config",
]
SKIP_DEFINITIONS = ["IntegrationModel", "WebhookModel"]


@lru_cache()
def get_environment_document_response() -> Schema:
model_json_schema = EnvironmentModel.model_json_schema(mode="serialization")

# Restrict segment rule recursion to two levels.
segment_rule_schema = deepcopy(model_json_schema["$defs"]["SegmentRuleModel"])
del segment_rule_schema["properties"]["rules"]
model_json_schema["$defs"]["SegmentRuleInnerModel"] = segment_rule_schema
model_json_schema["$defs"]["SegmentRuleModel"]["properties"]["rules"]["items"][
"$ref"
] = "#/$defs/SegmentRuleInnerModel"

# Remove integrations.
for prop in SKIP_PROPERTIES:
del model_json_schema["properties"][prop]
for definition in SKIP_DEFINITIONS:
del model_json_schema["$defs"][definition]

return Schema(**jsonref.replace_refs(model_json_schema))
3 changes: 3 additions & 0 deletions api/environments/sdk/views.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from core.constants import FLAGSMITH_UPDATED_AT_HEADER
from django.http import HttpRequest
from drf_yasg.utils import swagger_auto_schema
from rest_framework.response import Response
from rest_framework.views import APIView

from api.openapi import get_environment_document_response
from environments.authentication import EnvironmentKeyAuthentication
from environments.models import Environment
from environments.permissions.permissions import EnvironmentKeyPermissions
Expand All @@ -15,6 +17,7 @@ class SDKEnvironmentAPIView(APIView):
def get_authenticators(self):
return [EnvironmentKeyAuthentication(required_key_prefix="ser.")]

@swagger_auto_schema(responses={200: get_environment_document_response()})
def get(self, request: HttpRequest) -> Response:
environment_document = Environment.get_environment_document(
request.environment.api_key
Expand Down
5 changes: 2 additions & 3 deletions api/environments/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import logging

from django.db.models import Count
Expand All @@ -14,6 +11,7 @@
from rest_framework.request import Request
from rest_framework.response import Response

from api.openapi import get_environment_document_response
from environments.permissions.permissions import (
EnvironmentAdminPermission,
EnvironmentPermissions,
Expand Down Expand Up @@ -201,6 +199,7 @@ def user_permissions(self, request, *args, **kwargs):
serializer = UserObjectPermissionsSerializer(instance=permission_data)
return Response(serializer.data)

@swagger_auto_schema(responses={200: get_environment_document_response()})
@action(detail=True, methods=["GET"], url_path="document")
def get_document(self, request, api_key: str):
return Response(Environment.get_environment_document(api_key))
Expand Down
35 changes: 33 additions & 2 deletions api/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions api/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ flagsmith = "^3.6.0"
python-gnupg = "^0.5.1"
django-redis = "^5.4.0"
hubspot-api-client = "^8.2.1"
jsonref = "^1.1.0"

[tool.poetry.group.auth-controller]
optional = true
Expand Down

0 comments on commit dd89326

Please sign in to comment.