Skip to content
This repository has been archived by the owner on Sep 19, 2019. It is now read-only.

✨ Add create and retrieve methods for Source entry #7

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
133 changes: 93 additions & 40 deletions asyncio_stripe/stripe.py
Expand Up @@ -168,20 +168,44 @@ class Refund(object):
# description = attr.ib()


@attr.s(slots=True, frozen=True)
class Source(object):
id = attr.ib()
ach_credit_transfer = attr.ib(metadata={'expandable': True})
amount = attr.ib()
client_secret = attr.ib()
created = attr.ib()
currency = attr.ib()
metadata = attr.ib()
flow = attr.ib()
livemode = attr.ib()
owner = attr.ib()
receiver = attr.ib()
statement_descriptor = attr.ib()
status = attr.ib()
type = attr.ib()
usage = attr.ib()


DEFAULT_VERSION = '2017-02-14'
LAST_VERSION = '2018-11-08'


class Client(object):
def __init__(self, session, pk):
'''
def __init__(self, session, pk, version=DEFAULT_VERSION):
"""
Create a new Stripe client

@param session - aiohttp session
@param pk - private stripe key
'''
"""
self._session = session
self._auth = aiohttp.BasicAuth(pk)
self._url = 'https://api.stripe.com/v1'
self._version = version

async def _req(self, method, page, params=None):
'''
"""
Issue a request to the given page relative to the base Stripe API URL.

@param method - http method
Expand All @@ -191,11 +215,11 @@ async def _req(self, method, page, params=None):

@raises StripeError on error from stripe
@raises ParseError on failing to parse Stripe Object
'''
"""
url = self._url + '/' + page.lstrip('/')
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Stripe-Version': '2017-02-14',
'Stripe-Version': self._version,
}

if params is None:
Expand Down Expand Up @@ -239,7 +263,7 @@ async def _req(self, method, page, params=None):
return convert_json_response(body)

async def create_charge(self, amount, currency, **kwds):
'''
"""
Create a new charge.

Keyword arguments can be passed as defined by:
Expand All @@ -251,24 +275,24 @@ async def create_charge(self, amount, currency, **kwds):

@raises StripeError - Parsed errors from stripe
@raises ParseError - Parsing Charge instance failed
'''
"""
params = {'amount': amount, 'currency': currency, **kwds}
return await self._req('post', '/charges', params=params)

async def retrieve_charge(self, charge_id):
'''
"""
Retrieve a charge

@param charge_id - charge identifier
@return - matching Charge instance

@raises StripeError - Parsed errors from stripe
@raises ParseError - Parsing Charge instance failed
'''
"""
return await self._req('get', '/charges/%s' % (charge_id,))

async def update_charge(self, charge_id, **kwds):
'''
"""
Update a charge.

Keyword arguments can be passed as defined by:
Expand All @@ -279,14 +303,14 @@ async def update_charge(self, charge_id, **kwds):

@raises StripeError - Parsed errors from stripe
@raises ParseError - Parsing Charge instance failed
'''
"""
return await self._req(
'post',
'/charges/%s' % (charge_id,),
params=kwds)

async def capture_charge(self, charge_id, **kwds):
'''
"""
Capture payment of a charge previously created with capture=False.

Keyword arguments can be passed as defined by:
Expand All @@ -297,14 +321,14 @@ async def capture_charge(self, charge_id, **kwds):

@raises StripeError - Parsed errors from stripe
@raises ParseError - Parsing Charge instance failed
'''
"""
return await self._req(
'post',
'/charges/%s/capture' % (charge_id,),
params=kwds)

async def list_charges(self, **kwds):
'''
"""
Return a list of previously created charges matching the given
parameters.

Expand All @@ -315,11 +339,11 @@ async def list_charges(self, **kwds):

@raises StripeError - Parsed errors from stripe
@raises ParseError - Parsing Charge instance failed
'''
"""
return await self._req('get', '/charges', params=kwds)

async def create_customer(self, **kwds):
'''
"""
Create a new customer

Keyword arguments can be passed as defined by:
Expand All @@ -329,23 +353,23 @@ async def create_customer(self, **kwds):

@raises StripeError - Parsed errors from stripe
@raises ParseError - Parsing Charge instance failed
'''
"""
return await self._req('post', '/customers', params=kwds)

async def retrieve_customer(self, customer_id):
'''
"""
Retrieve a customer

@param customer_id - customer identifier
@return - matching Customer instance

@raises StripeError - Parsed errors from stripe
@raises ParseError - Parsing Charge instance failed
'''
"""
return await self._req('get', '/customers/%s' % (customer_id,))

async def update_customer(self, customer_id, **kwds):
'''
"""
Update a customer

Keyword arguments can be passed as defined by:
Expand All @@ -356,24 +380,24 @@ async def update_customer(self, customer_id, **kwds):

@raises StripeError - Parsed errors from stripe
@raises ParseError - Parsing Charge instance failed
'''
"""
return await self._req(
'post',
'/customers/%s' % (customer_id,),
params=kwds)

async def delete_customer(self, customer_id):
'''
"""
Delete a customer

