Skip to content

Commit 7d39255

Browse files
adding plan text parser
1 parent 616d6cf commit 7d39255

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

commons/utils/__init__.py

Whitespace-only changes.

commons/utils/parse_plain_text.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import json
2+
3+
from rest_framework.parsers import BaseParser
4+
5+
6+
class PlainTextParser(BaseParser):
7+
"""
8+
Plain text parser that handles SNS messages sent as text/plain
9+
"""
10+
11+
media_type = "text/plain"
12+
13+
def parse(self, stream, media_type=None, parser_context=None):
14+
"""
15+
Simply return a string representing the body of the request.
16+
"""
17+
try:
18+
return json.loads(stream.read().decode("utf-8"))
19+
except json.JSONDecodeError as e:
20+
raise e

email_config/views.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
from django.utils.decorators import method_decorator
22
from django.views.decorators.csrf import csrf_exempt
3+
from rest_framework.parsers import JSONParser
34
from rest_framework.permissions import AllowAny
45
from rest_framework.viewsets import ViewSet
56

67
from commons.api.responses import ResponseFactory
8+
from commons.utils.parse_plain_text import PlainTextParser
79
from .services import BlockedEmailService
810

911

1012
@method_decorator(csrf_exempt, name="dispatch")
1113
class BlockedEmailView(ViewSet):
1214
permission_classes = [AllowAny]
15+
parser_classes = [JSONParser, PlainTextParser]
1316

1417
def __init__(self, blocked_email_service=None, **kwargs):
1518
super().__init__(**kwargs)

0 commit comments

Comments
 (0)