first cut at producing iterator interfaces to Stripe objects -- here I implement one for events

don't yield empty page in bookdata.py's grouper
remove extraneous import in gutenberg.py
expose stripe test key as a module variable to make it easier to create a StripeClient that will be in test mode (sc=StipeClient(api_key=TEST_STRIPE_SK))
pull/1/head
Raymond Yee 2012-10-18 06:59:08 -07:00
parent df17c3c865
commit 8a85aa88d3
3 changed files with 35 additions and 6 deletions

View File

@ -58,7 +58,8 @@ def grouper(iterable, page_size):
if len(page) == page_size:
yield page
page= []
yield page
if len(page):
yield page
class FreebaseException(Exception):
pass

View File

@ -24,7 +24,6 @@ import operator
import time
import re
from itertools import islice, izip
import logging
import random
import json

View File

@ -4,6 +4,7 @@
import logging
from datetime import datetime, timedelta
from pytz import utc
from itertools import islice
from django.conf import settings
@ -17,6 +18,17 @@ import stripe
logger = logging.getLogger(__name__)
# http://stackoverflow.com/questions/2348317/how-to-write-a-pager-for-python-iterators/2350904#2350904
def grouper(iterable, page_size):
page= []
for item in iterable:
page.append( item )
if len(page) == page_size:
yield page
page= []
if len(page):
yield page
class StripelibError(baseprocessor.ProcessorError):
pass
@ -46,8 +58,8 @@ try:
except Exception, e:
# currently test keys for Gluejar and for raymond.yee@gmail.com as standin for non-profit
logger.info('Exception {0} Need to use TEST STRIPE_*_KEYs'.format(e))
STRIPE_PK = 'pk_0EajXPn195ZdF7Gt7pCxsqRhNN5BF'
STRIPE_SK = 'sk_0EajIO4Dnh646KPIgLWGcO10f9qnH'
STRIPE_PK = TEST_STRIPE_PK
STRIPE_SK = TEST_STRIPE_SK
# set default stripe api_key to that of unglue.it
@ -162,7 +174,6 @@ class StripeClient(object):
def event(self):
return stripe.Event(api_key=self.api_key)
def create_token(self, card):
return stripe.Token(api_key=self.api_key).create(card=card)
@ -209,6 +220,24 @@ class StripeClient(object):
# https://stripe.com/docs/api?lang=python#list_charges
return stripe.Charge(api_key=self.api_key).all(count=count, offset=offset, customer=customer)
def list_events(self, **kwargs):
"""a generator for events"""
# type=None, created=None, count=None, offset=0
#
kwargs2 = kwargs.copy()
kwargs2.setdefault('offset', 0)
kwargs2.setdefault('count', 100)
more_items = True
while more_items:
items = self.event.all(**kwargs2)['data']
for item in items:
yield item
if len(items):
kwargs2['offset'] += len(items)
else:
more_items = False
# can't test Transfer in test mode: "There are no transfers in test mode."