Skip to content

Commit

Permalink
Improved API robustness
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakub Reczko committed Jun 3, 2016
1 parent 9b0160d commit e83379d
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 34 deletions.
151 changes: 121 additions & 30 deletions examples/voucherify_test.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,146 @@
import pprint

from voucherify import Client as voucherifyClient
from voucherify import utils
import pprint

'''
"""
Initialization
'''
"""
voucherify = voucherifyClient(
application_id='c70a6f00-cf91-4756-9df5-47628850002b',
client_secret_key='3266b9f8-e246-4f79-bdf0-833929b1380c'
application_id="c70a6f00-cf91-4756-9df5-47628850002b",
client_secret_key="3266b9f8-e246-4f79-bdf0-833929b1380c"
)

tracking_id = "PythonTestUser"
voucher = {
'code': 'PyThonTeSt',
'discount': {
'type': 'AMOUNT',
'amount_off': 1000
"code": "PythonTestVoucher",
"discount": {
"type": "AMOUNT",
"amount_off": 12436
},
'category': 'PythonTest',
'start_date': '2016-01-01T00:00:00Z',
'active': True
"category": "PythonTestCategory",
"start_date": "2016-01-01T00:00:00Z",
"expiration_date": None,
"redemption": {
"quantity": None,
"redeemed_quantity": 0
},
"active": True
}

'''
"""
Create voucher
'''

pprint.pprint('=== Create voucher ===')
"""
pprint.pprint("=== Create voucher ===")
new_voucher = voucherify.create(voucher)
pprint.pprint(new_voucher)

'''
"""
Get voucher
'''

pprint.pprint('=== Get voucher ===')
voucher = voucherify.get(voucher['code'])
"""
pprint.pprint("=== Get voucher ===")
voucher = voucherify.get(voucher["code"])
pprint.pprint(voucher)

'''
"""
List vouchers from category
'''

pprint.pprint('=== List vouchers from category ===')
vouchers_list = voucherify.list({'category': voucher['category']})
"""
pprint.pprint("=== List vouchers from category ===")
pprint.pprint(voucher)
filter_params = {
"limit": 10,
"skip": 20,
"category": "PythonTest"
}
vouchers_list = voucherify.list(filter_params)
pprint.pprint(vouchers_list)

'''
Utils
'''
"""
Disable Voucher
"""
pprint.pprint("=== Disable Voucher ===")
result = voucherify.disable(voucher["code"])
pprint.pprint(result)

"""
Enable Voucher
"""
pprint.pprint("=== Enable Voucher ===")
result = voucherify.disable(voucher["code"])
pprint.pprint(result)

pprint.pprint('=== Calculate Discount Amount and Price after discount===')
"""
Redeem Voucher
"""
pprint.pprint("=== Redeem Voucher ===")
result = voucherify.redeem(voucher["code"])
pprint.pprint(result)

pprint.pprint("=== Redeem Voucher with Tracking ID ===")
result = voucherify.redeem(voucher["code"], tracking_id)
pprint.pprint(result)

pprint.pprint("=== Redeem Voucher with Customer Info ===")
customer = {
"id": "alice.morgan",
"name": "Alice Morgan",
"email": "[email protected]",
"description": "",
"metadata": {
"locale": "en-GB",
"shoeSize": 5,
"favourite_brands": ["Armani", "L'Autre Chose", "Vicini"]
}
}
payload = {
"voucher": voucher['code'],
"customer": customer
}
result = voucherify.redeem(payload)
pprint.pprint(result)

"""
Redemption Voucher
"""
pprint.pprint("=== Redemption Voucher ===")
redemptions_voucher_list = voucherify.redemption(voucher["code"])
pprint.pprint(redemptions_voucher_list)

"""
Redemptions List
"""
pprint.pprint("=== Redemptions Voucher ===")
filter_params = {
"limit": 1000,
"page": 0,
"start_date": "2015-01-01T00:00:00Z",
"end_date": "2016-12-31T23:59:59Z",
"result": "Success"
}
redemptions_list = voucherify.redemptions(filter_params)
pprint.pprint(len(redemptions_list))

"""
Rollback Voucher
"""
pprint.pprint("=== Rollback Redemption ===")
pprint.pprint(result)
redemption_id = ""
rollback_reason = "Wrong Customer"
result = voucherify.rollback(redemption_id, rollback_reason)
pprint.pprint(result)

"""
Publish Voucher
"""
pprint.pprint("=== Publish Campaign ===")
result = voucherify.publish("PythonTestCampaignName")
pprint.pprint(result)

"""
Utils
"""
pprint.pprint("=== Calculate Discount Amount and Price after discount===")

unit_price = 83.45
items_count = 13
Expand Down
19 changes: 15 additions & 4 deletions voucherify/client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import requests
import json
import pprint

try:
from urllib.parse import urlencode, quote
Expand Down Expand Up @@ -35,8 +36,14 @@ def request(self, path, method='GET', **kwargs):
except requests.HTTPError as e:
response = json.loads(e.read())

if 'json' in response.headers['content-type']:
if response.headers.get('content-type') and 'json' in response.headers['content-type']:
result = response.json()
elif response.status_code == 200:
result = {
"data": response.text,
"reason": response.reason,
"status": response.status_code
}
else:
raise VoucherifyError('Content-Type of API response is not in a JSON format')

Expand Down Expand Up @@ -73,19 +80,23 @@ def create(self, voucher):
def enable(self, code):
path = '/vouchers/' + quote(code) + '/enable'

return self.request(
result = self.request(
path,
method='POST'
)

return result['status'] == 200

def disable(self, code):
path = '/vouchers/' + quote(code) + '/disable'

return self.request(
result = self.request(
path,
method='POST'
)

return result['status'] == 200

def redemption(self, code):
path = '/vouchers/' + quote(code) + '/redemption'

Expand All @@ -104,7 +115,7 @@ def redemptions(self, query):
def redeem(self, code, tracking_id=None):
context = {}

if code and isinstance(code, dict) and code.get('error'):
if code and isinstance(code, dict):
context = code
code = context['voucher']
del context['voucher']
Expand Down

0 comments on commit e83379d

Please sign in to comment.