diff --git a/examples/voucherify_test.py b/examples/voucherify_test.py index 5082f426..85318bac 100644 --- a/examples/voucherify_test.py +++ b/examples/voucherify_test.py @@ -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": "alice@morgan.com", + "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 diff --git a/voucherify/client.py b/voucherify/client.py index 46868e55..1036d21b 100644 --- a/voucherify/client.py +++ b/voucherify/client.py @@ -1,5 +1,6 @@ import requests import json +import pprint try: from urllib.parse import urlencode, quote @@ -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') @@ -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' @@ -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']