示例#1
0
    def test_parent(self):
        tree_gen = wac.URIGen('trees', '{tree}')
        apple_gen = wac.URIGen('apples', '{apple}', parent=tree_gen)

        for expected, kwargs in [
            ('/trees/1/apples', dict(tree=1)),
        ]:
            self.assertEqual(apple_gen.collection_uri(**kwargs), expected)

        for expected, kwargs in [
            ('/trees/1/apples/2', dict(tree=1, apple=2)),
        ]:
            self.assertEqual(apple_gen.member_uri(**kwargs), expected)
示例#2
0
class Marketplace(Resource):
    """
    A Marketplace represents your central broker for all operations on the
    Balanced API.

    A Marketplace has a single `owner_customer` which represents your person or
    business.

    All Resources apart from APIKeys are associated with a Marketplace.

    A Marketplace has an escrow account which receives all funds from Debits
    that are not associated with Orders. The sum of the escrow (`in_escrow`) is
    (Debits - Refunds + Reversals - Credits).
    """

    type = 'marketplaces'

    uri_gen = wac.URIGen('/marketplaces', '{marketplace}')

    @utils.classproperty
    def mine(cls):
        """
        Returns an instance representing the marketplace associated with the
        current API key used for this request.
        """
        return cls.query.one()

    my_marketplace = mine
示例#3
0
class Resource(JSONSchemaResource):

    client = config.client

    registry = registry

    uri_gen = wac.URIGen('/resources', '{resource}')

    def unstore(self):
        return self.delete()

    @classmethod
    def fetch(cls, href):
        return cls.get(href)

    @classmethod
    def get(cls, href):
        if href.startswith('/resources'):
            # hackety hack hax
            # resource is an abstract type, we shouldn't have it comeing back itself
            # instead we need to figure out the type based off the api response
            resp = cls.client.get(href)
            resource = [
                k for k in resp.data.keys() if k != 'links' and k != 'meta'
            ]
            if resource:
                return Resource.registry.get(resource[0], cls)(**resp.data)
            return cls(**resp.data)
        return super(Resource, cls).get(href)
示例#4
0
    def test_composite_member(self):
        gen = wac.URIGen('as', '{a}/{b}')

        for expected, kwargs in [
            ('/as/1/ababa', dict(a=1, b='ababa')),
        ]:
            self.assertEqual(gen.member_uri(**kwargs), expected)
示例#5
0
class Dispute(Resource):
    """
    A dispute occurs when a customer disputes a transaction that
    occurred on their funding instrument.
    """
    type = 'disputes'

    uri_gen = wac.URIGen('/disputes', '{dispute}')
示例#6
0
class Callback(Resource):
    """
    A Callback is a publicly accessible location that can receive POSTed JSON
    data whenever an Event is generated.
    """

    type = 'callbacks'

    uri_gen = wac.URIGen('/callbacks', '{callback}')
示例#7
0
class ExternalAccount(FundingInstrument):
    """
    An External Account represents a source of funds provided by an external, 3rd
    party processor. You may Debit funds from the account if can_debit is true.
    """

    type = 'external_accounts'

    uri_gen = wac.URIGen('/external_accounts', '{external_account}')
示例#8
0
class Settlement(Transaction):
    """
    A Settlement is the action of moving money out of an Account to a
    bank account.
    """

    type = 'settlements'

    uri_gen = wac.URIGen('/settlements', '{settlements}')
示例#9
0
    def test_single_member(self):
        gen = wac.URIGen('as', '{a}')

        class Resource(object):
            pass

        for expected, kwargs in [
            ('/as/1', dict(a=1)),
        ]:
            self.assertEqual(gen.member_uri(**kwargs), expected)
示例#10
0
class Reversal(Transaction):
    """
    A Reversal represents a reversal of funds from a Credit. A Credit can have
    many Reversal associated with it up to the total amount of the original
    Credit. Funds are returned to your Marketplace's escrow account
    proportional to the amount of the Reversal.
    """

    type = 'reversals'

    uri_gen = wac.URIGen('/reversals', '{reversal}')
示例#11
0
class Event(Resource):
    """
    An Event is a snapshot of another resource at a point in time when
    something significant occurred. Events are created when resources are
    created, updated, deleted or otherwise change state such as a Credit being
    marked as failed.
    """

    type = 'events'

    uri_gen = wac.URIGen('/events', '{event}')
示例#12
0
class APIKey(Resource):
    """
    Your APIKey is used to authenticate when performing operations on the
    Balanced API. You must create an APIKey before you create a Marketplace.

    **NOTE:** Never give out or expose your APIKey. You may POST to this
    endpoint to create new APIKeys and then DELETE any old keys.
    """
    type = 'api_keys'

    uri_gen = wac.URIGen('/api_keys', '{api_key}')
示例#13
0
class CardHold(Resource):

    type = 'card_holds'

    uri_gen = wac.URIGen('/card_holds', '{card_hold}')

    def cancel(self):
        self.is_void = True
        return self.save()

    def capture(self, **kwargs):
        return Debit(href=self.debits.href, **kwargs).save()
