Skip to content

Commit

Permalink
Use the warnings module
Browse files Browse the repository at this point in the history
  • Loading branch information
jameshageman-stripe committed Jan 29, 2019
1 parent f38bb7e commit e48f0e6
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 50 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ production use, but `debug` is also available for more verbosity.

There are a few options for enabling it:

1. Set the environment variable `STRIPE_LOG` to the value `debug`, `info` or
`warning`
1. Set the environment variable `STRIPE_LOG` to the value `debug` or `info`

```
$ export STRIPE_LOG=debug
Expand Down
8 changes: 6 additions & 2 deletions stripe/api_requestor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import platform
import time
import uuid
import warnings

import stripe
from stripe import error, oauth_error, http_client, version, util, six
Expand Down Expand Up @@ -93,8 +94,11 @@ def __init__(
elif stripe.default_http_client:
self._client = stripe.default_http_client
if proxy != self._default_proxy:
util.log_warning(
"stripe.proxy was updated after sending a request - this is a no-op. To use a different proxy, set stripe.default_http_client to a new client configured with the proxy."
warnings.warn(
"Warning: stripe.proxy was updated after sending a" +
" request - this is a no-op. To use a different proxy," +
" set stripe.default_http_client to a new client" +
" configured with the proxy."
)
else:
# If the stripe.default_http_client has not been set by the user
Expand Down
12 changes: 2 additions & 10 deletions stripe/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"io",
"parse_qsl",
"utf8",
"log_warning",
"log_info",
"log_debug",
"dashboard_link",
Expand All @@ -42,9 +41,9 @@ def is_appengine_dev():


def _console_log_level():
if stripe.log in ["debug", "info", "warning"]:
if stripe.log in ["debug", "info"]:
return stripe.log
elif STRIPE_LOG in ["debug", "info", "warning"]:
elif STRIPE_LOG in ["debug", "info"]:
return STRIPE_LOG
else:
return None
Expand All @@ -64,13 +63,6 @@ def log_info(message, **params):
logger.info(msg)


def log_warning(message, **params):
msg = logfmt(dict(message=message, **params))
if _console_log_level() in ["debug", "info", "warning"]:
print(msg, file=sys.stderr)
logger.warning(msg)


def _test_or_live_environment():
if stripe.api_key is None:
return
Expand Down
20 changes: 10 additions & 10 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sys
from threading import Thread
import json
import warnings

import stripe
import pytest
Expand All @@ -26,19 +27,16 @@ def setup_stripe(self):
"api_base": stripe.api_base,
"api_key": stripe.api_key,
"default_http_client": stripe.default_http_client,
"log": stripe.log,
"proxy": stripe.proxy,
}
stripe.api_base = "http://localhost:12111" # stripe-mock
stripe.api_key = "sk_test_123"
stripe.default_http_client = None
stripe.log = "warning"
stripe.proxy = None
yield
stripe.api_base = orig_attrs["api_base"]
stripe.api_key = orig_attrs["api_key"]
stripe.default_http_client = orig_attrs["default_http_client"]
stripe.log = orig_attrs["log"]
stripe.proxy = orig_attrs["proxy"]

def setup_mock_server(self, handler):
Expand Down Expand Up @@ -74,7 +72,7 @@ def do_GET(self):
stripe.Balance.retrieve()
assert MockServerRequestHandler.num_requests == 1

def test_hits_proxy_through_default_http_client(self, mocker):
def test_hits_proxy_through_default_http_client(self):
class MockServerRequestHandler(BaseHTTPRequestHandler):
num_requests = 0

Expand All @@ -90,18 +88,20 @@ def do_GET(self):
return

self.setup_mock_server(MockServerRequestHandler)
logger_mock = mocker.patch("stripe.util.log_warning")

stripe.proxy = "http://localhost:%s" % self.mock_server_port
stripe.Balance.retrieve()
assert MockServerRequestHandler.num_requests == 1

stripe.proxy = "http://bad-url"
logger_mock.assert_not_called()
stripe.Balance.retrieve()
logger_mock.assert_called_with(
"stripe.proxy was updated after sending a request - this is a no-op. To use a different proxy, set stripe.default_http_client to a new client configured with the proxy."
)

with warnings.catch_warnings(record=True) as w:
stripe.Balance.retrieve()
assert len(w) == 1
assert "stripe.proxy was updated after sending a request" in str(
w[0].message
)

assert MockServerRequestHandler.num_requests == 2

def test_hits_proxy_through_custom_client(self):
Expand Down
26 changes: 0 additions & 26 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ def test_log_debug(self, mocker):
# (STRIPE_LOG, stripe.log): should_output?
test_cases = [
LogTestCase(env=None, flag=None, should_output=False),
LogTestCase(env="warning", flag=None, should_output=False),
LogTestCase(env=None, flag="warning", should_output=False),
LogTestCase(env=None, flag="debug", should_output=True),
LogTestCase(env=None, flag="info", should_output=False),
LogTestCase(env="debug", flag=None, should_output=True),
Expand All @@ -93,8 +91,6 @@ def test_log_info(self, mocker):
# (STRIPE_LOG, stripe.log): should_output?
test_cases = [
LogTestCase(env=None, flag=None, should_output=False),
LogTestCase(env="warning", flag=None, should_output=False),
LogTestCase(env=None, flag="warning", should_output=False),
LogTestCase(env=None, flag="debug", should_output=True),
LogTestCase(env=None, flag="info", should_output=True),
LogTestCase(env="debug", flag=None, should_output=True),
Expand All @@ -111,28 +107,6 @@ def test_log_info(self, mocker):
mocker=mocker,
)

def test_log_warning(self, mocker):
# (STRIPE_LOG, stripe.log): should_output?
test_cases = [
LogTestCase(env=None, flag=None, should_output=False),
LogTestCase(env="warning", flag=None, should_output=True),
LogTestCase(env=None, flag="warning", should_output=True),
LogTestCase(env=None, flag="debug", should_output=True),
LogTestCase(env=None, flag="info", should_output=True),
LogTestCase(env="debug", flag=None, should_output=True),
LogTestCase(env="debug", flag="debug", should_output=True),
LogTestCase(env="debug", flag="info", should_output=True),
LogTestCase(env="info", flag=None, should_output=True),
LogTestCase(env="info", flag="debug", should_output=True),
LogTestCase(env="info", flag="info", should_output=True),
]
self.log_test_loop(
test_cases,
logging_func=util.log_warning,
logger_name="stripe.util.logger.warning",
mocker=mocker,
)

def test_logfmt(self):
cases = [
FmtTestCase(
Expand Down

0 comments on commit e48f0e6

Please sign in to comment.