From a077fc8b36fca982d8243a95bb16ce2a2e5ce17f Mon Sep 17 00:00:00 2001 From: Jakub Reczko Date: Sun, 12 Jun 2016 17:43:32 +0200 Subject: [PATCH 1/5] Extracted request wrapper to separated class --- voucherify/client.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/voucherify/client.py b/voucherify/client.py index 4b9e7c5f..4960c181 100644 --- a/voucherify/client.py +++ b/voucherify/client.py @@ -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 = { @@ -47,6 +47,11 @@ def request(self, path, method='GET', **kwargs): return result + +class Client(VoucherifyRequest): + def __init__(self, *args, **kwargs): + super(Client, self).__init__(*args, **kwargs) + def list(self, query): path = '/vouchers/' From d6f0639de1c43eafd8584b6d5558967442bc0f17 Mon Sep 17 00:00:00 2001 From: Jakub Reczko Date: Sun, 12 Jun 2016 17:51:03 +0200 Subject: [PATCH 2/5] Added Customer namespace --- voucherify/client.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/voucherify/client.py b/voucherify/client.py index 4960c181..8259875a 100644 --- a/voucherify/client.py +++ b/voucherify/client.py @@ -48,9 +48,48 @@ 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/' + + 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/' From 6a6a27e07ba5a486dff58bbe5c4d8ef103b75d69 Mon Sep 17 00:00:00 2001 From: Jakub Reczko Date: Sun, 12 Jun 2016 18:01:07 +0200 Subject: [PATCH 3/5] Fixed missing parameter in endpoint URL --- voucherify/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/voucherify/client.py b/voucherify/client.py index 8259875a..8b869593 100644 --- a/voucherify/client.py +++ b/voucherify/client.py @@ -69,7 +69,7 @@ def fetch(self, customer_id): ) def update(self, customer): - path = '/customers/' + path = '/customers/' + quote(customer.get('id')) return self.request( path, From 3a574a3f0dbc1c8ca2367e2ff902e381e4abbc2a Mon Sep 17 00:00:00 2001 From: Jakub Reczko Date: Sun, 12 Jun 2016 18:03:27 +0200 Subject: [PATCH 4/5] Added examples of using Customers SDK methods --- examples/customer_test.py | 51 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 examples/customer_test.py diff --git a/examples/customer_test.py b/examples/customer_test.py new file mode 100644 index 00000000..4aff9add --- /dev/null +++ b/examples/customer_test.py @@ -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": "john@email.com", + "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) From 3b3b88052343fa98eafbc08bb2885223e011fdc8 Mon Sep 17 00:00:00 2001 From: Jakub Reczko Date: Sun, 12 Jun 2016 18:14:17 +0200 Subject: [PATCH 5/5] Updated Readme --- README.md | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 96 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 925ed317..8ad2cfbf 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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": "john@email.com", + "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": "john@email.com", + "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": "john@email.com", + "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": "john@email.com", + "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.