@param customer_id - customer identifier

@raises StripeError - Parsed errors from stripe
'''
"""
await self._req('delete', '/customers/%s' % (customer_id,))

async def list_customers(self, **kwds):
'''
"""
Return a list of previously created charges matching the given
parameters.

Expand All @@ -384,11 +408,11 @@ async def list_customers(self, **kwds):

@raises StripeError - Parsed errors from stripe
@raises ParseError - Parsing Charge instance failed
'''
"""
return await self._req('get', '/customers', params=kwds)

async def create_card(self, customer_id, source, metadata=None):
'''
"""
Create a new credit card for the specified customer

@param customer_id - customer identifier
Expand All @@ -398,7 +422,7 @@ async def create_card(self, customer_id, source, metadata=None):

@raises StripeError - Parsed errors from stripe
@raises ParseError - Parsing Card instance failed
'''
"""
params = {'source': source}
if metadata is not None:
params['metadata'] = metadata
Expand All @@ -409,21 +433,21 @@ async def create_card(self, customer_id, source, metadata=None):
params=params)

async def delete_card(self, customer_id, source_id):
'''
"""
Delete a credit card from the specified customer

@param customer_id - customer identifier
@param source_id - id of card to be deleted

@raises StripeError - Parsed errors from stripe
@raises ParseError - Parsing Card instance failed
'''
"""
await self._req(
'delete',
'/customers/%s/sources/%s' % (customer_id, source_id))

async def update_card(self, customer_id, source_id, **kwds):
'''
"""
Update card details

Keyword arguments can be passed as defined by:
Expand All @@ -435,14 +459,14 @@ async def update_card(self, customer_id, source_id, **kwds):

@raises StripeError - Parsed errors from stripe
@raises ParseError - Parsing Card instance failed
'''
"""
return await self._req(
'post',
'/customers/%s/sources/%s' % (customer_id, source_id),
params=kwds)

async def create_refund(self, charge_id, **kwds):
'''
"""
Refund all or part of a charge.

Keyword arguments can be passed as defined by:
Expand All @@ -453,25 +477,25 @@ async def create_refund(self, charge_id, **kwds):

@raises StripeError - Parsed errors from stripe
@raises ParseError - Parsing Refund instance failed
'''
"""
params = {'charge': charge_id}
params.update(kwds)
return await self._req('post', '/refunds', params)

async def retrieve_refund(self, refund_id):
'''
"""
Retrieve a refund

@param refund_id - refund identifier
@return - matching Refund instance

@raises StripeError - Parsed errors from stripe
@raises ParseError - Parsing Refund instance failed
'''
"""
return await self._req('get', '/refunds/%s' % (refund_id,))

async def update_refund(self, refund_id, metadata):
'''
"""
Update the metadata on a refund. Keys can be removed by setting the
value to None for that key.

Expand All @@ -481,12 +505,12 @@ async def update_refund(self, refund_id, metadata):

@raises StripeError - Parsed errors from stripe
@raises ParseError - Parsing Refund instance failed
'''
"""
params = {'metadata': metadata}
return await self._req('post', '/refunds/%s' % (refund_id,), params)

async def list_refunds(self, **kwds):
'''
"""
Return a list of previously refunds matching the given parameters.

Keyword arguments can be passed as defined by:
Expand All @@ -496,15 +520,44 @@ async def list_refunds(self, **kwds):

@raises StripeError - Parsed errors from stripe
@raises ParseError - Parsing Refund instance failed
'''
"""
return await self._req('get', '/refunds', params=kwds)

async def create_source(self, **kwds):
"""
Create a new source

Keyword arguments can be passed as defined by:
https://stripe.com/docs/api/sources/create?lang=curl

@return - created Source

@raises StripeError - Parsed errors from stripe
@raises ParseError - Parsing Source instance failed
"""

return await self._req('post', '/sources', params=kwds)

async def retrieve_source(self, source_id: str):
"""
Retrieve a source

@param source_id- source identifier
@return - matching Source instance

@raises StripeError - Parsed errors from stripe
@raises ParseError - Parsing Source instance failed
"""

return await self._req('get', '/sources/%s' % (source_id,))


cls_map = {
'charge': Charge,
'customer': Customer,
'card': Card,
'refund': Refund,
'source': Source,
}


Expand Down
6 changes: 3 additions & 3 deletions test/base.py
Expand Up @@ -3,10 +3,10 @@
import sys
import unittest.mock

import aiohttp

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))


def run_until(test, timeout=1.0, loop=None, pre=None):
"""
Run the event loop until:
Expand Down Expand Up @@ -59,11 +59,11 @@ async def test_wrapper():


def mkfuture(ret, return_from=None):
'''
"""
Make a asyncio.Future with the result set to `ret`. If specified,
`return_from` is assumed to be a unittest.mock.Mock object we can
set return_value on.
'''
"""
f = asyncio.Future()
f.set_result(ret)

Expand Down