This commit is contained in:
2026-04-05 00:57:25 +03:30
parent 6d5ece1f5d
commit 32dbbed1af
26 changed files with 825 additions and 291 deletions
View File
+6
View File
@@ -0,0 +1,6 @@
from django.apps import AppConfig
class SensorExternalApiConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "sensor_external_api"
+22
View File
@@ -0,0 +1,22 @@
from django.conf import settings
from rest_framework.authentication import BaseAuthentication
from rest_framework.exceptions import AuthenticationFailed
class SensorExternalAPIKeyAuthentication(BaseAuthentication):
keyword = "Api-Key"
def authenticate(self, request):
provided_key = request.headers.get("X-API-Key") or request.headers.get("Authorization")
expected_key = getattr(settings, "SENSOR_EXTERNAL_API_KEY", "12345")
if not provided_key:
raise AuthenticationFailed("API key is required.")
if provided_key.startswith(f"{self.keyword} "):
provided_key = provided_key[len(self.keyword) + 1 :]
if provided_key != expected_key:
raise AuthenticationFailed("Invalid API key.")
return (None, None)
+5
View File
@@ -0,0 +1,5 @@
from rest_framework import serializers
class SensorExternalRequestSerializer(serializers.Serializer):
payload = serializers.JSONField(required=False, default=dict)
+20
View File
@@ -0,0 +1,20 @@
from django.db import ProgrammingError, OperationalError
from notifications.services import create_notification_for_farm_uuid
DEFAULT_SENSOR_EXTERNAL_FARM_UUID = "11111111-1111-1111-1111-111111111111"
def create_sensor_external_notification(*, payload=None):
payload = payload or {}
try:
return create_notification_for_farm_uuid(
farm_uuid=DEFAULT_SENSOR_EXTERNAL_FARM_UUID,
title="Sensor external API request",
message="A request was received by sensor_external_api.",
level="info",
metadata={"payload": payload},
)
except (ProgrammingError, OperationalError) as exc:
raise ValueError("Notifications table is not migrated.") from exc
+52
View File
@@ -0,0 +1,52 @@
from django.contrib.auth import get_user_model
from django.test import TestCase, override_settings
from rest_framework.test import APIRequestFactory
from farm_hub.models import FarmHub, FarmType
from notifications.models import FarmNotification
from .views import SensorExternalAPIView
@override_settings(SENSOR_EXTERNAL_API_KEY="12345")
class SensorExternalAPIViewTests(TestCase):
def setUp(self):
self.factory = APIRequestFactory()
self.user = get_user_model().objects.create_user(
username="sensor-external-user",
password="secret123",
email="sensor-external@example.com",
phone_number="09120000015",
)
self.farm_type = FarmType.objects.create(name="سنسور خارجی")
self.farm = FarmHub.objects.create(
owner=self.user,
farm_type=self.farm_type,
name="Farm External",
farm_uuid="11111111-1111-1111-1111-111111111111",
)
def test_requires_api_key(self):
request = self.factory.post("/api/sensor-external-api/", {"payload": {"temp": 12}}, format="json")
response = SensorExternalAPIView.as_view()(request)
self.assertEqual(response.status_code, 401)
def test_creates_notification_for_fixed_farm_uuid(self):
request = self.factory.post(
"/api/sensor-external-api/",
{"payload": {"temp": 12}},
format="json",
HTTP_X_API_KEY="12345",
)
response = SensorExternalAPIView.as_view()(request)
self.assertEqual(response.status_code, 201)
self.assertTrue(
FarmNotification.objects.filter(
farm=self.farm,
title="Sensor external API request",
).exists()
)
+7
View File
@@ -0,0 +1,7 @@
from django.urls import path
from .views import SensorExternalAPIView
urlpatterns = [
path("", SensorExternalAPIView.as_view(), name="sensor-external-api"),
]
+54
View File
@@ -0,0 +1,54 @@
from rest_framework import status
from rest_framework.permissions import AllowAny
from rest_framework.response import Response
from rest_framework.views import APIView
from drf_spectacular.utils import OpenApiParameter, OpenApiTypes, extend_schema
from config.swagger import code_response
from notifications.serializers import FarmNotificationSerializer
from .authentication import SensorExternalAPIKeyAuthentication
from .serializers import SensorExternalRequestSerializer
from .services import create_sensor_external_notification
class SensorExternalAPIView(APIView):
authentication_classes = [SensorExternalAPIKeyAuthentication]
permission_classes = [AllowAny]
@extend_schema(
tags=["Sensor External API"],
request=SensorExternalRequestSerializer,
parameters=[
OpenApiParameter(
name="X-API-Key",
type=OpenApiTypes.STR,
location=OpenApiParameter.HEADER,
required=True,
default="12345",
description="API key for sensor external API.",
)
],
responses={
201: code_response("SensorExternalAPIResponse", data=FarmNotificationSerializer()),
401: code_response("SensorExternalAPIUnauthorizedResponse"),
404: code_response("SensorExternalAPIFarmNotFoundResponse"),
503: code_response("SensorExternalAPINotificationsUnavailableResponse"),
},
)
def post(self, request):
serializer = SensorExternalRequestSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
try:
notification = create_sensor_external_notification(payload=serializer.validated_data.get("payload"))
except ValueError as exc:
if str(exc) == "Notifications table is not migrated.":
return Response(
{"code": 503, "msg": "Notifications table is not ready. Run migrations."},
status=status.HTTP_503_SERVICE_UNAVAILABLE,
)
return Response({"code": 404, "msg": "Farm not found."}, status=status.HTTP_404_NOT_FOUND)
data = FarmNotificationSerializer(notification).data
return Response({"code": 201, "msg": "success", "data": data}, status=status.HTTP_201_CREATED)