From 262f3934f0b5f4c9150fb96caef683b1318ac05f Mon Sep 17 00:00:00 2001 From: tarek-berkane Date: Fri, 16 Feb 2024 11:18:36 +0100 Subject: [PATCH] add better exception handler --- dev_requirements.txt | Bin 512 -> 592 bytes pyproject.toml | 2 +- src/chargily_pay/api.py | 143 ++++++++++++++++++++++++---------------- tests/test_api.py | 15 ++++- 4 files changed, 102 insertions(+), 58 deletions(-) diff --git a/dev_requirements.txt b/dev_requirements.txt index c9eb76a6c42e9842baa4fee351b5171e6ee99243..9cf12323550db5a2dee96371fcf5a59af41a7d4c 100644 GIT binary patch literal 592 zcmX|8TW;ha5d7yZMm(HJ^1&S<NQaMZZ&4P=zmA`S@*3w+#4=DNmvw^8+jub97r1Wbw{r!h{5g@FZri#nxQw~=^NuCTdQ20<^<`PkP+DE+$Vj8b$2o13DwGY1 zZS1pfeo&}?=A4)2c!erwrMGoGwOUC1B zz1Dm)O*AFu8tF}?4!WrW{~!!6Knu_bOykb9O3b6SaPKHOjTO{zyO66YJ^4z5 zV>XYWSwE=7S)()VOQzcB$gQE~{g?mRZ-8?E>l|HC$>vxjH-?%%@H%_`#tnM>Q|ShB s#aXNsd)aLBv>n?_eCHmAoqMX2r={L~g1o_=32NWL4t;z9+ghIT2exiZGynhq diff --git a/pyproject.toml b/pyproject.toml index ca62d00..3038e8b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "chargily_pay" -version = "2.0.1" +version = "2.1.0" authors = [{ name = "Berkane Tarek", email = "tarekg320@gmail.com" }] description = "This Plugin is to integrate ePayment gateway with Chargily V2 easily." readme = "README.md" diff --git a/src/chargily_pay/api.py b/src/chargily_pay/api.py index 6e60962..d87ae71 100644 --- a/src/chargily_pay/api.py +++ b/src/chargily_pay/api.py @@ -6,7 +6,7 @@ from requests.compat import urljoin from .entity import Checkout, Customer, PaymentLink, Price, Product -from .settings import * +from .settings import CHARGILIY_URL # drop None values @@ -15,6 +15,21 @@ asdict_true_value = lambda x: asdict(x, dict_factory=exclude_none_value) +def response_or_exception(fn): + from functools import wraps + + @wraps(fn) + def wrapper(*args, **kwargs): + response: requests.Response = fn(*args, **kwargs) + if response.status_code == 422: + raise requests.exceptions.HTTPError(response, response=response) + response.raise_for_status() + + return response.json() + + return wrapper + + class ChargilyClient: def __init__(self, key, secret, url=CHARGILIY_URL): self.key = key @@ -32,22 +47,22 @@ def __init__(self, key, secret, url=CHARGILIY_URL): def get_balance(self): """Get your balance""" response = requests.get(urljoin(self.url, "balance"), headers=self.headers) - response.raise_for_status() + return response.json() # ================================== # Customers # ================================== - - def create_customer(self, customer: Customer): + @response_or_exception + def create_customer(self, customer: Customer, *args, **kwargs): """Create a customer""" customer_dict = asdict_true_value(customer) response = requests.post( urljoin(self.url, "customers"), headers=self.headers, json=customer_dict ) - response.raise_for_status() - return response.json() + return response + @response_or_exception def update_customer(self, id, customer: Customer): """Update a customer""" customer_dict = asdict_true_value(customer) @@ -56,17 +71,18 @@ def update_customer(self, id, customer: Customer): headers=self.headers, json=customer_dict, ) - response.raise_for_status() - return response.json() + return response + + @response_or_exception def retrieve_customer(self, id): """Retrieve a customer""" response = requests.get( urljoin(self.url, f"customers/{id}"), headers=self.headers ) - response.raise_for_status() - return response.json() + return response + @response_or_exception def list_customers(self, per_page: int = 10, page: int = 1): """List customers""" response = requests.get( @@ -74,21 +90,22 @@ def list_customers(self, per_page: int = 10, page: int = 1): headers=self.headers, params={"per_page": per_page}, ) - response.raise_for_status() - return response.json() + return response + + @response_or_exception def delete_customer(self, id): """Delete a customer""" response = requests.delete( urljoin(self.url, f"customers/{id}"), headers=self.headers ) - response.raise_for_status() - return response.json() + + return response # ================================== # Products # ================================== - + @response_or_exception def create_product(self, product: Product): """Create a product""" product_dict = asdict_true_value(product) @@ -98,9 +115,10 @@ def create_product(self, product: Product): headers=self.headers, json=product_dict, ) - response.raise_for_status() - return response.json() + return response + + @response_or_exception def update_product(self, id, product: Product): """Update a product""" product_dict = asdict_true_value(product) @@ -110,17 +128,19 @@ def update_product(self, id, product: Product): headers=self.headers, json=product_dict, ) - response.raise_for_status() - return response.json() + return response + + @response_or_exception def retrieve_product(self, id): """Retrieve a product""" response = requests.get( urljoin(self.url, f"products/{id}"), headers=self.headers ) - response.raise_for_status() - return response.json() + return response + + @response_or_exception def list_products(self, per_page: int = 10, page: int = 1): """List products""" response = requests.get( @@ -128,18 +148,19 @@ def list_products(self, per_page: int = 10, page: int = 1): headers=self.headers, params={"per_page": per_page}, ) - response.raise_for_status() - return response.json() + + return response def delete_product(self, id): """Delete a product""" response = requests.delete( urljoin(self.url, f"products/{id}"), headers=self.headers ) - response.raise_for_status() - return response.json() + + return response # todo: retrieve product prices + @response_or_exception def retrieve_product_prices(self, id, per_page: int = 10, page: int = 1): """Retrieve product prices""" response = requests.get( @@ -147,13 +168,14 @@ def retrieve_product_prices(self, id, per_page: int = 10, page: int = 1): headers=self.headers, params={"per_page": per_page}, ) - response.raise_for_status() - return response.json() + + return response # ================================== # Prices # ================================== + @response_or_exception def create_price(self, price: Price): """Create a price""" price_dict = asdict_true_value(price) @@ -162,9 +184,10 @@ def create_price(self, price: Price): headers=self.headers, json=price_dict, ) - response.raise_for_status() - return response.json() + return response + + @response_or_exception def update_price(self, id, price: Price): """Update a price""" price_dict = asdict_true_value(price) @@ -173,15 +196,17 @@ def update_price(self, id, price: Price): headers=self.headers, json=price_dict, ) - response.raise_for_status() - return response.json() + return response + + @response_or_exception def retrieve_price(self, id): """Retrieve a price""" response = requests.get(urljoin(self.url, f"prices/{id}"), headers=self.headers) - response.raise_for_status() - return response.json() + return response + + @response_or_exception def list_prices(self, per_page: int = 10, page: int = 1): """List prices""" response = requests.get( @@ -189,13 +214,14 @@ def list_prices(self, per_page: int = 10, page: int = 1): headers=self.headers, params={"per_page": per_page}, ) - response.raise_for_status() - return response.json() + + return response # ================================== # Checkouts # ================================== + @response_or_exception def create_checkout(self, checkout: Checkout): """Create a checkout""" checkout_dict = asdict_true_value(checkout) @@ -204,17 +230,19 @@ def create_checkout(self, checkout: Checkout): headers=self.headers, json=checkout_dict, ) - response.raise_for_status() - return response.json() + return response + + @response_or_exception def retrieve_checkout(self, id): """Retrieve a checkout""" response = requests.get( urljoin(self.url, f"checkouts/{id}"), headers=self.headers ) - response.raise_for_status() - return response.json() + return response + + @response_or_exception def list_checkouts(self, per_page: int = 10, page: int = 1): """List checkouts""" response = requests.get( @@ -223,9 +251,9 @@ def list_checkouts(self, per_page: int = 10, page: int = 1): params={"per_page": per_page}, ) - response.raise_for_status() - return response.json() + return response + @response_or_exception def retrieve_checkout_items(self, id, per_page: int = 10, page: int = 1): """List checkouts items""" response = requests.get( @@ -234,20 +262,21 @@ def retrieve_checkout_items(self, id, per_page: int = 10, page: int = 1): params={"per_page": per_page}, ) - response.raise_for_status() - return response.json() + return response + @response_or_exception def expire_checkout(self, id): """Expire a checkout""" response = requests.post( urljoin(self.url, f"checkouts/{id}/expire"), headers=self.headers ) - response.raise_for_status() - return response.json() + + return response # ================================== # Payment Links # ================================== + @response_or_exception def create_payment_link(self, payment_link: PaymentLink): """Create a payment link""" payment_link_dict = asdict_true_value(payment_link) @@ -256,10 +285,11 @@ def create_payment_link(self, payment_link: PaymentLink): headers=self.headers, json=payment_link_dict, ) - response.raise_for_status() - return response.json() - def updata_payment_link(self, id, payment_link: PaymentLink): + return response + + @response_or_exception + def update_payment_link(self, id, payment_link: PaymentLink): """Update a payment link""" payment_link_dict = asdict_true_value(payment_link) response = requests.post( @@ -267,17 +297,19 @@ def updata_payment_link(self, id, payment_link: PaymentLink): headers=self.headers, json=payment_link_dict, ) - response.raise_for_status() - return response.json() + return response + + @response_or_exception def retrieve_payment_link(self, id): """Retrieve a payment link""" response = requests.get( urljoin(self.url, f"payment-links/{id}"), headers=self.headers ) - response.raise_for_status() - return response.json() + return response + + @response_or_exception def list_payment_links(self, per_page: int = 10, page: int = 1): """List payment links""" response = requests.get( @@ -286,9 +318,9 @@ def list_payment_links(self, per_page: int = 10, page: int = 1): params={"per_page": per_page}, ) - response.raise_for_status() - return response.json() + return response + @response_or_exception def retrieve_payment_link_items(self, id, per_page: int = 10, page: int = 1): """List payment link items""" response = requests.get( @@ -297,8 +329,7 @@ def retrieve_payment_link_items(self, id, per_page: int = 10, page: int = 1): params={"per_page": per_page}, ) - response.raise_for_status() - return response.json() + return response # ================================== # Utils diff --git a/tests/test_api.py b/tests/test_api.py index 2690d74..eb0e050 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -37,6 +37,19 @@ def test_create_customer(self): response = self.chargily.create_customer(customer) self.assertEqual(type(response), dict) + def test_create_customer_with_exception(self): + customer = Customer( + name="Username", + email="example@gmail.com", + address=Address( + address="Address", state="State", country="wrong country code" + ), + ) + try: + response = self.chargily.create_customer(customer) + except requests.exceptions.HTTPError as e: + self.assertEqual(type(e.response.json()), dict) + def test_create_customer_with_phone(self): customer = Customer( name="Username", @@ -452,7 +465,7 @@ def test_update_payment_link(self): self.assertEqual(response["name"], "Payment link name") try: payment_link.name = "Payment link name 2" - response = self.chargily.updata_payment_link(payment_link_id, payment_link) + response = self.chargily.update_payment_link(payment_link_id, payment_link) self.assertEqual(response["name"], "Payment link name 2") raise Exception("Should fail") except requests.exceptions.HTTPError as err: