Created
November 9, 2016 02:07
-
-
Save bartdag/dadf7f43233fce00c3662f6f0b0a8ae3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from contextlib import contextmanager | |
import logging | |
logger = logging.getLogger("root") | |
class History(object): | |
def __init__(self, value): | |
self.value = value | |
def call_web_api(raise_error=False): | |
"""Replace this by a real call to zeep | |
""" | |
if raise_error: | |
raise ValueError("AN ERROR") | |
else: | |
return "SUCCESS" | |
@contextmanager | |
def logwsdl(history): | |
captured_value = [] | |
def wrapper(value): | |
"""Capture the value before returning it. | |
There must be simpler way I'm not seeing because I'm too tired :-) | |
""" | |
captured_value.append(value) | |
return value | |
try: | |
yield wrapper | |
logger.warning("Logged successful response: {0}".format(captured_value[0])) | |
except Exception: | |
logger.error( | |
"An error occurred. Here is the history: {0}" | |
.format(history.value)) | |
raise | |
if __name__ == "__main__": | |
# Just a mock. Use zeep history instead of this | |
history = History("some historic value") | |
response = "" | |
with logwsdl(history) as log: | |
response = log(call_web_api(False)) | |
print(response) | |
response = "" | |
with logwsdl(history) as log: | |
response = log(call_web_api(True)) | |
print(response) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment