Skip to content

Commit 696883e

Browse files
authored
Merge pull request #586 from ably/579/fix-capabilities-string
Allow a string value to be passed to `Capability` constructor
2 parents 72f1946 + 9213854 commit 696883e

File tree

4 files changed

+30
-13
lines changed

4 files changed

+30
-13
lines changed

ably/rest/auth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ async def request_token(self, token_params: Optional[dict] = None,
229229
log.debug("Token: %s" % str(response_dict.get("token")))
230230
return TokenDetails.from_dict(response_dict)
231231

232-
async def create_token_request(self, token_params: Optional[dict] = None, key_name: Optional[str] = None,
232+
async def create_token_request(self, token_params: Optional[dict | str] = None, key_name: Optional[str] = None,
233233
key_secret: Optional[str] = None, query_time=None):
234234
token_params = token_params or {}
235235
token_request = {}

ably/types/capability.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from collections.abc import MutableMapping
2+
from typing import Optional, Union
23
import json
34
import logging
45

@@ -7,11 +8,19 @@
78

89

910
class Capability(MutableMapping):
10-
def __init__(self, obj=None):
11-
if obj is None:
12-
obj = {}
13-
self.__dict = dict(obj)
14-
for k, v in obj.items():
11+
def __init__(self, capability: Optional[Union[dict, str]] = None):
12+
# RSA9f: provided capability can be a JSON string
13+
if capability and isinstance(capability, str):
14+
try:
15+
capability = json.loads(capability)
16+
except json.JSONDecodeError:
17+
capability = json.loads(capability.replace("'", '"'))
18+
19+
if capability is None:
20+
capability = {}
21+
22+
self.__dict = dict(capability)
23+
for k, v in capability.items():
1524
self[k] = v
1625

1726
def __eq__(self, other):

ably/types/tokendetails.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,7 @@ def __init__(self, token=None, expires=None, issued=0,
2020
self.__expires = expires
2121
self.__token = token
2222
self.__issued = issued
23-
if capability and isinstance(capability, str):
24-
try:
25-
self.__capability = Capability(json.loads(capability))
26-
except json.JSONDecodeError:
27-
self.__capability = Capability(json.loads(capability.replace("'", '"')))
28-
else:
29-
self.__capability = Capability(capability or {})
23+
self.__capability = Capability(capability or {})
3024
self.__client_id = client_id
3125

3226
@property

test/ably/rest/restcapability_test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,17 @@ async def test_invalid_capabilities_3(self):
240240
the_exception = excinfo.value
241241
assert 400 == the_exception.status_code
242242
assert 40000 == the_exception.code
243+
244+
@dont_vary_protocol
245+
def test_capability_from_string(self):
246+
capability_from_str = Capability('{"cansubscribe":["subscribe"]}')
247+
capability_from_str_single_quote = Capability('{\'cansubscribe\':[\'subscribe\']}')
248+
249+
capability_from_dict = Capability({
250+
"cansubscribe": ["subscribe"]
251+
})
252+
253+
assert capability_from_str == capability_from_dict, "Unexpected Capability constructed from string"
254+
assert (
255+
capability_from_str_single_quote == capability_from_dict
256+
), "Unexpected Capability constructed from string"

0 commit comments

Comments
 (0)