Created
October 19, 2011 03:29
-
-
Save jordanorelli/1297412 to your computer and use it in GitHub Desktop.
it's decorators all the way down.
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 | |
from recurly.serialization import parse_xml | |
def injectcaller(fn): | |
""" | |
Attaches a "caller" attribute to a response object representing partial | |
function. The "caller" partial is a bound method that retains a | |
reference to its owner. | |
""" | |
@functools.wraps(fn) | |
def wraps(self, *args, **kwargs): | |
response = fn(self, *args, **kwargs) | |
response.caller = functools.partial(fn, self) | |
return response | |
return wraps | |
def autoparse(fn): | |
""" | |
Wraps python-requests calls to Recurly. Causes requests to raise | |
standard errors when appropriate. Results are parsed before being | |
returned. | |
""" | |
@injectcaller | |
@functools.wraps(fn) | |
def wraps(self, *args, **kwargs): | |
response = fn(self, *args, **kwargs) | |
if not response: | |
raise response.error | |
if hasattr(self, '_client'): | |
return parse_xml(response.content, self._client) | |
return parse_xml(response.content) | |
return wraps |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment