<div dir="ltr"><div><div><div>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. <br><br>It logs in to an external webservice, like this:<br>
</div> $bot->login($username, $password);<br>This stores some credentilals in the bot, which uses them for other requests.<br>And then it does some other stuff:<br></div> $->do_stuff;<br></div><div>The bot exists and can make these very basic operations from a script.<br>
<br></div><div>To create the REST API with Dancer2, I made something in those lines:<br></div><div> my $bot = MyBot->new;<br></div><div> post '/login.json' => sub {<br> my ($username, $password) = (params->{username}, params->{password});<br>
<br> $bot->login($username, $password); # Store credentials in the bot<br> };<br></div><div>This works perfectly. <br><br>And then<br></div><div> post '/do_stuff.json' => sub {<br></div><div>
$bot->do_stuff;<br> }<br></div><div>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.<br>
<br>It works if add<br></div><div> $bot->login( ... )<br></div><div>at the beginning of the '/do_stuff.json' subroutine, but I don't want to login at each new request.<br><br></div><div>What is a clean solution for this use case?<br>
<br></div><div>* 30 minutes later *<br>I have found <a href="https://metacpan.org/pod/Dancer2::Plugin::Adapter">Dancer2::Plugin::Adapter</a> and the "singleton" scope, which seems to be what I need. But it won't install. I will send a RT ticket. <br>
</div><div>Any idea for an alternative solution?<br></div><div><br></div></div>