Skip to content

Instantly share code, notes, and snippets.

@ericflo
Created March 21, 2012 00:15
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ericflo/2142891 to your computer and use it in GitHub Desktop.
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
Copy link

ashot commented Mar 21, 2012

whats wrong with the client lib?

@ericflo
Copy link
Author

ericflo commented Mar 21, 2012

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
Copy link
Author

ericflo commented Mar 21, 2012

Also, their client lib wildly abuses global state.

import stripe
stripe.api_key = 'my_key'

No thanks!

@ashot
Copy link

ashot commented Mar 21, 2012 via email

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