-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathclient.py
More file actions
82 lines (65 loc) · 2.44 KB
/
client.py
File metadata and controls
82 lines (65 loc) · 2.44 KB
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
81
82
import uuid
from .base import PaymentBaseClient
from .endpoints import PaymentEndpoints
class PaymentClient(PaymentBaseClient):
def get_user_token(self, user_id) -> dict:
"""Create a user session and token for a user.
:param user_id: The _id of the user
:returns: Object
"""
return self.handle_request(
url=self._base_url + PaymentEndpoints.get_user_token,
data={'userId': user_id},
)
def get_authorized_jwt(
self, token: str, expiration_seconds: int = 15 * 60 * 1000
) -> dict:
"""Create a payment authorized JWT for user.
:param user_id: The _id of the user
:param expiration_seconds: Number of seconds until the JWT expires
:returns: Object
"""
return self.handle_request(
url=self._base_url + PaymentEndpoints.get_authorized_jwt,
data={'token': token, 'ttl': expiration_seconds},
)
def verify_authorized_jwt(self, token: str) -> bool:
"""Verify if a token is a payment authorized JWT
:param token: User token
:returns: Boolean (true if payment authorized, false otherwise)
"""
try:
decoded = self.validate_jwt(token)
is_authorized = decoded.get('paymentAuthorized', False)
return is_authorized
except Exception:
return False
def get_summary(self, token: str, app_id: str) -> object:
"""Get a list of a user's subscriptions and consumption for a given app.
:param token: User token
:param app_id: ID of the application
:returns: Object
"""
return self.handle_request(
url=self._base_url + PaymentEndpoints.get_summary,
data={'token': token, 'internalAppId': app_id},
)
def report_usage(
self, token: str, app_id: str, product_id: str, quantity: int
) -> object:
"""Report usage for a given app.
:param token: User token
:param app_id: ID of the application
:param product_id: ID of the product
:returns: Object
"""
return self.handle_request(
url=self._base_url + PaymentEndpoints.report_usage,
data={
'token': token,
'id': str(uuid.uuid4()),
'internalAppId': app_id,
'internalProductId': product_id,
'quantity': quantity,
},
)