Every request from the web server generates a call to the application code reference that is built by "dance". Variables do not persist between requests, nor should they. You could solve it by keeping the variable in a cache. You could use Memcached, KiokuDB, or the likes. Another way would be to simply login every time you make a request. This is what's done in scopes in KiokuDB. Another way to solve it, uglier IMHO, is using singletons. But that's just having global scope for stuff, which you should generally avoid. On Thu, Dec 5, 2013 at 6:24 PM, Pierre M <piemas25@gmail.com> wrote:
Hi, I'm making a bot. I'm trying to integrate it into a Dancer2 app, in order to make a REST API for it, and later be able to interact with it from the browser.
It logs in to an external webservice, like this: $bot->login($username, $password); This stores some credentilals in the bot, which uses them for other requests. And then it does some other stuff: $->do_stuff; The bot exists and can make these very basic operations from a script.
To create the REST API with Dancer2, I made something in those lines: my $bot = MyBot->new; post '/login.json' => sub { my ($username, $password) = (params->{username}, params->{password});
$bot->login($username, $password); # Store credentials in the bot }; This works perfectly.
And then post '/do_stuff.json' => sub { $bot->do_stuff; } This doesn't work: when I call '/login.json', followed by '/do_stuff.json', the second request fails because the credentials are not present: it seems to not be interacting with the same $bot object than the login request.
It works if add $bot->login( ... ) at the beginning of the '/do_stuff.json' subroutine, but I don't want to login at each new request.
What is a clean solution for this use case?
* 30 minutes later * I have found Dancer2::Plugin::Adapter<https://metacpan.org/pod/Dancer2::Plugin::Adapter>and the "singleton" scope, which seems to be what I need. But it won't install. I will send a RT ticket. Any idea for an alternative solution?
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users