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

self.tokens does not persist between requests #8

Open
hello-josh opened this issue Sep 17, 2013 · 5 comments
Open

self.tokens does not persist between requests #8

hello-josh opened this issue Sep 17, 2013 · 5 comments

Comments

@hello-josh
Copy link
Contributor

I'm working on handling glass callbacks with subscriptions and on https://github.com/SamyPesse/glass.py/blob/master/glass/subscriptions.py#L51
there is a check to see if the callback json data matches an existing user token. Since self.tokens does not persist between requests this always fails. I am fixing a few bugs and I figured while I was at it I would implement a different solution for this.

Does this work for you in the emulator or is it mostly untested code? In order to keep this code as platform agnostic as possible I was thinking of implementing some sort of user defined get_user callback so you can make your own depending on your storage needs. What do you think?

@SamyPesse
Copy link
Owner

It seems that there is another error that the persisting of tokens because self is maybe not the Subscriptions object in handler (so self.tokens is not defined).
I don't think there is an other error with Subscriptions.tokens because there is only one Subscription object for the all application, the fail in your test probably come from the error I talked about before.

here is a really quick fix (I didn't test it), tell me what you think about it : https://gist.github.com/SamyPesse/795258b15c12f14a6058

But it's a good idea to implement sort of user defined get_user callback with a default in-memory storage for the users.
I have issues for well testing glass.py and subscriptions are really hard to test, sorry for the error, I have to find a better way to test it and maybe add some unit tests.

(Sorry for my english).

@hello-josh
Copy link
Contributor Author

In my case, self does work inside of handler() because I am able to use self.app.logger. I've also had to return a response from handler() in order for Flask to respond properly. It may be because I am using google app engine instead of Application.run()?

# Add view for subscription
def handler():
    data = json.loads(flask.request.data)
    userid = data["userToken"]
    if not userid in self.tokens:
        self.app.logger.error("Callback for a non-existent user")
    else:
        user = User(app=self.app, tokens=self.tokens[userid])
        if data["collection"] == "timeline":
            for action in data["actions"]:
                self.call_endpoint("action."+action["type"], user)
        elif data["collection"] == "locations":
            self.call_endpoint("location", user)
    return '', 200

self.app.web.add_url_rule('/glass/callback/%s' % subscription_id, 'callback_%s' % subscription_id,
                          handler, methods=["POST"])

I will implement a user defined get_user callback with an in-memory fallback and see how that works. If all is well with it I will submit the fixes as a pull request later today or tomorrow.

@SamyPesse
Copy link
Owner

OK, I think it could be a good way to implement it in another class and so user can inherit this class to create his own backend storage for users (for example to store in mysql or mongodb). And let user build application with its own backend storage or use the default one "in memory", example :

app = Application(.., storage=MyStorageBackend())

@hello-josh
Copy link
Contributor Author

I agree.

@hello-josh
Copy link
Contributor Author

I'm working on some of this functionality along with some bug fixes and enhancements. I've added an ext package with an appengine datastore backend as well as an appengine memcached backend. You can see a compare if it here: https://github.com/Trii/glass.py/compare/master...pluggable-user-storage
It isn't ready for a pull request yet but I will have something soon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants