Discussion:
using mojolicious to make a batch request to google api
(too old to reply)
Steve Dondley
2018-09-18 02:22:09 UTC
Permalink
I'm trying to hack a perl module that uses mojolicious to make api calls to
google's api so that it can make batch api call as documented
here: https://developers.google.com/gmail/api/guides/batch

I've made various attempts but no calls are resulting in a successful
response. Here's what I'm currently trying.

my $options =
[
{
content => 'GET
/gmail/v1/users/***@gmail.com/messages/165e91c27e4af0be',
'Content-Type' => 'application/http',
},
{
content => 'GET
/gmail/v1/users/***@gmail.com/messages/165e94ce49900df2',
'Content-Type' => 'application/http',
}
];


my $path = 'https://www.googleapis.com/batch/' . $s->api . '/' . $s->
version;


my $headers = {'Authorization' => 'Bearer TOKEN_HERE'};




$tx = $self->ua->build_tx(
uc $http_method => $path => $headers => json => $optional_data
);




This results in a tranaction object that looks like this:


$VAR1 = bless( {
'req' => bless( {
'content' => bless( {
'headers' => bless(
{

'headers' => {

'content-type' => [

'application/json'

],

'authorization' => [

'Bearer TOKEN_HERE'

],

'accept-encoding' => [

'gzip'

],

'user-agent' => [

'Mojolicious (Perl)'

]

}

}, 'Mojo::Headers' ),
'events' => {

'read' => [

sub { "DUMMY" }

]
},
'read' => $VAR1->{
'req'}{'content'}{'events'}{'read'}[0],
'asset' => bless( {

'content' => '[{"Content-Type":"application\\/http","content":"GET
\\/gmail\\/v1\\/u
sers\\/***@gmail.com\\/messages\\/165e91c27e4af0be"},{"Content-Type":"application\\/http","content":"GET
\\/gmail\\/v1\\/users\\/***@gmail.com\\/messag
es\\/165e94ce49900df2"}]'

}, 'Mojo::Asset::Memory' )
},
'Mojo::Content::Single' ),
'url' => bless( {
'scheme' => 'https',
'host' =>
'www.googleapis.com',
'path' => bless( {

'path' => '/batch/gmail/v1'
},
'Mojo::Path' )
}, 'Mojo::URL' ),
'method' => 'POST'
}, 'Mojo::Message::Request' )
}, 'Mojo::Transaction::HTTP' );



Trying anything other than json for the generator results in an error:
Use of uninitialized value $cb in method lookup

Fiddling with the content type settings doesn't help make this go away.
--
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 mojolicious+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.
Stefan Adams
2018-09-18 02:50:48 UTC
Permalink
Post by Steve Dondley
I'm trying to hack a perl module that uses mojolicious to make api calls
https://developers.google.com/gmail/api/guides/batch
I've made various attempts but no calls are resulting in a successful
response. Here's what I'm currently trying.
my $options =
[
{
content => 'GET /gmail/v1/users/
'Content-Type' => 'application/http',
},
{
content => 'GET /gmail/v1/users/
'Content-Type' => 'application/http',
}
];
I don't see $options being used in your sample code. Also it seems weird.
Admittedly, I haven't read (or even looked at) the api guide you referenced.
I personally have never seen content type application/http -- perhaps I
live under a rock.
Post by Steve Dondley
my $path = 'https://www.googleapis.com/batch/' . $s->api . '/' . $s
->version;
my $headers = {'Authorization' => 'Bearer TOKEN_HERE'};
$tx = $self->ua->build_tx(
uc $http_method => $path => $headers => json =>
$optional_data );
What is $http_method and what is $optional_data? These are used but
undefined, in your sample.

This results in a tranaction object that looks like this:
It may not be what you want, but the object looks assembled correctly to
me. Can you point out anything in particular that doesn't look the way you
think it should? Fishiest looking thing I see is the content body (req ->
content -> asset -> content). I assume your intentionally removed your
bearer token for this extract.
Post by Steve Dondley
$VAR1 = bless( {
'req' => bless( {
'content' => bless( {
'headers' =>
bless( {
'headers' => {
'content-type' => [
'application/json'
],
'authorization' => [
'Bearer TOKEN_HERE'
],
'accept-encoding' => [
'gzip'
],
'user-agent' => [
'Mojolicious (Perl)'
]
}
}, 'Mojo::Headers' ),
'events' => {
'read' => [
sub { "DUMMY" }
]
},
'read' => $VAR1
->{'req'}{'content'}{'events'}{'read'}[0],
'asset' => bless( {
'content' => '[{"Content-Type":"application\\/http","content":"GET
\\/gmail\\/v1\\/u
es\\/165e94ce49900df2"}]'
}, 'Mojo::Asset::Memory' )
},
'Mojo::Content::Single' ),
'url' => bless( {
'scheme' => 'https',
'host' => '
www.googleapis.com',
'path' => bless( {
'path' => '/batch/gmail/v1'
},
'Mojo::Path' )
}, 'Mojo::URL' ),
'method' => 'POST'
}, 'Mojo::Message::Request' )
}, 'Mojo::Transaction::HTTP' );
Use of uninitialized value $cb in method lookup
Is that because $optional_data isn't defined?
Post by Steve Dondley
Fiddling with the content type settings doesn't help make this go away.
content-type in $headers, or in $options? The json generator is correctly
generating the correct application/json content type -- I assume (hope) you
are wanting json? What's with the funny business of the content type in
$options? It seems that would be a google api -specific thing, which I
can't comment on (didn't open the api spec).

I don't expect to be of any direct help, but hopefully I got you thinking.
--
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 mojolicious+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.
Steve Dondley
2018-09-18 02:56:43 UTC
Permalink
My bad. Where it says $optional_data, that is actually the $options
variable. That bit was pasted from a different function. Thanks for getting
back.

The Google API example shows a "boundary" in the "Content-Type" and a
"Content-Length." I assume I can get rid of those and Mojolicious will
handle that for me?
Post by Stefan Adams
Post by Steve Dondley
I'm trying to hack a perl module that uses mojolicious to make api calls
https://developers.google.com/gmail/api/guides/batch
I've made various attempts but no calls are resulting in a successful
response. Here's what I'm currently trying.
my $options =
[
{
content => 'GET /gmail/v1/users/
'Content-Type' => 'application/http',
},
{
content => 'GET /gmail/v1/users/
'Content-Type' => 'application/http',
}
];
I don't see $options being used in your sample code. Also it seems
weird. Admittedly, I haven't read (or even looked at) the api guide you
referenced.
I personally have never seen content type application/http -- perhaps I
live under a rock.
Post by Steve Dondley
my $path = 'https://www.googleapis.com/batch/' . $s->api . '/' . $s
->version;
my $headers = {'Authorization' => 'Bearer TOKEN_HERE'};
$tx = $self->ua->build_tx(
uc $http_method => $path => $headers => json =>
$optional_data );
What is $http_method and what is $optional_data? These are used but
undefined, in your sample.
It may not be what you want, but the object looks assembled correctly to
me. Can you point out anything in particular that doesn't look the way you
think it should? Fishiest looking thing I see is the content body (req ->
content -> asset -> content). I assume your intentionally removed your
bearer token for this extract.
Post by Steve Dondley
$VAR1 = bless( {
'req' => bless( {
'content' => bless( {
'headers' =>
bless( {
'headers' => {
'content-type' => [
'application/json'
],
'authorization' => [
'Bearer TOKEN_HERE'
],
'accept-encoding' => [
'gzip'
],
'user-agent' => [
'Mojolicious (Perl)'
]
}
}, 'Mojo::Headers' ),
'events' => {
'read' => [
sub { "DUMMY" }
]
},
'read' => $VAR1
->{'req'}{'content'}{'events'}{'read'}[0],
'asset' => bless
( {
'content' => '[{"Content-Type":"application\\/http","content":"GET
\\/gmail\\/v1\\/u
es\\/165e94ce49900df2"}]'
}, 'Mojo::Asset::Memory' )
},
'Mojo::Content::Single' ),
'url' => bless( {
'scheme' => 'https',
'host' => '
www.googleapis.com',
'path' => bless( {
'path' => '/batch/gmail/v1'
},
'Mojo::Path' )
}, 'Mojo::URL' ),
'method' => 'POST'
}, 'Mojo::Message::Request' )
}, 'Mojo::Transaction::HTTP' );
Use of uninitialized value $cb in method lookup
Is that because $optional_data isn't defined?
Post by Steve Dondley
Fiddling with the content type settings doesn't help make this go away.
content-type in $headers, or in $options? The json generator is
correctly generating the correct application/json content type -- I assume
(hope) you are wanting json? What's with the funny business of the content
type in $options? It seems that would be a google api -specific thing,
which I can't comment on (didn't open the api spec).
I don't expect to be of any direct help, but hopefully I got you thinking.
--
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 mojolicious+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.
Stefan Adams
2018-09-18 03:48:50 UTC
Permalink
Post by Steve Dondley
My bad. Where it says $optional_data, that is actually the $options
variable. That bit was pasted from a different function. Thanks for getting
back.
The Google API example shows a "boundary" in the "Content-Type" and a
"Content-Length."
"boundary"... that sounds like you should be using the multipart generator?

Sorry, I'm completely out of my league here...

I assume I can get rid of those and Mojolicious will handle that for me?


I generally find that I can assume anything with Mojolicious and it'll Do
The Right Thing. ;D No of course that's not true, just plugging how well
designed it is. :D
--
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 mojolicious+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.
Steve Dondley
2018-09-18 02:58:58 UTC
Permalink
$httpMethod is set to "post". I forgot to add that in.
Post by Stefan Adams
Post by Steve Dondley
I'm trying to hack a perl module that uses mojolicious to make api calls
https://developers.google.com/gmail/api/guides/batch
I've made various attempts but no calls are resulting in a successful
response. Here's what I'm currently trying.
my $options =
[
{
content => 'GET /gmail/v1/users/
'Content-Type' => 'application/http',
},
{
content => 'GET /gmail/v1/users/
'Content-Type' => 'application/http',
}
];
I don't see $options being used in your sample code. Also it seems
weird. Admittedly, I haven't read (or even looked at) the api guide you
referenced.
I personally have never seen content type application/http -- perhaps I
live under a rock.
Post by Steve Dondley
my $path = 'https://www.googleapis.com/batch/' . $s->api . '/' . $s
->version;
my $headers = {'Authorization' => 'Bearer TOKEN_HERE'};
$tx = $self->ua->build_tx(
uc $http_method => $path => $headers => json =>
$optional_data );
What is $http_method and what is $optional_data? These are used but
undefined, in your sample.
It may not be what you want, but the object looks assembled correctly to
me. Can you point out anything in particular that doesn't look the way you
think it should? Fishiest looking thing I see is the content body (req ->
content -> asset -> content). I assume your intentionally removed your
bearer token for this extract.
Post by Steve Dondley
$VAR1 = bless( {
'req' => bless( {
'content' => bless( {
'headers' =>
bless( {
'headers' => {
'content-type' => [
'application/json'
],
'authorization' => [
'Bearer TOKEN_HERE'
],
'accept-encoding' => [
'gzip'
],
'user-agent' => [
'Mojolicious (Perl)'
]
}
}, 'Mojo::Headers' ),
'events' => {
'read' => [
sub { "DUMMY" }
]
},
'read' => $VAR1
->{'req'}{'content'}{'events'}{'read'}[0],
'asset' => bless
( {
'content' => '[{"Content-Type":"application\\/http","content":"GET
\\/gmail\\/v1\\/u
es\\/165e94ce49900df2"}]'
}, 'Mojo::Asset::Memory' )
},
'Mojo::Content::Single' ),
'url' => bless( {
'scheme' => 'https',
'host' => '
www.googleapis.com',
'path' => bless( {
'path' => '/batch/gmail/v1'
},
'Mojo::Path' )
}, 'Mojo::URL' ),
'method' => 'POST'
}, 'Mojo::Message::Request' )
}, 'Mojo::Transaction::HTTP' );
Use of uninitialized value $cb in method lookup
Is that because $optional_data isn't defined?
Post by Steve Dondley
Fiddling with the content type settings doesn't help make this go away.
content-type in $headers, or in $options? The json generator is
correctly generating the correct application/json content type -- I assume
(hope) you are wanting json? What's with the funny business of the content
type in $options? It seems that would be a google api -specific thing,
which I can't comment on (didn't open the api spec).
I don't expect to be of any direct help, but hopefully I got you thinking.
--
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 mojolicious+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.
Steve Dondley
2018-09-18 02:57:12 UTC
Permalink
Post by Steve Dondley
I'm trying to hack a perl module that uses mojolicious to make api calls
https://developers.google.com/gmail/api/guides/batch
I've made various attempts but no calls are resulting in a successful
response. Here's what I'm currently trying.
my $options =
[
{
content => 'GET /gmail/v1/users/
'Content-Type' => 'application/http',
},
{
content => 'GET /gmail/v1/users/
'Content-Type' => 'application/http',
}
];
my $path = 'https://www.googleapis.com/batch/' . $s->api . '/' . $s
->version;
my $headers = {'Authorization' => 'Bearer TOKEN_HERE'};
$tx = $self->ua->build_tx(
uc $http_method => $path => $headers => json =>
$optional_data );
$VAR1 = bless( {
'req' => bless( {
'content' => bless( {
'headers' =>
bless( {
'headers' => {
'content-type' => [
'application/json'
],
'authorization' => [
'Bearer TOKEN_HERE'
],
'accept-encoding' => [
'gzip'
],
'user-agent' => [
'Mojolicious (Perl)'
]
}
}, 'Mojo::Headers' ),
'events' => {
'read' => [
sub { "DUMMY" }
]
},
'read' => $VAR1
->{'req'}{'content'}{'events'}{'read'}[0],
'asset' => bless( {
'content' => '[{"Content-Type":"application\\/http","content":"GET
\\/gmail\\/v1\\/u
es\\/165e94ce49900df2"}]'
}, 'Mojo::Asset::Memory' )
},
'Mojo::Content::Single' ),
'url' => bless( {
'scheme' => 'https',
'host' => '
www.googleapis.com',
'path' => bless( {
'path' => '/batch/gmail/v1'
},
'Mojo::Path' )
}, 'Mojo::URL' ),
'method' => 'POST'
}, 'Mojo::Message::Request' )
}, 'Mojo::Transaction::HTTP' );
Use of uninitialized value $cb in method lookup
Fiddling with the content type settings doesn't help make this go away.
--
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 mojolicious+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.
Dan Book
2018-09-18 04:45:24 UTC
Permalink
You want the multipart content generator. Scroll down to the very last
example in https://metacpan.org/pod/Mojo::UserAgent::Transactor#tx

-Dan
Post by Steve Dondley
I'm trying to hack a perl module that uses mojolicious to make api calls
https://developers.google.com/gmail/api/guides/batch
I've made various attempts but no calls are resulting in a successful
response. Here's what I'm currently trying.
my $options =
[
{
content => 'GET /gmail/v1/users/
'Content-Type' => 'application/http',
},
{
content => 'GET /gmail/v1/users/
'Content-Type' => 'application/http',
}
];
my $path = 'https://www.googleapis.com/batch/' . $s->api . '/' . $s
->version;
my $headers = {'Authorization' => 'Bearer TOKEN_HERE'};
$tx = $self->ua->build_tx(
uc $http_method => $path => $headers => json =>
$optional_data );
$VAR1 = bless( {
'req' => bless( {
'content' => bless( {
'headers' =>
bless( {
'headers' => {
'content-type' => [
'application/json'
],
'authorization' => [
'Bearer TOKEN_HERE'
],
'accept-encoding' => [
'gzip'
],
'user-agent' => [
'Mojolicious (Perl)'
]
}
}, 'Mojo::Headers' ),
'events' => {
'read' => [
sub { "DUMMY" }
]
},
'read' => $VAR1
->{'req'}{'content'}{'events'}{'read'}[0],
'asset' => bless( {
'content' => '[{"Content-Type":"application\\/http","content":"GET
\\/gmail\\/v1\\/u
es\\/165e94ce49900df2"}]'
}, 'Mojo::Asset::Memory' )
},
'Mojo::Content::Single' ),
'url' => bless( {
'scheme' => 'https',
'host' => '
www.googleapis.com',
'path' => bless( {
'path' => '/batch/gmail/v1'
},
'Mojo::Path' )
}, 'Mojo::URL' ),
'method' => 'POST'
}, 'Mojo::Message::Request' )
}, 'Mojo::Transaction::HTTP' );
Use of uninitialized value $cb in method lookup
Fiddling with the content type settings doesn't help make this go away.
--
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
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.
--
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 mojolicious+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.
Steve Dondley
2018-09-18 06:09:07 UTC
Permalink
Thanks. Yeah, I got that working somehow. When I tried it before I got an
error. I'm now getting a 400 error from google, however. Here's the $tx
object now:

$VAR1 = bless( {
'req' => bless( {
'method' => 'POST',
'content' => bless( {
'parts' => [
bless(
{

'asset' => bless( {

'content' => 'GET
/gmail/v1/users/***@gmail.com/messages/165eb111fcf21503'

}, 'Mojo::Asset::Memory' ),

'headers' => bless( {

'headers' => {

'content-type' => [


'application/http'

]

}

}, 'Mojo::Headers' ),

'events' => {

'read' => [

sub { "DUMMY" }

]

},

'read' => $VAR1->{'req'}{'content'}{'parts'}[0]{'events'}{'read'}[0]

}, 'Mojo::Content::Single' )
],
'events' => {

'read' => [

sub { "DUMMY" }

]
},
'headers' => bless(
{

'headers' => {

'accept-encoding' => [

'gzip'

],

'content-type' => [

'mulitpart/mixed'

],

'authorization' => [

'Bearer REAL_TOKEN_HERE'

],

'user-agent' => [

'Mojolicious (Perl)'

]

}

}, 'Mojo::Headers' )
},
'Mojo::Content::MultiPart' ),
'url' => bless( {
'host' =>
'www.googleapis.com',
'path' => bless( {

'path' => '/batch/gmail/v1'
},
'Mojo::Path' ),
'scheme' => 'https'
}, 'Mojo::URL' )
}, 'Mojo::Message::Request' )
}, 'Mojo::Transaction::HTTP' );

This object gets called with
my $res = $self->ua->start($tx)->res;

The $res contains the 400 error from google with no explanation.
Post by Dan Book
You want the multipart content generator. Scroll down to the very last
example in https://metacpan.org/pod/Mojo::UserAgent::Transactor#tx
-Dan
Post by Steve Dondley
I'm trying to hack a perl module that uses mojolicious to make api calls
https://developers.google.com/gmail/api/guides/batch
I've made various attempts but no calls are resulting in a successful
response. Here's what I'm currently trying.
my $options =
[
{
content => 'GET /gmail/v1/users/
'Content-Type' => 'application/http',
},
{
content => 'GET /gmail/v1/users/
'Content-Type' => 'application/http',
}
];
my $path = 'https://www.googleapis.com/batch/' . $s->api . '/' . $s
->version;
my $headers = {'Authorization' => 'Bearer TOKEN_HERE'};
$tx = $self->ua->build_tx(
uc $http_method => $path => $headers => json =>
$optional_data );
$VAR1 = bless( {
'req' => bless( {
'content' => bless( {
'headers' =>
bless( {
'headers' => {
'content-type' => [
'application/json'
],
'authorization' => [
'Bearer TOKEN_HERE'
],
'accept-encoding' => [
'gzip'
],
'user-agent' => [
'Mojolicious (Perl)'
]
}
}, 'Mojo::Headers' ),
'events' => {
'read' => [
sub { "DUMMY" }
]
},
'read' => $VAR1
->{'req'}{'content'}{'events'}{'read'}[0],
'asset' => bless
( {
'content' => '[{"Content-Type":"application\\/http","content":"GET
\\/gmail\\/v1\\/u
es\\/165e94ce49900df2"}]'
}, 'Mojo::Asset::Memory' )
},
'Mojo::Content::Single' ),
'url' => bless( {
'scheme' => 'https',
'host' => '
www.googleapis.com',
'path' => bless( {
'path' => '/batch/gmail/v1'
},
'Mojo::Path' )
}, 'Mojo::URL' ),
'method' => 'POST'
}, 'Mojo::Message::Request' )
}, 'Mojo::Transaction::HTTP' );
Use of uninitialized value $cb in method lookup
Fiddling with the content type settings doesn't help make this go away.
--
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
<javascript:>.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.
--
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 mojolicious+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.
Loading...