Great job pushing your application forward with Async/Await! Looking at
your snippet, here's one small change you can probably make using the json()
method from Mojo::Message
<https://mojolicious.org/perldoc/Mojo/Message#json>:
$app->helper(summary_data => async sub ($c, $access_token) {
my $ua = Mojo::UserAgent->new;
my $tx = await $ua->post_p(
'http://' . $c->config->{API_IP} .
'/api-app/analytics/summary/',
{ Authorization => 'Bearer ' . $access_token },
*json* => { periods => *$c->session('cx_current_quarter')* },
);
if ($tx->result->is_success) {
return *$tx->result->json*;
}
$c->flash(message_type => 'warning');
$c->flash(message => 'Could not get summary data');
return $c->redirect_to('cx-summary', status => 403);
});
On Thu, Jun 4, 2020 at 2:31 AM Jeyaraj Durairaj <[email protected]>
wrote:
> Hi,
>
> It works for *helpers* as well with the following declaration.
>
> $app->helper(summary_data => async sub ($c, $access_token) {
> my $ua = Mojo::UserAgent->new;
>
> my $tx = await $ua->post_p(
> 'http://' . $c->config->{API_IP} .
> '/api-app/analytics/summary/',
> { Authorization => 'Bearer ' . $access_token },
> form => { periods => encode_json([
> $c->session('cx_current_quarter') ]) },
> );
> if ($tx->result->is_success) {
> return from_json($tx->result->body);
> }
> $c->flash(message_type => 'warning');
> $c->flash(message => 'Could not get summary data');
> return $c->redirect_to('cx-summary', status => 403);
> });
>
>
>
>
> -- Jeyaraj
>
> On Thursday, 4 June 2020 12:06:01 UTC+5:30, Jeyaraj Durairaj wrote:
>>
>> Hi,
>>
>> Eureka! Eureka! It works!.
>>
>> The solution I found (because of my error) is that I uninstalled
>> Mojo::AsyncAwait and installed Future::AsyncAwait.
>> It just works!
>>
>> By June end, my business intelligence application will be live to serve
>> 100+ users with insights and analytics.
>>
>> (To apply icing on the cake, I have plugged in Moose also in to model
>> layer for my application)
>>
>> As Glen Hinkle said, it is really really a fun to work in Mojolicious. I
>> love Hypnotoad.
>>
>> BTW, I am doing a private project as well to help a home designer to
>> launch a web site for their small firm. Guess! what framework I am gonna
>> use.. *Mojolicious.*
>>
>> Thanks to Mojo group and special thanks to SRI as well.
>>
>> -- Jeyaraj
>>
>>
>>
>> On Thursday, 28 May 2020 14:15:04 UTC+5:30, Jeyaraj Durairaj wrote:
>>>
>>> any luck on the solutions?
>>>
>>> -- Jeyaraj
>>>
>>> On Thursday, 28 May 2020 00:10:48 UTC+5:30, Jeyaraj Durairaj wrote:
>>>>
>>>> However, the below code perfectly work for me outside Mojolicious App.
>>>>
>>>>
>>>> use Modern::Perl;
>>>> use Mojo::Base -strict, -signatures;
>>>> use Mojo::UserAgent;
>>>> use Mojo::Promise;
>>>> use Mojo::IOLoop;
>>>> use Mojo::Util 'trim';
>>>> use Mojo::AsyncAwait;
>>>> use LWP::UserAgent;
>>>> use JSON;
>>>>
>>>>
>>>> my $ua = Mojo::UserAgent->new;
>>>> my $result = $ua->post('http://localhost:8000/api/token/' => json => {
>>>> username => 'username', password => 'mypassword' })
>>>> ->result
>>>> ->json;
>>>> my $access_token = $result->{access};
>>>> my $refresh_token = $result->{refresh};
>>>> print "Access Token: " . $result->{access}, "\n";
>>>> print "Refresh Token: " . $result->{refresh}, "\n\n";
>>>>
>>>>
>>>> $ua = Mojo::UserAgent->new;
>>>> $result = $ua->post('http://' . 'localhost:8000' .
>>>> '/api/token/refresh/' => json => { refresh => $refresh_token })
>>>> ->result
>>>> ->json;
>>>>
>>>>
>>>> my $agent = Mojo::UserAgent->new;
>>>> async post_user_p => sub ($url) {
>>>> say "\n\n", $url;
>>>> my $tx = await $agent->post_p(
>>>> $url,
>>>> {
>>>> Authorization => 'Bearer ' . $result->{access},
>>>> },
>>>> form => {"username" => "username"},
>>>>
>>>> );
>>>> return trim from_json($tx->result->body)->{first_name};
>>>> };
>>>>
>>>> async main => sub (@urls) {
>>>> my @promises = map { post_user_p($_) } @urls;
>>>> my @names = await Mojo::Promise->all(@promises);
>>>> say for map { $_->[0] } @names;
>>>> };
>>>>
>>>> my @urls = (qw(
>>>> http://localhost:8000/api-converge/auth/user/
>>>> ));
>>>>
>>>> main(@urls)->wait;
>>>>
>>>>
>>>>
>>>>
>>>> On Wednesday, 27 May 2020 23:41:27 UTC+5:30, Jeyaraj Durairaj wrote:
>>>>>
>>>>> I tried generating an example app and copied pasted the code segment
>>>>> you posted here and then tried.
>>>>>
>>>>> "Action not found!" error is shown.
>>>>>
>>>>> Should I reinstall Perl again and then try?
>>>>> Will it work under morbo or works only on Hypnotoad?
>>>>> I am using Windows for my development, wherein morbo is the only
>>>>> server I can test.
>>>>> Or can I test it on daemon mode and try>
>>>>> Please suggest as to how I should diagnose it.
>>>>> I would need this Async/await to be working in my system.
>>>>> I have tried without Async, all other functionalities rock both in
>>>>> development and production (hypnotoad). Mojo::UserAgent is amazing to
>>>>> fetch
>>>>> API call data from Django server (running under Apache).
>>>>>
>>>>> I am completely counting on Mojolicious as my primary framework for my
>>>>> new projects. Please help.
>>>>>
>>>>>
>>>>> Regards/Jeyaraj
>>>>>
>>>>> On Wednesday, 27 May 2020 17:46:49 UTC+5:30, Sebastian Riedel wrote:
>>>>>>
>>>>>> I'll assume it was you who asked the same question on IRC earlier.
>>>>>> Had you been sticking around for a bit longer you would have seen my
>>>>>> instructions for getting a working application that i tested locally.
>>>>>>
>>>>>> 14:08 <kraih> to be sure i did a "mojo generate app" and then
>>>>>> replaced the controller with http://paste.scsys.co.uk/589044
>>>>>>
>>>>>> That's the important part, if that doesn't work your Perl or some
>>>>>> module you installed is broken.
>>>>>>
>>>>>> --
>>>>>> sebastian
>>>>>>
>>>>> --
> You received this message because you are subscribed to the Google Groups
> "Mojolicious" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/mojolicious/debdea2d-2f67-4feb-814d-9d936d1b6732%40googlegroups.com
> <https://groups.google.com/d/msgid/mojolicious/debdea2d-2f67-4feb-814d-9d936d1b6732%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
--
You received this message because you are subscribed to the Google Groups
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/mojolicious/CACyQ%2BFRTLREHuWGiVRUZ0sYN7qJ_aS_pXe4bA41b9bgMN23gdw%40mail.gmail.com.