Skip to content

Commit 33ae26d

Browse files
author
Fabio Todaro
committed
Rework requests Mixin and Session
1 parent d1b46c1 commit 33ae26d

File tree

3 files changed

+21
-35
lines changed

3 files changed

+21
-35
lines changed

nephthys/clients/requests_session.py renamed to nephthys/clients/requests.py

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22
from datetime import datetime
33
from urllib.parse import parse_qs, urlparse
4-
from requests.sessions import Session
4+
from requests.sessions import Session as RequestsSession
55

66
from nephthys import FilterLoggerAdapter, RequestLogRecord, Log
77
from nephthys.filters.requests import BodyTypeFilter
@@ -78,7 +78,7 @@ class NephthysMixin:
7878

7979
_logger = None
8080

81-
def init_nephthys_logger(self, log_tag, log_filters):
81+
def __init__(self, log_tag=None, log_filters=None, *args, **kwargs):
8282
"""
8383
:param log_tag: The tag that will identify logs from this Session
8484
:type log_tag: string
@@ -93,6 +93,7 @@ def init_nephthys_logger(self, log_tag, log_filters):
9393
self._logger = FilterLoggerAdapter(
9494
logger=logger, filters=_log_filters, extra_tags=[log_tag]
9595
)
96+
super().__init__(*args, **kwargs)
9697

9798
@catch_logger_exception
9899
def _send_log_record(
@@ -121,28 +122,6 @@ def _send_log_record(
121122
else:
122123
self._logger.info(Log(log_rec))
123124

124-
125-
class NephthysSession(Session, NephthysMixin):
126-
"""
127-
Provides a requests.session.Session overriding .send() method so that it logs
128-
each request with an instance of nephthys.FilterLoggerAdapter.
129-
130-
This class can be used as a concrete implementation of requests.session.Session
131-
in an existing project.
132-
If said project already uses a custom implementation of Session, this class can
133-
be used as an example of how to add NephthysMixin to it.
134-
"""
135-
136-
def __init__(self, log_tag=None, log_filters=None):
137-
"""
138-
:param log_tag: The tag that will identify logs from this Session
139-
:type log_tag: string
140-
:param log_filters: List of additional IRequestFilter
141-
:type log_filters: list
142-
"""
143-
super().__init__()
144-
self.init_nephthys_logger(log_tag, log_filters)
145-
146125
def send(self, request, **kwargs):
147126
start_time = datetime.utcnow().timestamp()
148127

@@ -165,3 +144,10 @@ def send(self, request, **kwargs):
165144
)
166145

167146
return response
147+
148+
149+
class Session(NephthysMixin, RequestsSession):
150+
"""
151+
Provides a requests.session.Session with Nephthys Logging.
152+
"""
153+
pass

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def make_long_description():
1414
description="Advanced Python Logger",
1515
long_description=make_long_description(),
1616
long_description_content_type="text/markdown",
17-
version="0.4.0",
17+
version="0.5.0",
1818
author="Fabio Todaro",
1919
license="MIT",
2020
author_email="ft@ovalmoney.com",

tests/test_clients/test_requests_session.py renamed to tests/test_clients/test_requests.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
import pytest
99
import requests
1010

11-
from nephthys.clients.requests_session import (
11+
from nephthys.clients.requests import (
1212
catch_logger_exception,
1313
decorate_log_request,
1414
decorate_log_response,
15-
NephthysSession)
15+
Session)
1616
from nephthys.filters.requests import BODY_NOT_LOGGABLE
1717

1818

@@ -127,7 +127,7 @@ def test_send_log_record(caplog, m):
127127
caplog.set_level(logging.INFO)
128128
m.get("https://ovalmoney.com/user", status_code=200)
129129

130-
s = NephthysSession()
130+
s = Session()
131131
s._logger.info = MagicMock()
132132
s.get("https://ovalmoney.com/user")
133133

@@ -138,7 +138,7 @@ def test_send_log_record_exception(caplog, m):
138138
caplog.set_level(logging.INFO)
139139
m.get("https://ovalmoney.com/user", exc=requests.exceptions.ConnectTimeout)
140140

141-
s = NephthysSession()
141+
s = Session()
142142
s._logger.exception = MagicMock()
143143
with pytest.raises(requests.exceptions.ConnectTimeout):
144144
s.get("https://ovalmoney.com/user")
@@ -154,7 +154,7 @@ def test_raw_data_reponse_log(caplog, m):
154154
content=b"\xFF\x8F",
155155
)
156156

157-
s = NephthysSession()
157+
s = Session()
158158
s.get("https://ovalmoney.com/raw_data")
159159

160160
log_rec = [rec for rec in caplog.records][0]
@@ -168,7 +168,7 @@ def test_raw_data_request_log(caplog, m):
168168
caplog.set_level(logging.INFO)
169169
m.post("https://ovalmoney.com/raw_data")
170170

171-
s = NephthysSession()
171+
s = Session()
172172
s.post("https://ovalmoney.com/raw_data", headers={"Content-Type": "video"}, data=b"\xFF\x8F")
173173

174174
log_rec = [rec for rec in caplog.records][0]
@@ -181,7 +181,7 @@ def test_raw_data_request_log(caplog, m):
181181
def test_session_fail_http(m):
182182
m.get("https://ovalmoney.com/user", exc=requests.exceptions.ConnectTimeout)
183183

184-
s = NephthysSession()
184+
s = Session()
185185
with pytest.raises(requests.exceptions.ConnectTimeout):
186186
s.get("https://ovalmoney.com/user")
187187

@@ -190,7 +190,7 @@ def test_session_fail_http_log(caplog, m):
190190
caplog.set_level(logging.INFO)
191191
m.get("https://ovalmoney.com/user", exc=requests.exceptions.ConnectTimeout)
192192

193-
s = NephthysSession()
193+
s = Session()
194194
with pytest.raises(requests.exceptions.ConnectTimeout):
195195
s.get("https://ovalmoney.com/user")
196196

@@ -213,7 +213,7 @@ def test_session_success(caplog, m):
213213
status_code=200,
214214
)
215215

216-
s = NephthysSession()
216+
s = Session()
217217
response = s.get("https://ovalmoney.com/user", params={"key1": "value1", "key2": ["value2", "value3"]})
218218

219219
assert response.headers
@@ -231,7 +231,7 @@ def test_session_success_log(caplog, m):
231231
)
232232

233233
with freeze_time(datetime.utcnow()):
234-
s = NephthysSession(log_tag="test")
234+
s = Session(log_tag="test")
235235
s.get("https://ovalmoney.com/user", params={"key1": "value1", "key2": ["value2", "value3"]})
236236
now = datetime.utcnow().timestamp()
237237

0 commit comments

Comments
 (0)