]> git.koha-community.org Git - koha.git/blob - Koha/SharedContent.pm
Bug 17047: subscriptions management with Mana-KB
[koha.git] / Koha / SharedContent.pm
1 package Koha::SharedContent;
2
3 # Copyright 2016 BibLibre Morgane Alonso
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use Modern::Perl;
21 use JSON;
22 use HTTP::Request;
23 use LWP::UserAgent;
24
25 our $MANA_IP = "http://10.25.159.107:5000";
26
27 sub manaRequest {
28     my $mana_request = shift;
29     my $result;
30
31     $mana_request->content_type('application/json');
32     my $userAgent = LWP::UserAgent->new;
33     my $response  = $userAgent->request($mana_request);
34
35     if ( $response->code ne "204" ) {
36         $result = from_json( $response->decoded_content );
37     }
38     $result->{code} = $response->code;
39
40     return $result if ( $response->code =~ /^2..$/ );
41 }
42
43 sub manaNewUserPatchRequest {
44     my $resource = shift;
45     my $id       = shift;
46
47     my $url = "$MANA_IP/$resource/$id.json/newUser";
48     my $request = HTTP::Request->new( PATCH => $url );
49
50     return manaRequest($request);
51 }
52
53 sub manaPostRequest {
54     my $resource = shift;
55     my $content  = shift;
56
57     my $url = "$MANA_IP/$resource.json";
58     my $request = HTTP::Request->new( POST => $url );
59
60     $content->{bulk_import} = 0;
61     my $json = to_json( $content, { utf8 => 1 } );
62     $request->content($json);
63
64     return manaRequest($request);
65 }
66
67 sub manaGetRequestWithId {
68     my $resource = shift;
69     my $id       = shift;
70
71     my $url = "$MANA_IP/$resource/$id.json";
72     my $request = HTTP::Request->new( GET => $url );
73
74     return manaRequest($request);
75 }
76
77 sub manaGetRequest {
78     my $resource   = shift;
79     my $parameters = shift;
80
81     $parameters = join '&',
82       map { defined $parameters->{$_} ? $_ . "=" . $parameters->{$_} : () }
83       keys %$parameters;
84     my $url = "$MANA_IP/$resource.json?$parameters";
85     my $request = HTTP::Request->new( GET => $url );
86
87     return manaRequest($request);
88 }
89
90 1;