$Astro::ADS::VERSION
=
'1.90'
;
no
warnings
'experimental'
;
my
$DEBUG
= 0;
has
ua
=> (
is
=>
'lazy'
);
has
proxy
=> (
is
=>
'lazy'
);
has
base_url
=> (
is
=>
'ro'
,
);
sub
_build_ua {
my
(
$self
) =
@_
;
return
Mojo::UserAgent->new;
}
sub
_build_proxy {
my
(
$self
,
$proxy
) =
@_
;
$self
->ua->proxy->https(
$proxy
);
return
$proxy
;
}
sub
get_response {
my
(
$self
,
$url
) =
@_
;
my
$tx
=
$self
->ua->build_tx(
GET
=>
$url
);
$tx
->req->headers->authorization(
'Bearer '
.
$ENV
{ADS_DEV_KEY} );
warn
$tx
->req->to_string
if
$DEBUG
;
try
{
$tx
=
$self
->ua->start(
$tx
) }
catch
(
$error
) {
carp
"Got this error: "
,
$error
;
}
my
$res
;
try
{
$res
=
$tx
->result }
catch
(
$error
) {
carp
"Connection error: "
,
$error
;
return
;
}
if
(
$res
->is_success) {
warn
$res
->body
if
$DEBUG
> 1 }
elsif
(
$res
->is_error) { carp
'HTTP Error: '
,
$res
->message }
elsif
(
$res
->code == 301) { carp
'Redirected: '
,
$res
->headers->location
if
$DEBUG
}
return
$res
;
}
sub
post_response {
my
(
$self
,
$url
) =
@_
;
my
$tx
=
$self
->ua->build_tx(
POST
=>
$url
);
$tx
->req->headers->authorization(
'Bearer '
.
$ENV
{ADS_DEV_KEY} );
carp
$tx
->req->to_string
if
$DEBUG
;
try
{
$tx
=
$self
->ua->start(
$tx
) }
catch
(
$error
) {
carp
"Got this error: "
,
$error
;
}
return
$tx
->result;
}
1;