Skip to content

Commit d40d708

Browse files
authored
Add support for Sanic 20.3.0 and up (#52)
1 parent 12a6cdb commit d40d708

4 files changed

Lines changed: 23 additions & 19 deletions

File tree

src/dockerflow/sanic/app.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,14 @@ def _request_middleware(self, request):
141141
"""
142142
The request middleware.
143143
"""
144-
request["_id"] = str(uuid.uuid4())
145-
request["_start_timestamp"] = time.time()
144+
request.ctx.id = str(uuid.uuid4())
145+
request.ctx.start_timestamp = time.time()
146146

147147
def _response_middleware(self, request, response):
148148
"""
149149
The response middleware.
150150
"""
151-
if not request.get("_logged"):
151+
if not getattr(request.ctx, "logged", False):
152152
extra = self.summary_extra(request)
153153
self.summary_logger.info("", extra=extra)
154154

@@ -159,7 +159,7 @@ def _exception_handler(self, request, exception):
159159
extra = self.summary_extra(request)
160160
extra["errno"] = 500
161161
self.summary_logger.error(str(exception), extra=extra)
162-
request["_logged"] = True
162+
request.ctx.logged = True
163163

164164
def summary_extra(self, request):
165165
"""
@@ -175,15 +175,17 @@ def summary_extra(self, request):
175175
}
176176

177177
# the rid value to the current request ID
178-
request_id = request.get("_id", None)
179-
if request_id is not None:
180-
out["rid"] = request_id
178+
try:
179+
out["rid"] = request.ctx.id
180+
except AttributeError:
181+
pass
181182

182183
# and the t value to the time it took to render
183-
start_timestamp = request.get("_start_timestamp", None)
184-
if start_timestamp is not None:
184+
try:
185185
# Duration of request, in milliseconds.
186-
out["t"] = int(1000 * (time.time() - start_timestamp))
186+
out["t"] = int(1000 * (time.time() - request.ctx.start_timestamp))
187+
except AttributeError:
188+
pass
187189

188190
return out
189191

tests/constraints/sanic-20.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Sanic<21

tests/sanic/test_sanic.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,9 @@ def assert_log_record(caplog, errno=0, level=logging.INFO, rid=None, t=int, path
228228

229229
def test_request_summary(caplog, dockerflow, test_client):
230230
request, _ = test_client.get(headers=headers)
231-
assert isinstance(request.get("_start_timestamp"), float)
232-
assert request.get("_id") is not None
233-
assert_log_record(caplog, rid=request.get("_id"))
231+
assert isinstance(request.ctx.start_timestamp, float)
232+
assert request.ctx.id is not None
233+
assert_log_record(caplog, rid=request.ctx.id)
234234

235235

236236
def test_request_summary_exception(app, caplog, dockerflow, test_client):
@@ -240,7 +240,7 @@ def exception_raiser(request):
240240

241241
request, _ = test_client.get("/exception", headers=headers)
242242
record = assert_log_record(
243-
caplog, 500, logging.ERROR, request.get("_id"), path="/exception"
243+
caplog, 500, logging.ERROR, request.ctx.id, path="/exception"
244244
)
245245
assert record.getMessage() == "exception message"
246246

@@ -249,8 +249,8 @@ def test_request_summary_failed_request(app, caplog, dockerflow, test_client):
249249
@app.middleware
250250
def hostile_callback(request):
251251
# simulating resetting request changes
252-
del request["_id"]
253-
del request["_start_timestamp"]
252+
del request.ctx.id
253+
del request.ctx.start_timestamp
254254

255255
test_client.get(headers=headers)
256256
assert_log_record(caplog, rid=None, t=None)

tox.ini

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ envlist =
1010
py{36,37}-dj{21,22}-tests,
1111
# py{36,37,38}-dj30-tests,
1212
py{27,36,37,38}-fl{011,012,10}-tests,
13-
py{36,37,38}-s19-tests,
13+
py{36,37,38}-s{19,20}-tests,
1414

1515
[testenv]
1616
basepython =
@@ -27,7 +27,7 @@ deps =
2727
-rtests/requirements/default.txt
2828
dj{111,21,22,30}: -rtests/requirements/django.txt
2929
fl{011,012,10}: -rtests/requirements/flask.txt
30-
s19: -rtests/requirements/sanic.txt
30+
s{19,20}: -rtests/requirements/sanic.txt
3131
dj111: -ctests/constraints/django-1.11.txt
3232
dj21: -ctests/constraints/django-2.1.txt
3333
dj22: -ctests/constraints/django-2.2.txt
@@ -36,12 +36,13 @@ deps =
3636
fl012: -ctests/constraints/flask-0.12.txt
3737
fl10: -ctests/constraints/flask-1.0.txt
3838
s19: -ctests/constraints/sanic-19.txt
39+
s20: -ctests/constraints/sanic-20.txt
3940
py27: -ctests/constraints/python-2.7.txt
4041
commands =
4142
python --version
4243
dj{111,21,22,30}-tests: pytest tests/core/ tests/django --nomigrations {posargs:}
4344
fl{011,012,10}-tests: pytest tests/core/ tests/flask/ {posargs:}
44-
s19: pytest tests/core/ tests/sanic/
45+
s{19,20}: pytest tests/core/ tests/sanic/ {posargs:}
4546

4647
[testenv:py38-docs]
4748
basepython = python3.8

0 commit comments

Comments
 (0)