our
$VERSION
=
'0.004'
;
use
JSON
qw(decode_json)
;
WebServiceValidSignURI
WebServiceValidSignAuthModule
)
;
requires
qw(action_endpoint)
;
has
json
=> (
is
=>
'ro'
,
builder
=> 1,
lazy
=> 1,
);
sub
_build_json {
return
JSON::XS->new->convert_blessed;
}
has
auth
=> (
is
=>
'ro'
,
required
=> 1,
isa
=> WebServiceValidSignAuthModule,
);
sub
get_endpoint {
my
(
$self
,
@misc
) =
@_
;
my
$uri
=
$self
->endpoint->clone;
my
@path
=
$uri
->path_segments;
@path
=
grep
{
defined
$_
&&
length
$_
}
@path
,
@misc
;
$uri
->path_segments(
@path
);
return
$uri
;
}
sub
download_file {
my
(
$self
,
$uri
) =
@_
;
my
$fh
= File::Temp->new();
my
$request
= HTTP::Request->new(
GET
=>
$uri
,
[
'Content-Type'
=>
'application/json'
,
]
);
$self
->call_api_download(
$request
,
sub
{
my
(
$chunk
,
$res
,
$proto
) =
@_
;
print
$fh
$chunk
;
}
);
$fh
->
seek
(0,0);
return
$fh
;
}
sub
call_api_download {
my
(
$self
,
$req
,
@opts
) =
@_
;
my
$res
=
$self
->lwp->request(
$req
,
@opts
);
if
(!
$res
->is_success) {
my
$msg
=
join
(
"$/"
,
""
,
$req
->as_string,
$res
->as_string);
my
$apikey
=
$self
->secret;
$msg
=~ s/
$apikey
/APIKEYHIDDEN/g;
croak(
"ValidSign error: $msg"
);
}
my
$headers
=
$res
->headers;
foreach
(
qw(x-died client-aborted)
) {
if
(
my
$header
=
$headers
->header(
$_
)) {
my
$msg
=
join
(
"$/"
,
""
,
$req
->as_string,
$res
->as_string);
my
$apikey
=
$self
->secret;
$msg
=~ s/
$apikey
/APIKEYHIDDEN/g;
die
sprintf
(
"%s: Unable to complete file download"
,
$_
);
}
}
return
1;
}
sub
call_api {
my
(
$self
,
$req
,
%options
) =
@_
;
if
(
$options
{with_token} &&
$self
->can(
'auth'
)) {
$req
->header(
"Authentication"
,
$self
->auth->token);
}
my
$res
=
$self
->lwp->request(
$req
);
return
decode_json(
$res
->decoded_content)
if
$res
->is_success;
my
$msg
=
join
(
"$/"
,
""
,
$req
->as_string,
$res
->as_string);
my
$apikey
=
$self
->secret;
$msg
=~ s/
$apikey
/APIKEYHIDDEN/g;
croak(
"ValidSign error: $msg"
);
}
1;