Skip to content

Instantly share code, notes, and snippets.

@cjhgo
Forked from twiceyuan/interceptor_mock.py
Created July 23, 2017 17:03
Show Gist options
  • Save cjhgo/415a3a0944dee098947cf0455043e743 to your computer and use it in GitHub Desktop.
Save cjhgo/415a3a0944dee098947cf0455043e743 to your computer and use it in GitHub Desktop.
import functools
def log(func):
@functools.wraps(func)
def wrapper(*args):
print(u"request body: {}".format(args[0]))
response = func(*args)
print u"response body: {}".format(response)
return response
return wrapper
def append_params(func):
@functools.wraps(func)
def wrapper(*args):
response = func(u"[{}]".format(args[0]))
return u"={}=".format(response)
return wrapper
class RequestManager:
def __init__(self):
pass
interceptors = []
def add_interceptor(self, interceptor):
self.interceptors.append(interceptor)
def request(self, body):
final_request = RequestManager.__real_request
for interceptor in self.interceptors:
final_request = interceptor(final_request)
final_request(body)
@staticmethod
def __real_request(body):
print u"------- real call -------"
return u"{}'s response".format(body)
manager = RequestManager()
manager.add_interceptor(RequestManager.request)
manager.add_interceptor(log)
manager.request(u"Hello")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment