Skip to content

Instantly share code, notes, and snippets.

@paulosman
Created November 9, 2013 01:17
Show Gist options
  • Save paulosman/7380196 to your computer and use it in GitHub Desktop.
Save paulosman/7380196 to your computer and use it in GitHub Desktop.
SoundCloud API client that uses a circuit breaker
import pybreaker
import soundcloud
import requests
sc_breaker = pybreaker.CircuitBreaker(fail_max=3, reset_timeout=30)
class LogListener(pybreaker.CircuitBreakerListener):
def before_call(self, cb, func, *args, **kwargs):
print "Before calling %s" % func
def failure(self, cb, exc):
print "Failure: %s" % repr(exc)
sc_breaker.add_listeners(LogListener())
class SoundCloudClient(object):
def __init__(self, *args, **kwargs):
self.client = soundcloud.Client(*args, **kwargs)
@sc_breaker
def playlists(self, *args, **kwargs):
return self.client.get('/me/playlists', *args, **kwargs)
@sc_breaker
def me(self):
return self.client.get('/me')
client = SoundCloudClient(access_token='paultest')
for i in range(10):
try:
playlists = client.playlists()
print [
(playlist.title, len(playlist.tracks)) for playlist in playlists
]
except requests.HTTPError, e:
print "Exception raised calling playlists(): %s" % e.message
except pybreaker.CircuitBreakerError, e:
pass
try:
info = client.me()
print info.fields()
except Exception, e:
print e.message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment