forked from Shopify/sample-django-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
partnerjam.py
80 lines (65 loc) · 1.97 KB
/
partnerjam.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from __future__ import annotations
from decimal import Decimal
from typing import cast
from urllib.parse import urljoin
import requests
from django.conf import settings
class PartnerJamClient:
_domain = 'https://be-app.partnerjam.com/'
_webhook_path = "webhooks/installation-confirm/"
_discount_path = "api/v1/discount-check/"
@classmethod
def _is_active(cls) -> bool:
return bool(cls._domain)
@classmethod
def send_webhook(
cls,
token: str,
shopify_id: int,
shop_name: str,
myshopify_domain: str,
secret: str,
test: bool,
) -> None:
if not cls._is_active():
return
if not token:
return
url = urljoin(cast(str, cls._domain), cls._webhook_path)
response = requests.post(
url,
json={
"token": token,
"shopify_id": shopify_id,
"shop_name": shop_name,
"myshopify_domain": myshopify_domain,
"secret": secret,
"test": test,
},
timeout=5,
)
response.raise_for_status()
@classmethod
def get_discount(cls, token: str) -> Decimal | None:
if not cls._is_active():
return None
if not token:
return None
url = urljoin(cast(str, cls._domain), cls._discount_path)
response = requests.get(
url,
params={
"token": token,
},
timeout=3,
)
response.raise_for_status()
response_json = response.json()
discount_value = response_json.get("discount")
if discount_value is not None:
discount = Decimal(discount_value)
if discount < Decimal(0) or discount > Decimal(100):
raise ValueError("Unexpected value. Got {response_json} as the response")
return discount
else:
return None