my $self = shift;
my $req = shift;
my $session_id = shift;
+ my $do_not_redirect = shift;
my $uri;
if (!defined ($req->{uri})) {
$uri,
$headers,
$content,
- $request_timeout
+ $request_timeout,
+ undef,
+ $do_not_redirect
);
if (!defined ($res)) {
$logger->error('EbookAPI: no HTTP response received');
return {
is_success => $res->is_success,
status => $res->status_line,
- content => OpenSRF::Utils::JSON->JSON2perl($res->decoded_content)
+ content => OpenSRF::Utils::JSON->JSON2perl($res->decoded_content),
+ location => $res->header('Location')
};
}
}
sub handle_http_request {
my $self = shift;
my $req = shift;
+ my $session_id = shift;
+ my $do_not_redirect = shift;
# Prep our request using defaults.
$req->{method} = 'GET' if (!$req->{method});
$req = $self->set_http_headers($req);
# Send the request.
- my $res = $self->request($req, $self->{session_id});
+ my $res = $self->request($req, $self->{session_id}, $do_not_redirect);
$logger->info("EbookAPI: raw OverDrive HTTP response: " . Dumper $res);
# Now we can update our headers with our fresh client/patron tokens
# and re-send our request.
$req = $self->set_http_headers($req);
- return $self->request($req, $self->{session_id});
+ return $self->request($req, $self->{session_id}, $do_not_redirect);
}
# For any non-401 response (including no response at all),
}
$checkout->{formats} = $formats;
}
+ if ($res->{content}->{links}->{downloadRedirect}->{href}) {
+ my $redir = $res->{content}->{links}->{downloadRedirect}->{href};
+ my $req2 = {
+ method => 'GET',
+ uri => $redir
+ };
+ if (my $res2 = $self->handle_http_request($req2, $self->{session_id}, 1)) {
+ if ($res2->{location}) {
+ $checkout->{download_redirect} = $res2->{location};
+ }
+ }
+ }
return $checkout;
}
$logger->error("EbookAPI: checkout failed for OverDrive title $title_id");
my $ftype = $f->{formatType};
$formats->{$ftype} = $f->{linkTemplates}->{downloadLink}->{href};
};
+ my $download_redirect = '';
+ my $redirect = $checkout->{links}->{downloadRedirect}->{href};
+ if ($redirect) {
+ my $req2 = {
+ method => 'GET',
+ uri => $redirect
+ };
+ if (my $res2 = $self->handle_http_request($req2, $self->{session_id}, 1)) {
+ if ($res2->{location}) {
+ $download_redirect = $res2->{location};
+ }
+ }
+ }
push @$checkouts, {
title_id => $title_id,
due_date => $checkout->{expires},
title => $title_info->{title},
author => $title_info->{author},
- formats => $formats
+ formats => $formats,
+ download_redirect => $download_redirect
}
};
$self->{checkouts} = $checkouts;
# Use $res->content to get response content.
#
sub request {
- my ($self, $method, $uri, $headers, $content, $request_timeout, $useragent) = @_;
+ my ($self, $method, $uri, $headers, $content, $request_timeout, $useragent, $do_not_redirect) = @_;
my $ua = new LWP::UserAgent;
$request_timeout = $request_timeout || $self->{default_timeout} || 60;
$useragent = $useragent || $self->{useragent} || 'SameOrigin/1.0';
$ua->agent($useragent);
+ if ($do_not_redirect) {
+ $ua->requests_redirectable([]);
+ }
my $h = HTTP::Headers->new();
foreach my $k (keys %$headers) {
if (x.download_url) {
dl_td.innerHTML = '<a href="' + x.download_url + '">' + l_strings.download + '</a>';
}
- if (x.formats) {
+ if (x.download_redirect) {
+ dl_td.innerHTML = '<a target="_blank" href="' + x.download_redirect + '">' + l_strings.download + '</a>';
+ } else if (x.formats) {
var select = dojo.create("select", { id: "download-format" }, dl_td);
for (f in x.formats) {
dojo.create("option", { value: x.formats[f], innerHTML: f }, select);
new_xact.download_url = resp.download_url;
dojo.create("a", { href: new_xact.download_url, innerHTML: l_strings.download }, dojo.byId('checkout-button-td'));
new_xact.finish();
+ } else if (resp.download_redirect) {
+ // Use download URL from checkout response, if available.
+ new_xact.download_redirect = resp.download_redirect;
+ dojo.create("a", { target: "_blank", href: new_xact.download_redirect, innerHTML: l_strings.download }, dojo.byId('checkout-button-td'));
+ new_xact.finish();
} else if (typeof resp.formats !== 'undefined') {
// User must select download format from list of options.
var select = dojo.create("select", { id: "download-format" }, dojo.byId('checkout-button-td'));