[dancer-users] Running a bot as a REST webservice

Pierre M piemas25 at gmail.com
Thu Dec 5 17:24:21 GMT 2013


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?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.preshweb.co.uk/pipermail/dancer-users/attachments/20131205/d0a4a8d6/attachment.html>


More information about the dancer-users mailing list