File Coverage

blib/lib/WebService/Kramerius/API4.pm
Criterion Covered Total %
statement 15 57 26.3
branch 0 10 0.0
condition n/a
subroutine 5 16 31.2
pod 10 10 100.0
total 30 93 32.2


line stmt bran cond sub pod time code
1             package WebService::Kramerius::API4;
2              
3 2     2   16485 use strict;
  2         2  
  2         43  
4 2     2   6 use warnings;
  2         2  
  2         42  
5              
6 2     2   800 use Class::Utils qw(set_params);
  2         33505  
  2         32  
7 2     2   132 use Error::Pure qw(err);
  2         3  
  2         71  
8 2     2   1091 use LWP::UserAgent;
  2         70598  
  2         970  
9              
10             our $VERSION = 0.01;
11              
12             # Constructor.
13             sub new {
14 0     0 1   my ($class, @params) = @_;
15              
16             # Create object.
17 0           my $self = bless {}, $class;
18              
19             # Library URL.
20 0           $self->{'library_url'} = undef;
21              
22             # Output dispatch.
23 0           $self->{'output_dispatch'} = {};
24              
25             # Process params.
26 0           set_params($self, @params);
27              
28             # Check library URL.
29 0 0         if (! defined $self->{'library_url'}) {
30 0           err "Parameter 'library_url' is required.";
31             }
32              
33             # LWP::UserAgent.
34 0           $self->{'_ua'} = LWP::UserAgent->new;
35 0           $self->{'_ua'}->agent('WebService::Kramerius::API4/'.$VERSION);
36              
37             # Object.
38 0           return $self;
39             }
40              
41             # Get item.
42             sub get_item {
43 0     0 1   my ($self, $item_id) = @_;
44 0           return $self->_get_data($self->{'library_url'}."search/api/v5.0/item/".
45             "uuid:$item_id");
46             }
47              
48             sub get_item_children {
49 0     0 1   my ($self, $item_id) = @_;
50 0           return $self->_get_data($self->{'library_url'}."search/api/v5.0/item/".
51             "uuid:$item_id/children");
52             }
53              
54             sub get_item_siblings {
55 0     0 1   my ($self, $item_id) = @_;
56 0           return $self->_get_data($self->{'library_url'}."search/api/v5.0/item/".
57             "uuid:$item_id/siblings");
58             }
59              
60             sub get_item_streams {
61 0     0 1   my ($self, $item_id) = @_;
62 0           return $self->_get_data($self->{'library_url'}."search/api/v5.0/item/".
63             "uuid:$item_id/streams");
64             }
65              
66             sub get_item_streams_one {
67 0     0 1   my ($self, $item_id, $stream_id) = @_;
68              
69             # XXX Hack for bad content type for alto.
70 0 0         if ($stream_id eq 'alto') {
71 0           $self->{'_force_content_type'} = 'text/xml';
72             }
73              
74 0           return $self->_get_data($self->{'library_url'}."search/api/v5.0/item/".
75             "uuid:$item_id/streams/$stream_id");
76             }
77              
78             sub get_item_image {
79 0     0 1   my ($self, $item_id) = @_;
80 0           return $self->_get_data($self->{'library_url'}."search/api/v5.0/item/".
81             "uuid:$item_id/full");
82             }
83              
84             sub get_item_preview {
85 0     0 1   my ($self, $item_id) = @_;
86 0           return $self->_get_data($self->{'library_url'}."search/api/v5.0/item/".
87             "uuid:$item_id/preview");
88             }
89              
90             sub get_item_thumb {
91 0     0 1   my ($self, $item_id) = @_;
92 0           return $self->_get_data($self->{'library_url'}."search/api/v5.0/item/".
93             "uuid:$item_id/thumb");
94             }
95              
96             sub get_item_foxml {
97 0     0 1   my ($self, $item_id) = @_;
98 0           return $self->_get_data($self->{'library_url'}."search/api/v5.0/item/".
99             "uuid:$item_id/foxml");
100             }
101              
102             sub _get_data {
103 0     0     my ($self, $url) = @_;
104 0           my $req = HTTP::Request->new('GET' => $url);
105 0           my $res = $self->{'_ua'}->request($req);
106 0 0         if (! $res->is_success) {
107 0           err "Cannot get '$url' URL.";
108             }
109 0           my $content_type = $res->headers->content_type;
110              
111             # XXX Hack for forced content type.
112 0 0         if (exists $self->{'_force_content_type'}) {
113 0           $content_type = delete $self->{'_force_content_type'};
114             }
115              
116 0           my $ret = $res->content;
117 0 0         if (exists $self->{'output_dispatch'}->{$content_type}) {
118 0           $ret = $self->{'output_dispatch'}->{$content_type}->($ret);
119             }
120 0           return $ret;
121             }
122              
123             1;
124              
125             __END__