Skip to content

Instantly share code, notes, and snippets.

@jordanorelli
Created October 19, 2011 03:29
Show Gist options
  • Save jordanorelli/1297412 to your computer and use it in GitHub Desktop.
Save jordanorelli/1297412 to your computer and use it in GitHub Desktop.
it's decorators all the way down.
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