示例#14
0
class Card(FundingInstrument):
    """
    A card represents a source of funds. You may Debit funds from the Card.
    """

    type = 'cards'

    uri_gen = wac.URIGen('/cards', '{card}')

    def hold(self, amount, **kwargs):
        return CardHold(href=self.card_holds.href, amount=amount,
                        **kwargs).save()
示例#15
0
class Account(FundingInstrument):
    """
    An Account is a way to transfer funds from multiple Orders into one place,
    which can later be bulk credited out.
    """

    type = 'accounts'

    uri_gen = wac.URIGen('/accounts', '{account}')

    def settle(self, funding_instrument, **kwargs):
        return Settlement(href=self.settlements.href,
                          funding_instrument=funding_instrument,
                          **kwargs).save()
示例#16
0
class Order(Resource):
    """
    An Order is a logical construct for grouping Transactions.

    An Order may have 0:n Transactions associated with it so long as the sum
    (`amount_escrowed`) which is calculated as
    (Debits - Refunds - Credits + Reversals), is always >= 0.
    """

    type = 'orders'

    uri_gen = wac.URIGen('/orders', '{order}')

    def credit_to(self, destination, amount, **kwargs):
        return destination.credit(order=self.href, amount=amount, **kwargs)

    def debit_from(self, source, amount, **kwargs):
        return source.debit(order=self.href, amount=amount, **kwargs)
示例#17
0
class Customer(Resource):
    """
    A Customer represents a business or person within your Marketplace. A
    Customer can have many funding instruments such as cards and bank accounts
    associated to them. Customers are logical grouping constructs for
    associating many Transactions and FundingInstruments.
    """

    type = 'customers'

    uri_gen = wac.URIGen('/customers', '{customer}')

    def create_order(self, **kwargs):
        return Order(href=self.orders.href, **kwargs).save()

    @property
    def payable_account(self):
        return self.accounts.filter(type="payable").first()
示例#18
0
class BankAccount(FundingInstrument):
    """
    A BankAccount is both a source, and a destination of, funds. You may
    create Debits and Credits to and from, this funding instrument.
    """

    type = 'bank_accounts'

    uri_gen = wac.URIGen('/bank_accounts', '{bank_account}')

    def verify(self):
        """
        Creates a verification of the associated BankAccount so it can
        perform verified operations (debits).

        :rtype: BankAccountVerification
        """
        return BankAccountVerification(
            href=self.bank_account_verifications.href).save()
示例#19
0
class Credit(Transaction):
    """
    A Credit represents a transfer of funds from your Marketplace's
    escrow account to a FundingInstrument.

    Credits are created by calling the `credit` method on a FundingInstrument.
    """

    type = 'credits'

    uri_gen = wac.URIGen('/credits', '{credit}')

    def reverse(self, **kwargs):
        """
        Reverse a Credit.  If no amount is specified it will reverse the entire
        amount of the Credit, you may create many Reversals up to the sum of
        the total amount of the original Credit.

        :rtype: Reversal
        """
        return Reversal(href=self.reversals.href, **kwargs).save()
示例#20
0
class Debit(Transaction):
    """
    A Debit represents a transfer of funds from a FundingInstrument to your
    Marketplace's escrow account.

    A Debit may be created directly, or it will be created as a side-effect
    of capturing a CardHold. If you create a Debit directly it will implicitly
    create the associated CardHold if the FundingInstrument supports this.
    """

    type = 'debits'

    uri_gen = wac.URIGen('/debits', '{debit}')

    def refund(self, **kwargs):
        """
        Refunds this Debit. If no amount is specified it will refund the entire
        amount of the Debit, you may create many Refunds up to the sum total
        of the original Debit's amount.

        :rtype: Refund
        """
        return Refund(href=self.refunds.href, **kwargs).save()
示例#21
0
class Playlist(Resource):

    type = 'playlist'

    uri_gen = wac.URIGen('/v1/playlists', '{playlist}')
示例#22
0
    def test_root(self):
        tree_gen = wac.URIGen('trees', '{tree}')
        self.assertIsNotNone(tree_gen.root_uri)

        apple_spec = wac.URIGen('trees/{tree}/apples', '{apple}')
        self.assertIsNone(apple_spec.root_uri)
示例#23
0
        class Resource4(Resource):

            type = 'four'
            uri_gen = wac.URIGen('/v1/4s', '{fours}')
示例#24
0
class Resource1(Resource):

    type = 'one'
    uri_gen = wac.URIGen('/v2/1s', '{one}')
示例#25
0
class Resource2(Resource):

    type = 'two'
    uri_gen = wac.URIGen('/v2/2s', '{two}')
示例#26
0
class Resource3(Resource):

    type = 'three'
    uri_gen = wac.URIGen('/v2/3s', '{three}')
示例#27
0
class Song(Resource):

    type = 'song'

    uri_gen = wac.URIGen('/v1/songs', '{song}')