Skip to content

Commit fb3ca6c

Browse files
committed
read req body without renderer_context,added warns
1 parent 0759ad9 commit fb3ca6c

File tree

2 files changed

+37
-20
lines changed

2 files changed

+37
-20
lines changed

usagelogger/django.py

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,40 +24,45 @@ def __init__(self, get_response):
2424
url=__read_settings__("url"), rules=__read_settings__("rules")
2525
)
2626

27-
def prepare_request(self, request, response):
27+
def prepare_request_body(self, request, response=None):
2828
RE_PATTERN = r'(?<=; filename=")(.*?)"\\r\\nContent-Type: (.*?)\\r\\n(.*?)((-+)([a-zA-Z0-9_.-]+)\\r\\nContent-Disposition:)' # noqa
2929
RE_REPLACE_TO = r'\1"\\r\\nContent-Type: \2\\r\\n\\r\\n<file-data>\\r\\n\4'
3030
request.encoding = "utf-8"
3131
is_multipart = request.content_type == "multipart/form-data"
3232

33-
try:
34-
if is_multipart:
35-
request._rsf_body = (
36-
re.sub(
37-
pattern=RE_PATTERN,
38-
repl=RE_REPLACE_TO,
39-
string="%r" % request.body,
33+
if response is None:
34+
try:
35+
if is_multipart:
36+
body = (
37+
re.sub(
38+
pattern=RE_PATTERN,
39+
repl=RE_REPLACE_TO,
40+
string="%r" % request.body,
41+
)
42+
.encode()
43+
.decode("unicode_escape")
4044
)
41-
.encode()
42-
.decode("unicode_escape")
43-
)
44-
else:
45-
request._rsf_body = request.body.decode(request.encoding)
46-
except RawPostDataException:
45+
else:
46+
body = request.body.decode(request.encoding)
47+
except RawPostDataException:
48+
body = None
49+
else:
4750
try:
48-
request._rsf_body = str(response.renderer_context["request"].data)
51+
body = str(response.renderer_context["request"].data)
4952
except AttributeError:
50-
request._rsf_body = request.readlines()
53+
body = request.readlines()
5154

52-
return request
55+
return body
5356

5457
def __call__(self, request):
5558
start_time = time.time()
59+
request_body = self.prepare_request_body(request)
5660
response = self.get_response(request)
5761
interval = str((time.time() - start_time) * 1000)
5862
method = request.method
59-
60-
_request = self.prepare_request(request, response)
63+
64+
if request_body is None:
65+
request_body = self.prepare_request_body(request, response)
6166

6267
HttpMessage.send(
6368
self.logger,
@@ -66,7 +71,7 @@ def __call__(self, request):
6671
url=str(request.build_absolute_uri()),
6772
headers=request.headers,
6873
params=request.POST if method == "POST" else request.GET,
69-
body=_request._rsf_body,
74+
body=request_body,
7075
),
7176
response=HttpResponseImpl(
7277
status=response.status_code,

usagelogger/http_logger.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# © 2016-2021 Resurface Labs Inc.
33

44
import json
5+
import logging
56
from typing import List, Optional
67

78
from .base_logger import BaseLogger
@@ -24,6 +25,14 @@ def __init__(
2425
rules: Optional[str] = None,
2526
) -> None:
2627

28+
warner = logging.getLogger(__name__)
29+
argtype_warn = "Resurface: Invalid type for {} " \
30+
"(argument should be a {}). Logger won't be enabled."
31+
if url and type(url) != str:
32+
warner.error(argtype_warn.format("url", "string"))
33+
if rules and type(rules) != str:
34+
warner.error(argtype_warn.format("rules", "string"))
35+
2736
super().__init__(
2837
self.AGENT,
2938
enabled=enabled,
@@ -47,6 +56,9 @@ def __init__(
4756
):
4857
self._enableable = False
4958
self._enabled = False
59+
60+
if not self.enabled:
61+
warner.error("Resurface: Logger is not enabled.")
5062

5163
@property
5264
def rules(self) -> HttpRules:

0 commit comments

Comments
 (0)