|
| 1 | +from typing import Union, List, Optional |
1 | 2 | from pubnub import utils |
2 | 3 | from pubnub.endpoints.endpoint import Endpoint |
3 | 4 | from pubnub.errors import PNERR_TTL_MISSING, PNERR_INVALID_META, PNERR_RESOURCES_MISSING |
4 | 5 | from pubnub.exceptions import PubNubException |
5 | 6 | from pubnub.enums import HttpMethod, PNOperationType |
| 7 | +from pubnub.models.consumer.common import PNStatus |
6 | 8 | from pubnub.models.consumer.v3.access_manager import PNGrantTokenResult |
| 9 | +from pubnub.structures import Envelope |
| 10 | + |
| 11 | + |
| 12 | +class PNGrantTokenResultEnvelope(Envelope): |
| 13 | + result: PNGrantTokenResult |
| 14 | + status: PNStatus |
7 | 15 |
|
8 | 16 |
|
9 | 17 | class GrantToken(Endpoint): |
10 | 18 | GRANT_TOKEN_PATH = "/v3/pam/%s/grant" |
11 | 19 |
|
12 | | - def __init__(self, pubnub): |
| 20 | + def __init__(self, pubnub, channels: Union[str, List[str]] = None, channel_groups: Union[str, List[str]] = None, |
| 21 | + users: Union[str, List[str]] = None, spaces: Union[str, List[str]] = None, |
| 22 | + authorized_user_id: str = None, ttl: Optional[int] = None, meta: Optional[any] = None): |
13 | 23 | Endpoint.__init__(self, pubnub) |
14 | | - self._ttl = None |
15 | | - self._meta = None |
16 | | - self._authorized_uuid = None |
| 24 | + self._ttl = ttl |
| 25 | + self._meta = meta |
| 26 | + self._authorized_uuid = authorized_user_id |
17 | 27 | self._channels = [] |
| 28 | + if channels: |
| 29 | + utils.extend_list(self._channels, channels) |
| 30 | + if spaces: |
| 31 | + utils.extend_list(self._channels, spaces) |
| 32 | + |
18 | 33 | self._groups = [] |
| 34 | + if channel_groups: |
| 35 | + utils.extend_list(self._groups, channel_groups) |
19 | 36 | self._uuids = [] |
| 37 | + if users: |
| 38 | + utils.extend_list(self._uuids, users) |
20 | 39 |
|
21 | 40 | self._sort_params = True |
22 | 41 |
|
23 | | - def ttl(self, ttl): |
| 42 | + def ttl(self, ttl: int) -> 'GrantToken': |
24 | 43 | self._ttl = ttl |
25 | 44 | return self |
26 | 45 |
|
27 | | - def meta(self, meta): |
| 46 | + def meta(self, meta: any) -> 'GrantToken': |
28 | 47 | self._meta = meta |
29 | 48 | return self |
30 | 49 |
|
31 | | - def authorized_uuid(self, uuid): |
| 50 | + def authorized_uuid(self, uuid: str) -> 'GrantToken': |
32 | 51 | self._authorized_uuid = uuid |
33 | 52 | return self |
34 | 53 |
|
35 | | - def authorized_user(self, user): |
| 54 | + def authorized_user(self, user) -> 'GrantToken': |
36 | 55 | self._authorized_uuid = user |
37 | 56 | return self |
38 | 57 |
|
39 | | - def spaces(self, spaces): |
| 58 | + def spaces(self, spaces: Union[str, List[str]]) -> 'GrantToken': |
40 | 59 | self._channels = spaces |
41 | 60 | return self |
42 | 61 |
|
43 | | - def users(self, users): |
| 62 | + def users(self, users: Union[str, List[str]]) -> 'GrantToken': |
44 | 63 | self._uuids = users |
45 | 64 | return self |
46 | 65 |
|
47 | | - def channels(self, channels): |
| 66 | + def channels(self, channels: Union[str, List[str]]) -> 'GrantToken': |
48 | 67 | self._channels = channels |
49 | 68 | return self |
50 | 69 |
|
51 | | - def groups(self, groups): |
| 70 | + def groups(self, groups: Union[str, List[str]]) -> 'GrantToken': |
52 | 71 | self._groups = groups |
53 | 72 | return self |
54 | 73 |
|
55 | | - def uuids(self, uuids): |
| 74 | + def uuids(self, uuids: Union[str, List[str]]) -> 'GrantToken': |
56 | 75 | self._uuids = uuids |
57 | 76 | return self |
58 | 77 |
|
@@ -102,9 +121,12 @@ def validate_params(self): |
102 | 121 | self.validate_ttl() |
103 | 122 | self.validate_resources() |
104 | 123 |
|
105 | | - def create_response(self, envelope): |
| 124 | + def create_response(self, envelope) -> PNGrantTokenResult: |
106 | 125 | return PNGrantTokenResult.from_json(envelope['data']) |
107 | 126 |
|
| 127 | + def sync(self) -> PNGrantTokenResultEnvelope: |
| 128 | + return PNGrantTokenResultEnvelope(super().sync()) |
| 129 | + |
108 | 130 | def is_auth_required(self): |
109 | 131 | return False |
110 | 132 |
|
|
0 commit comments