Bug 21468: (QA follow-up) Simplify payload
[koha.git] / Koha / SharedContent.pm
blob7082fe03a3424a6da7a9f456cf87cd654c16f7fc
1 package Koha::SharedContent;
3 # Copyright 2016 BibLibre Morgane Alonso
5 # This file is part of Koha.
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.
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.
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>.
20 use Modern::Perl;
21 use JSON;
22 use HTTP::Request;
23 use LWP::UserAgent;
25 use Koha::Serials;
26 use Koha::Reports;
27 use C4::Context;
29 =head1 NAME
31 Koha::SharedContent - Set of methods for querying Mana KB server
33 =head1 DESCRIPTION
35 Package for accessing shared content via Mana KB. Methods here are intended
36 to build and process queries for requesting from Mana KB server.
38 =cut
40 =head2 process_request
42 Koha::SharedContent::process_request($request);
44 Send a request to Mana KB server. URL is defined in koha-conf.xml in mana_config
45 tag. $request parameter must be a HTTP::Request object. See build_request method.
47 =cut
49 sub process_request {
50 my $mana_request = shift;
51 my $result;
52 $mana_request->content_type('application/json');
53 my $userAgent = LWP::UserAgent->new;
54 if ( $mana_request->method eq "POST" ){
55 my $content;
56 if ($mana_request->content) {$content = from_json( $mana_request->content )};
57 $content->{securitytoken} = C4::Context->preference("ManaToken");
58 $mana_request->content( to_json($content) );
61 my $response = $userAgent->simple_request($mana_request);
62 eval { $result = from_json( $response->decoded_content, { utf8 => 1} ); };
63 $result->{code} = $response->code;
64 if ( $@ ){
65 $result->{msg} = $@;
67 if ($response->is_error){
68 $result->{msg} = "An error occurred, mana server returned: " . $response->message;
70 return $result ;
73 =head2 increment_entity_value
75 Koha::SharedContent::increment_entity_value($entity_type, $mana_entity_id, $field);
77 Increment by 1 the field $field of a Mana entity. I.e, this is used to count the number
78 of Koha instances using a specific entity.
80 =cut
82 sub increment_entity_value {
83 return process_request(build_request('increment', @_));
86 =head2 send_entity
88 my $result = Koha::SharedContent::send_entity($language, $borrowernumber, $mana_entity_id, $entity_type);
90 Share a Koha entity (i.e subscription or report) to Mana KB.
92 =cut
94 sub send_entity {
95 my ($lang, $loggedinuser, $resourceid, $resourcetype) = @_;
97 my $content = prepare_entity_data($lang, $loggedinuser, $resourceid, $resourcetype);
99 my $result = process_request(build_request('post', $resourcetype, $content));
101 if ( $result and ($result->{code} eq "200" or $result->{code} eq "201") ) {
102 my $packages = "Koha::".ucfirst($resourcetype)."s";
103 my $resource = $packages->find($resourceid);
104 eval { $resource->set( { mana_id => $result->{id} } )->store };
106 return $result;
109 =head3 comment_entity
111 my $result = Koha::SharedContent::comment_entity($resource_id, $resource_type, $comment);
113 Send a comment about a Mana entity.
115 =cut
117 sub comment_entity {
118 my ($resourceid, $resourcetype, $comment) = @_;
120 my $result = process_request(build_request('post', 'resource_comment',
121 { resource_id => $resourceid, resource_type => $resourcetype, message => $comment }));
123 return $result;
126 =head2 prepare_entity_data
128 $data = prepare_entity_data($language, $borrowernumber, $mana_entity_id, $entity_type);
130 Prepare Koha entity data to be sent to Mana KB.
132 =cut
134 sub prepare_entity_data {
135 my ($lang, $loggedinuser, $ressourceid, $ressourcetype) = @_;
136 $lang ||= C4::Context->preference('language');
138 my $mana_email;
139 if ( $loggedinuser ne 0 ) {
140 my $borrower = Koha::Patrons->find($loggedinuser);
141 $mana_email = $borrower->first_valid_email_address
142 || Koha::Libraries->find( C4::Context->userenv->{'branch'} )->branchemail
144 $mana_email = C4::Context->preference('KohaAdminEmailAddress')
145 if ( ( not defined($mana_email) ) or ( $mana_email eq '' ) );
147 my %versions = C4::Context::get_versions();
149 my $mana_info = {
150 language => $lang,
151 kohaversion => $versions{'kohaVersion'},
152 exportemail => $mana_email
155 my $ressource_mana_info;
156 my $packages = "Koha::".ucfirst($ressourcetype)."s";
157 my $package = "Koha::".ucfirst($ressourcetype);
158 $ressource_mana_info = $package->get_sharable_info($ressourceid);
159 $ressource_mana_info = { %$ressource_mana_info, %$mana_info };
161 return $ressource_mana_info;
164 =head2 get_entity_by_id
166 my $entity = Koha::SharedContent::get_entity_by_id($entity_type, $mana_entity_id, [{usecomments => 1}]);
168 Retrieve a Mana entity to be imported into Koha. Add {usecomments => 1} to tell Mana to
169 embed all user reviews.
171 =cut
173 sub get_entity_by_id {
174 return process_request(build_request('getwithid', @_));
177 =head2 search_entities
179 my $result = Koha::SharedContent::search_entities( $entity_type, $search_params );
180 my $entities = $result->{data};
182 Search entities on ManaKB.
184 =cut
186 sub search_entities {
187 return process_request(build_request('get', @_));
190 =head2 build_request
192 $request = build_request($mana_method, [$param1, $param2, ...]);
194 Create a HTTP::Request object to be passed to process_request.
196 =cut
198 sub build_request {
199 my $type = shift;
200 my $resource = shift;
201 my $mana_url = get_sharing_url();
203 if ( $type eq 'get' ) {
204 my $params = shift;
205 $params = join '&',
206 map { defined $params->{$_} && $params->{$_} ne '' ? $_ . "=" . $params->{$_} : () }
207 keys %$params;
208 my $url = "$mana_url/$resource.json?$params";
209 return HTTP::Request->new( GET => $url );
212 if ( $type eq 'getwithid' ) {
213 my $id = shift;
214 my $params = shift;
215 $params = join '&',
216 map { defined $params->{$_} && $params->{$_} ne '' ? $_ . "=" . $params->{$_} : () }
217 keys %$params;
219 my $url = "$mana_url/$resource/$id.json?$params";
220 return HTTP::Request->new( GET => $url );
223 if ( $type eq 'post' ) {
224 my $content = shift;
226 my $url = "$mana_url/$resource.json";
227 my $request = HTTP::Request->new( POST => $url );
229 my $json = to_json( $content, { utf8 => 1 } );
230 $request->content($json);
232 return $request;
235 if ( $type eq 'increment' ) {
236 my $id = shift;
237 my $field = shift;
238 my $step = shift;
239 my $param;
241 $param->{step} = $step || 1;
242 $param->{id} = $id;
243 $param->{resource} = $resource;
244 $param = join '&',
245 map { defined $param->{$_} ? $_ . "=" . $param->{$_} : () }
246 keys %$param;
247 my $url = "$mana_url/$resource/$id.json/increment/$field?$param";
248 my $request = HTTP::Request->new( POST => $url );
253 =head2 get_sharing_url
255 my $mana_url = get_sharing_url();
257 Get the Mana KB server URL set in koha config file.
259 =cut
261 sub get_sharing_url {
262 return C4::Context->config('mana_config');