{"action":"created","issue":{"url":"https://api.github.com/repos/thisbejim/Pyrebase/issues/11","labels_url":"https://api.github.com/repos/thisbejim/Pyrebase/issues/11/labels{/name}","comments_url":"https://api.github.com/repos/thisbejim/Pyrebase/issues/11/comments","events_url":"https://api.github.com/repos/thisbejim/Pyrebase/issues/11/events","html_url":"https://github.com/thisbejim/Pyrebase/issues/11","id":118810258,"number":11,"title":"get() on simple structures fails","user":{"login":"Tenzer","id":68696,"avatar_url":"https://avatars.githubusercontent.com/u/68696?v=3","gravatar_id":"","url":"https://api.github.com/users/Tenzer","html_url":"https://github.com/Tenzer","followers_url":"https://api.github.com/users/Tenzer/followers","following_url":"https://api.github.com/users/Tenzer/following{/other_user}","gists_url":"https://api.github.com/users/Tenzer/gists{/gist_id}","starred_url":"https://api.github.com/users/Tenzer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Tenzer/subscriptions","organizations_url":"https://api.github.com/users/Tenzer/orgs","repos_url":"https://api.github.com/users/Tenzer/repos","events_url":"https://api.github.com/users/Tenzer/events{/privacy}","received_events_url":"https://api.github.com/users/Tenzer/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-11-25T10:55:42Z","updated_at":"2015-11-25T21:37:32Z","closed_at":null,"body":"If you only want to get a single item/key in Firebase, which contains one of the primitives Firebase supports (string, number or boolean), `get()` will fail with something like this:\r\n\r\n      File \"path/to/virtualenv/lib/python3.4/site-packages/pyrebase/pyrebase.py\", line 94, in get\r\n        if not isinstance(list(request_dict.values())[0], dict):\r\n    AttributeError: 'str' object has no attribute 'values'\r\n\r\nThis is with Pyrebase 0.3.1 and with the key I am trying to get being a string. In my [original pull request](https://github.com/thisbejim/Pyrebase/pull/4) for the previous `one()` method, I added a check for if we got a dictionary or something else back, before the id attribute was attempted to be set on the data:\r\n\r\n```python\r\nif isinstance(request_json, dict):\r\n    request_json[\"id\"] = item_id\r\n```\r\n\r\nIn the current version we have a check which is supposed to check the same, as far as I can see, in the interest of returning the raw primitive, if it's not a dictionary, which looks like this:\r\n\r\n```python\r\nif not isinstance(list(request_dict.values())[0], dict):\r\n    return request_dict\r\n```\r\n\r\nAs is evident in the stacktrace, the `values()` method fails on a string, and would in fact fail on anything but a dictionary. I wonder what the reason was behind structuring the check like that. Wouldn't it be better with the simpler method shown on top here?"},"comment":{"url":"https://api.github.com/repos/thisbejim/Pyrebase/issues/comments/159736741","html_url":"https://github.com/thisbejim/Pyrebase/issues/11#issuecomment-159736741","issue_url":"https://api.github.com/repos/thisbejim/Pyrebase/issues/11","id":159736741,"user":{"login":"thisbejim","id":10917363,"avatar_url":"https://avatars.githubusercontent.com/u/10917363?v=3","gravatar_id":"","url":"https://api.github.com/users/thisbejim","html_url":"https://github.com/thisbejim","followers_url":"https://api.github.com/users/thisbejim/followers","following_url":"https://api.github.com/users/thisbejim/following{/other_user}","gists_url":"https://api.github.com/users/thisbejim/gists{/gist_id}","starred_url":"https://api.github.com/users/thisbejim/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/thisbejim/subscriptions","organizations_url":"https://api.github.com/users/thisbejim/orgs","repos_url":"https://api.github.com/users/thisbejim/repos","events_url":"https://api.github.com/users/thisbejim/events{/privacy}","received_events_url":"https://api.github.com/users/thisbejim/received_events","type":"User","site_admin":false},"created_at":"2015-11-25T21:37:32Z","updated_at":"2015-11-25T21:37:32Z","body":"Ah yes I see what's happening here. So the new version is checking to see if only one dictionary level is returned, as in: \r\n```python\r\n{\r\n  \"name\": \"henry\"\r\n}\r\n```\r\nrather than \r\n```python\r\n{\r\n  \"-K2QRiOt8oQ5vBYS5mI1\":  {\r\n    \"name\": \"henry\"\r\n  }, \r\n  \"-K2QfF5GZ2psbso7lEMj\": {\r\n    \"name\": \"bob\"\r\n  }\r\n}\r\n```\r\nso we don't have to unpack it from a list. Then the first version is checking if we are only returning one primitive e.g. ```\"bob\"```, so really they should be combined into something like:\r\n```python\r\n# check if dict\r\nif isinstance(request_object.json(), dict):\r\n    # if one level dict return to avoid unpacking list\r\n    if not isinstance(list(request_dict.values())[0], dict):\r\n        return request_dict\r\nelse:\r\n    # return primitive\r\n    return request_object.json()\r\n```\r\nActually that's not quite right, because the request_dict.values() section only checks if the first value is a dict while it is certainly possible the second, third or fourth value could be a dict (as seen in #12). Hmmm will have to think about this a bit."}}