a1682930d0437f1b35c8853511ef4741645e88b2
[srvgit] / C4 / Search / PazPar2.pm
1 package C4::Search::PazPar2;
2
3 # Copyright (C) 2007 LibLime
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use LWP::UserAgent;
23 use URI;
24 use URI::QueryParam;
25 use XML::Simple;
26
27 =head1 NAME
28
29 C4::Search::PazPar2 - implement client for PazPar2
30
31 [Note: may rename to Net::PazPar2 or somesuch if decide to put on CPAN separate
32  from Koha]
33
34 =head1 SYNOPSIS
35
36 =cut
37
38 =head1 DESCRIPTION
39
40 =cut
41
42 sub new {
43     my $class = shift;
44     my $endpoint = shift;
45
46     my $self = {};
47     $self->{'endpoint'} = $endpoint;
48     $self->{'session'} = '';
49     $self->{'ua'} = LWP::UserAgent->new;
50     bless $self, $class;
51
52     return $self;
53 }
54
55 sub init {
56     my $self = shift;
57
58     my $uri = URI->new($self->{'endpoint'});
59     $uri->query_param(command => 'init');
60     my $response = $self->{'ua'}->get($uri);
61     if ($response->is_success) {
62         my $message = XMLin($response->content);
63         if ($message->{'status'} eq 'OK') {
64             $self->{'session'} = $message->{'session'};
65         }
66     } else {
67         warn $response->status_line;
68     }
69 }
70
71 sub search {
72     my $self = shift;
73     my $query = shift;
74
75     my $uri = URI->new($self->{'endpoint'});
76     $uri->query_param(command => 'search');
77     $uri->query_param(session => $self->{'session'});
78     $uri->query_param(query => $query);
79     my $response = $self->{'ua'}->get($uri);
80     if ($response->is_success) {
81         #print $response->content, "\n";
82     } else {
83         warn $response->status_line;
84     }
85
86 }
87
88 sub stat {
89     my $self = shift;
90
91     my $uri = URI->new($self->{'endpoint'});
92     $uri->query_param(command => 'stat');
93     $uri->query_param(session => $self->{'session'});
94     my $response = $self->{'ua'}->get($uri);
95     if ($response->is_success) {
96         return $response->content;
97     } else {
98         warn $response->status_line;
99         return;
100     }
101 }
102
103 sub show {
104     my $self = shift;
105     my $start = shift;
106     my $count = shift;
107     my $sort = shift;
108
109     my $uri = URI->new($self->{'endpoint'});
110     $uri->query_param(command => 'show');
111     $uri->query_param(start => $start);
112     $uri->query_param(num => $count);
113     $uri->query_param(block => 1);
114     $uri->query_param(session => $self->{'session'});
115     $uri->query_param(sort => $sort);
116     my $response = $self->{'ua'}->get($uri);
117     if ($response->is_success) {
118         return $response->content;
119     } else {
120         warn $response->status_line;
121         return;
122     }
123     
124 }
125
126 sub record {
127     my $self = shift;
128     my $id = shift;
129     my $offset = shift;
130
131     my $uri = URI->new($self->{'endpoint'});
132     $uri->query_param(command => 'record');
133     $uri->query_param(id => $id);
134     $uri->query_param(offset => $offset);
135     $uri->query_param(binary => 1);
136     $uri->query_param(session => $self->{'session'});
137     my $response = $self->{'ua'}->get($uri);
138     if ($response->is_success) {
139         return $response->content;
140     } else {
141         warn $response->status_line;
142         return;
143     }
144 }
145
146 sub termlist {
147     my $self = shift;
148     my $name = shift;
149
150     my $uri = URI->new($self->{'endpoint'});
151     $uri->query_param(command => 'termlist');
152     $uri->query_param(name => $name);
153     $uri->query_param(session => $self->{'session'});
154     my $response = $self->{'ua'}->get($uri);
155     if ($response->is_success) {
156         return $response->content;
157     } else {
158         warn $response->status_line;
159         return;
160     }
161
162 }
163
164 1;
165
166 =head1 AUTHOR
167
168 Koha Development Team <http://koha-community.org/>
169
170 Galen Charlton <galen.charlton@liblime.com>
171
172 =cut