Skip to content

Commit

Permalink
add better exception handler
Browse files Browse the repository at this point in the history
  • Loading branch information
tarek-berkane committed Feb 16, 2024
1 parent e3f5285 commit 262f393
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 58 deletions.
Binary file modified dev_requirements.txt
Binary file not shown.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "[email protected]" }]
description = "This Plugin is to integrate ePayment gateway with Chargily V2 easily."
readme = "README.md"
Expand Down
143 changes: 87 additions & 56 deletions src/chargily_pay/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -56,39 +71,41 @@ 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(
urljoin(self.url, f"customers?page={page}"),
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)
Expand All @@ -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)
Expand All @@ -110,50 +128,54 @@ 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(
urljoin(self.url, f"products?page={page}"),
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(
urljoin(self.url, f"products/{id}/prices?page={page}"),
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)
Expand All @@ -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)
Expand All @@ -173,29 +196,32 @@ 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(
urljoin(self.url, f"prices?page={page}"),
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)
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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)
Expand All @@ -256,28 +285,31 @@ 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(
urljoin(self.url, f"payment-links/{id}"),
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(
Expand All @@ -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(
Expand All @@ -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
Expand Down
Loading

0 comments on commit 262f393

Please sign in to comment.