Skip to content
This repository has been archived by the owner on Jan 25, 2018. It is now read-only.

Commit

Permalink
Merge pull request #15 from andymckay/812511
Browse files Browse the repository at this point in the history
pull in price tiers, partial, add in api (bug 812511)
  • Loading branch information
Andy McKay committed Dec 18, 2012
2 parents f33aee9 + 4770376 commit 123ecb8
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 26 deletions.
Empty file added lib/marketplace/__init__.py
Empty file.
13 changes: 13 additions & 0 deletions lib/marketplace/api.py
@@ -0,0 +1,13 @@
from django.conf import settings

from ..utils import SlumberWrapper


class MarketplaceAPI(SlumberWrapper):
errors = {}

def get_price(self, tier):
return self.slumber.api.webpay.prices(id=tier).get()


client = MarketplaceAPI(settings.MARKETPLACE_URL or 'http://example.com')
24 changes: 24 additions & 0 deletions lib/marketplace/tests.py
@@ -0,0 +1,24 @@
from django.test import TestCase

import mock
from nose.tools import eq_

from lib.marketplace.api import client


sample_price = {
u'name': u'Tier 0',
u'prices': [{u'amount': u'1.00', u'currency': u'USD'},
{u'amount': u'3.00', u'currency': u'JPY'}],
u'resource_uri': u'/api/webpay/prices/1/'
}

@mock.patch('lib.marketplace.api.client.slumber')
class SolitudeAPITest(TestCase):

def test_get_prices(self, slumber):
sample = mock.Mock()
sample.get.return_value = sample_price
slumber.api.webpay.prices.return_value = sample
prices = client.get_price(1)
eq_(prices['name'], sample_price['name'])
31 changes: 5 additions & 26 deletions lib/solitude/api.py
@@ -1,11 +1,9 @@
import json
import logging

from django.conf import settings

from slumber import API
from slumber.exceptions import HttpClientError

from ..utils import SlumberWrapper
from .errors import ERROR_STRINGS


Expand All @@ -17,14 +15,12 @@ class SellerNotConfigured(Exception):
"""The seller has not yet been configued for the payment."""


class SolitudeAPI(object):
class SolitudeAPI(SlumberWrapper):
"""A solitude API client.
:param url: URL of the solitude endpoint.
"""

def __init__(self, url):
self.slumber = API(url)
errors = ERROR_STRINGS

def _buyer_from_response(self, res):
buyer = {}
Expand All @@ -40,23 +36,6 @@ def _buyer_from_response(self, res):
buyer['uuid'] = res['uuid']
return buyer

def parse_res(self, res):
if res == '':
return {}
if isinstance(res, (str, unicode)):
return json.loads(res)
return res

def safe_run(self, command, *args, **kwargs):
try:
res = command(*args, **kwargs)
except HttpClientError as e:
res = self.parse_res(e.response.content)
for key, value in res.iteritems():
res[key] = [ERROR_STRINGS[v] for v in value]
return {'errors': res}
return res

def buyer_has_pin(self, uuid):
"""Returns True if the existing buyer has a PIN.
Expand Down Expand Up @@ -180,7 +159,8 @@ def configure_product_for_billing(self, webpay_trans_id,
% (webpay_trans_id, bill_id))
return bill_id

def create_product(self, external_id, product_name, currency, amount, seller):
def create_product(self, external_id, product_name, currency, amount,
seller):
"""
Creates a product and a Bango ID on the fly in solitude.
"""
Expand Down Expand Up @@ -213,7 +193,6 @@ def create_product(self, external_id, product_name, currency, amount, seller):
return bango['resource_uri']



if getattr(settings, 'SOLITUDE_URL', False):
client = SolitudeAPI(settings.SOLITUDE_URL)
else:
Expand Down
30 changes: 30 additions & 0 deletions lib/utils.py
@@ -0,0 +1,30 @@
import json

from slumber import API
from slumber.exceptions import HttpClientError


class SlumberWrapper(object):
"""
A wrapper around the Slumber API.
"""

def __init__(self, url):
self.slumber = API(url)

def parse_res(self, res):
if res == '':
return {}
if isinstance(res, (str, unicode)):
return json.loads(res)
return res

def safe_run(self, command, *args, **kwargs):
try:
res = command(*args, **kwargs)
except HttpClientError as e:
res = self.parse_res(e.response.content)
for key, value in res.iteritems():
res[key] = [self.errors[v] for v in value]
return {'errors': res}
return res
3 changes: 3 additions & 0 deletions webpay/settings/base.py
Expand Up @@ -200,3 +200,6 @@
# This is the URL for the bango payment screen.
# It will receive one string substitution: the billing configuration ID.
BANGO_PAY_URL = 'http://mozilla.test.bango.org/mozpayments/?bcid=%s'

# This is the URL to the marketplace.
MARKETPLACE_URL = None

0 comments on commit 123ecb8

Please sign in to comment.