Created
July 23, 2017 17:02
-
-
Save twiceyuan/3138e3d81e52f820b8682897319f5bd5 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
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