mirror of
https://github.com/simple-login/app.git
synced 2024-11-13 07:31:12 +01:00
e43a2dd34d
* Have subscription callback whenever a subscription changes * Fixed tests --------- Co-authored-by: Adrià Casajús <adria.casajus@proton.ch>
33 lines
1 KiB
Python
33 lines
1 KiB
Python
import requests
|
|
from requests import RequestException
|
|
|
|
from app import config
|
|
from app.log import LOG
|
|
from app.models import User
|
|
|
|
|
|
def execute_subscription_webhook(user: User):
|
|
webhook_url = config.SUBSCRIPTION_CHANGE_WEBHOOK
|
|
if webhook_url is None:
|
|
return
|
|
subscription_end = user.get_active_subscription_end(
|
|
include_partner_subscription=False
|
|
)
|
|
sl_subscription_end = None
|
|
if subscription_end:
|
|
sl_subscription_end = subscription_end.timestamp
|
|
payload = {
|
|
"user_id": user.id,
|
|
"is_premium": user.is_premium(),
|
|
"active_subscription_end": sl_subscription_end,
|
|
}
|
|
try:
|
|
response = requests.post(webhook_url, json=payload)
|
|
if response.status_code == 200:
|
|
LOG.i("Sent request to subscription update webhook successfully")
|
|
else:
|
|
LOG.i(
|
|
f"Request to webhook failed with statue {response.status_code}: {response.text}"
|
|
)
|
|
except RequestException as e:
|
|
LOG.warn(f"Subscription request exception: {e}")
|