Skip to content

Commit

Permalink
Merge pull request #6 from voucherifyio/rspective/features/customer-s…
Browse files Browse the repository at this point in the history
…ervice

SDK Update - Customer Service
  • Loading branch information
pawelrychlik authored Jun 16, 2016
2 parents a8bb70c + 3b3b880 commit 1a1bcf0
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 2 deletions.
97 changes: 96 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ It will create a rollback entry in `redemption.redemption_entries` and give 1 re
Example:

```python
result = voucherify.rollback("r_irOQWUTAjthQwnkn5JQM1V6N", "alice.morgan")
result = voucherify.rollback("r_irOQWUTAjthQwnkn5JQM1V6N", "john.doe")
```

Result:
Expand Down Expand Up @@ -591,6 +591,101 @@ Possible errors are:
- 400 - Already rolled back - if redemption with given `redemption_id` has been rolled back already
- 400 - Invalid redemption id - when trying to rollback a rollback.


#### Create customer

Example:

```python
payload = {
"name": "John Doe",
"email": "[email protected]",
"description": "Sample description of customer",
"metadata": {
"lang": "en"
}
}

result = voucherify.customer.create(payload)
```

Result:
```json
{
"id": "cust_WGG615E92dhOHz7PV9Vo9gk9",
"created_at": "2016-06-12T15:52:49Z",
"description": "Sample description of customer",
"email": "[email protected]",
"metadata": {
"lang": "en"
},
"name": "John Doe",
"object": "customer"
}
```

#### Fetch customer

Example:

```python
result = voucherify.customer.fetch("cust_gVYAaioitMz3GO6HSKFLf7Or")
```

Result:
```json
{
"id": "cust_gVYAaioitMz3GO6HSKFLf7Or",
"created_at": "2016-06-12T16:03:36Z",
"description": "Sample description of customer",
"email": "[email protected]",
"metadata": {
"lang": "en"
},
"name": "John Doe",
"object": "customer"
}
```

#### Update customer

Example:

```python
payload = {
"id": "cust_gVYAaioitMz3GO6HSKFLf7Or",
"description": "Updated description for given customer ID"
}

result = voucherify.customer.update(payload)
```

Result:
```json
{
"id": "cust_gVYAaioitMz3GO6HSKFLf7Or",
"created_at": "2016-06-12T16:03:36Z",
"description": "Updated description for given customer ID",
"email": "[email protected]",
"metadata": {
"lang": "en"
},
"name": "John Doe",
"object": "customer"
}
```

#### Delete customer

Example:

```python
result = voucherify.customer.delete("cust_gVYAaioitMz3GO6HSKFLf7Or")
```

Result:
`Result is an empty body`

### Utils

Use our set of utils to calculate a price after discount or discount amount.
Expand Down
51 changes: 51 additions & 0 deletions examples/customer_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import pprint

from voucherify import Client as voucherifyClient

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

"""
Create Customer
"""
payload = {
"name": "John Doe",
"email": "[email protected]",
"description": "Sample description of customer",
"metadata": {
"lang": "en"
}
}

result = voucherify.customer.create(payload)
pprint.pprint("--- Create ---")
pprint.pprint(result)

"""
Fetch Customer
"""
result = voucherify.customer.fetch(result.get("id"))
pprint.pprint("--- Fetch ---")
pprint.pprint(result)

"""
Update Customer
"""
payload = result
payload['description'] = "Sample description of customer with changes"

result = voucherify.customer.update(payload)
pprint.pprint("--- Update ---")
pprint.pprint(result)

"""
Delete Customer
"""
result = voucherify.customer.delete(result["id"])
pprint.pprint("--- Delete ---")
pprint.pprint(result)
46 changes: 45 additions & 1 deletion voucherify/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
TIMEOUT = 30 * 1000


class Client(object):
class VoucherifyRequest(object):
def __init__(self, application_id, client_secret_key):
self.timeout = TIMEOUT
self.headers = {
Expand Down Expand Up @@ -47,6 +47,50 @@ def request(self, path, method='GET', **kwargs):

return result


class Customer(VoucherifyRequest):
def __init__(self, *args, **kwargs):
super(Customer, self).__init__(*args, **kwargs)

def create(self, customer):
path = '/customers/'

return self.request(
path,
data=json.dumps(customer),
method='POST'
)

def fetch(self, customer_id):
path = '/customers/' + quote(customer_id)

return self.request(
path
)

def update(self, customer):
path = '/customers/' + quote(customer.get('id'))

return self.request(
path,
data=json.dumps(customer),
method='PUT'
)

def delete(self, customer_id):
path = '/customers/' + quote(customer_id)

return self.request(
path,
method='DELETE'
)


class Client(VoucherifyRequest):
def __init__(self, *args, **kwargs):
super(Client, self).__init__(*args, **kwargs)
self.customer = Customer(*args, **kwargs)

def list(self, query):
path = '/vouchers/'

Expand Down

0 comments on commit 1a1bcf0

Please sign in to comment.