Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ably_rest.auth.request_token capabilities call - unnecessary json.dumps() ? #579

Open
NgyAnthony opened this issue Feb 10, 2025 · 1 comment
Labels
bug Something isn't working. It's clear that this does need to be fixed.

Comments

@NgyAnthony
Copy link

NgyAnthony commented Feb 10, 2025

Following the tutorial at https://ably.com/docs/auth/capabilities:

# Token request that specifies capabilities:
capabilities = {
    "chat:bob": ["subscribe"],  # only "subscribe" intersects
    "status": ["*"],  # "*" intersects with "subscribe"
    "secret": ["publish", "subscribe"]  # key does not have access to "secret" channel
}


token_details = await ably_rest.auth.request_token({
    'capability': json.dumps(capabilities)
})

Will result in the following error:

  File "/usr/local/lib/python3.12/site-packages/ably/rest/auth.py", line 272, in create_token_request
    token_request['capability'] = str(Capability(capability))
                                      ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/ably/types/capability.py", line 13, in __init__
    self.__dict = dict(obj)
                  ^^^^^^^^^
ValueError: dictionary update sequence element #0 has length 1; 2 is required

This is due to the following init form Capability:

class Capability(MutableMapping):
    def __init__(self, obj=None):
        if obj is None:
            obj = {}
        self.__dict = dict(obj)
        for k, v in obj.items():
            self[k] = v

Unwrapping capabilities from the suggested json.dumps() solves the issue

┆Issue is synchronized with this Jira Task by Unito

@VeskeR
Copy link
Contributor

VeskeR commented Feb 12, 2025

Hi @NgyAnthony !

Thank you for reporting this issue.

The SDK is intended to be able to accept a JSON string for the capability parameter (as well as a dictionary) when creating token details. The fact that it cannot process json.dumps(capabilities) is a bug. We will fix this in the library.

In the meantime, you can pass a dictionary directly when creating a token, just as you suggested:

token_details = await ably_rest.auth.request_token({
    'capability': capabilities
})

@VeskeR VeskeR added the bug Something isn't working. It's clear that this does need to be fixed. label Feb 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working. It's clear that this does need to be fixed.
Development

No branches or pull requests

2 participants