Skip to content

Instantly share code, notes, and snippets.

@ericflo
Created March 21, 2012 00:15
Show Gist options
  • Select an option

  • Save ericflo/2142891 to your computer and use it in GitHub Desktop.

Select an option

Save ericflo/2142891 to your computer and use it in GitHub Desktop.
Replaced my use of Stripe's Python API with this
import httplib
import base64
import contextlib
import simplejson
import urllib
from django.conf import settings
def stripe_fetch(resource, method='GET', params=None, secret=settings.STRIPE_SECRET, prefix='/v1'):
headers = {
'Accept': 'application/json',
'User-Agent': 'Clutch 1.0',
'Authorization': 'Basic %s' % (base64.b64encode('%s:' % (secret,)),),
}
if params:
flattened = {}
for key, val in params.iteritems():
if getattr(val, 'iteritems', None) is not None:
for sub_key, sub_val in val.iteritems():
flattened['%s[%s]' % (key, sub_key)] = sub_val
else:
flattened[key] = val
body = urllib.urlencode(flattened)
else:
body = ''
with contextlib.closing(httplib.HTTPSConnection('api.stripe.com')) as conn:
conn.request(method, prefix + resource, body, headers)
raw_data = conn.getresponse().read()
return simplejson.loads(raw_data)
@ashot

ashot commented Mar 21, 2012

Copy link
Copy Markdown

whats wrong with the client lib?

@ericflo

ericflo commented Mar 21, 2012

Copy link
Copy Markdown
Author

It gives me back these weird objects that are hard to use and are largely undefined. I just want the (decoded) JSON that the API returns.

@ericflo

ericflo commented Mar 21, 2012

Copy link
Copy Markdown
Author

Also, their client lib wildly abuses global state.

import stripe
stripe.api_key = 'my_key'

No thanks!

@ashot

ashot commented Mar 21, 2012 via email

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment