Skip to content

Commit

Permalink
Merge pull request #5 from peonone/only-log-request-debug-mode
Browse files Browse the repository at this point in the history
only show request log on debug mode (#3)
  • Loading branch information
stav authored Jul 16, 2020
2 parents 6ac0801 + 9e8cf4b commit c5452ff
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
1 change: 0 additions & 1 deletion src/varanus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
[tool.poetry.plugins]
"""
from .__version__ import __version__ # noqa app version
from .__patch__ import __patches__ # noqa monkeypatches
from .__main__ import main as app # noqa create cli app
from .utils import see # noqa debugging
from .api import * # noqa python interface
6 changes: 5 additions & 1 deletion src/varanus/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from .__version__ import __version__
from .session import Session
from .__patch__ import apply_patch

CLINS = 'varanus.clier'

Expand Down Expand Up @@ -46,10 +47,13 @@ def clean_up(self, cmd, result, err):
def main():
""" Execute the application
"""
VaranusApp().run(sys.argv[1:])
app = VaranusApp()
apply_patch(app)
app.run(sys.argv[1:])


# Make the script runnable

if __name__ == "__main__":
raise SystemExit(main())

36 changes: 21 additions & 15 deletions src/varanus/__patch__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
.. todo:: Inject code instead of replacing routines.
"""
import logging
from functools import partialmethod

import requests
import scrapinghub.legacy
import scrapinghub.client
Expand All @@ -25,7 +27,7 @@
}


def HubstorageClient_request(self, is_idempotent=False, **kwargs):
def HubstorageClient_request(self, is_idempotent=False, app=None, **kwargs):
"""Execute an HTTP request with the current client session
Use the retry policy configured in the client when is_idempotent is True.
Expand All @@ -34,11 +36,12 @@ def HubstorageClient_request(self, is_idempotent=False, **kwargs):

def invoke_request():
r = self.session.request(**kwargs)
log_response(
r,
source=f'{__name__}:scrapinghub.client.HubstorageClient.request',
color='blue',
)
if app.options.debug:
log_response(
r,
source=f'{__name__}:scrapinghub.client.HubstorageClient.request',
color='blue',
)
try:
r.raise_for_status()
return r
Expand All @@ -52,7 +55,7 @@ def invoke_request():
return invoke_request()


def Connection_request(self, url, data, headers, format, raw, files=None):
def Connection_request(self, url, data, headers, format, raw, files=None, app=None):
"""Performs the request using and returns the content deserialized,
based on given `format`
Expand All @@ -74,13 +77,16 @@ def Connection_request(self, url, data, headers, format, raw, files=None):
response = self._session.post(url, headers=headers,
data=data, files=files,
timeout=self._connection_timeout)
log_response(
response,
source=f'{__name__}:scrapinghub.legacy.Connection._request',
color='cyan',
)
return self._decode_response(response, format, raw)
if app.options.debug:
log_response(
response,
source=f'{__name__}:scrapinghub.legacy.Connection._request',
color='cyan',
)
return self._decode_response(response, format, raw)


def apply_patch(app):
scrapinghub.client.HubstorageClient.request = partialmethod(HubstorageClient_request, app=app)
scrapinghub.legacy.Connection._request = partialmethod(Connection_request, app=app)

scrapinghub.client.HubstorageClient.request = HubstorageClient_request
scrapinghub.legacy.Connection._request = Connection_request

0 comments on commit c5452ff

Please sign in to comment.