Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ably/rest/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ async def request_token(self, token_params: Optional[dict] = None,
log.debug("Token: %s" % str(response_dict.get("token")))
return TokenDetails.from_dict(response_dict)

async def create_token_request(self, token_params: Optional[dict] = None, key_name: Optional[str] = None,
async def create_token_request(self, token_params: Optional[dict | str] = None, key_name: Optional[str] = None,
key_secret: Optional[str] = None, query_time=None):
token_params = token_params or {}
token_request = {}
Expand Down
19 changes: 14 additions & 5 deletions ably/types/capability.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from collections.abc import MutableMapping
from typing import Optional, Union
import json
import logging

Expand All @@ -7,11 +8,19 @@


class Capability(MutableMapping):
def __init__(self, obj=None):
if obj is None:
obj = {}
self.__dict = dict(obj)
for k, v in obj.items():
def __init__(self, capability: Optional[Union[dict, str]] = None):
# RSA9f: provided capability can be a JSON string
if capability and isinstance(capability, str):
try:
capability = json.loads(capability)
except json.JSONDecodeError:
capability = json.loads(capability.replace("'", '"'))

if capability is None:
capability = {}

self.__dict = dict(capability)
for k, v in capability.items():
self[k] = v

def __eq__(self, other):
Expand Down
8 changes: 1 addition & 7 deletions ably/types/tokendetails.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,7 @@ def __init__(self, token=None, expires=None, issued=0,
self.__expires = expires
self.__token = token
self.__issued = issued
if capability and isinstance(capability, str):
try:
self.__capability = Capability(json.loads(capability))
except json.JSONDecodeError:
self.__capability = Capability(json.loads(capability.replace("'", '"')))
else:
self.__capability = Capability(capability or {})
self.__capability = Capability(capability or {})
self.__client_id = client_id

@property
Expand Down
14 changes: 14 additions & 0 deletions test/ably/rest/restcapability_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,17 @@ async def test_invalid_capabilities_3(self):
the_exception = excinfo.value
assert 400 == the_exception.status_code
assert 40000 == the_exception.code

@dont_vary_protocol
def test_capability_from_string(self):
capability_from_str = Capability('{"cansubscribe":["subscribe"]}')
capability_from_str_single_quote = Capability('{\'cansubscribe\':[\'subscribe\']}')

capability_from_dict = Capability({
"cansubscribe": ["subscribe"]
})

assert capability_from_str == capability_from_dict, "Unexpected Capability constructed from string"
assert (
capability_from_str_single_quote == capability_from_dict
), "Unexpected Capability constructed from string"
Loading