Skip to content

Commit

Permalink
Fixed issuses reported in CR
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakub Reczko committed Jun 5, 2016
1 parent 5e0fccf commit 1c3c2c4
Showing 3 changed files with 51 additions and 19 deletions.
39 changes: 35 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -189,8 +189,23 @@ result = voucherify.disable("JxiJaV")

Result:

```text
True|None
**This method does not return result when succeed**

Error:

```json
{
"code": 404,
"message": "Resource not found",
"details": "Cannot find Voucher with code: <voucher_code>"
}
```

```json
{
"statusCode": 400,
"error": "Bad Request"
}
```

#### Enabling a voucher
@@ -204,8 +219,24 @@ result = voucherify.enable("JxiJaV")
```

Result:
```text
True|None

**This method does not return result when succeed**

Error:

```json
{
"code": 404,
"message": "Resource not found",
"details": "Cannot find Voucher with code: <voucher_code>"
}
```

```json
{
"statusCode": 400,
"error": "Bad Request"
}
```

#### Getting voucher redemption
8 changes: 8 additions & 0 deletions examples/voucherify_test.py
Original file line number Diff line number Diff line change
@@ -62,13 +62,21 @@
result = voucherify.disable(voucher["code"])
pprint.pprint(result)

pprint.pprint("=== Disable not existing Voucher ===")
result = voucherify.disable("TotallyRandomVoucher")
pprint.pprint(result)

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

pprint.pprint("=== Disable not existing Voucher ===")
result = voucherify.enable("TotallyRandomVoucher")
pprint.pprint(result)

"""
Redeem Voucher
"""
23 changes: 8 additions & 15 deletions voucherify/client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pprint
import requests
import json

@@ -22,6 +23,8 @@ def __init__(self, application_id, client_secret_key):
}

def request(self, path, method='GET', **kwargs):
result = ""

try:
url = ENDPOINT_URL + path

@@ -32,21 +35,15 @@ def request(self, path, method='GET', **kwargs):
timeout=self.timeout,
**kwargs
)
except requests.HTTPError as e:
except requests.ConnectionError or requests.HTTPError as e:
response = json.loads(e.read())

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')
result = response.text

if result and isinstance(result, dict) and result.get('error'):
if isinstance(result, dict) and result.get('error'):
raise VoucherifyError(result)

return result
@@ -79,23 +76,19 @@ def create(self, voucher):
def enable(self, code):
path = '/vouchers/' + quote(code) + '/enable'

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

return result['status'] == 200

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

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

return result['status'] == 200

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

0 comments on commit 1c3c2c4

Please sign in to comment.