blib/lib/FreeMED/Relay.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 #!/usr/bin/perl
2 #
3 # $Id: Relay.pm 71 2007-08-14 00:33:05Z jeff $
4 #
5 # Authors:
6 # Jeff Buchbinder <jeff@freemedsoftware.org>
7 #
8 # FreeMED Electronic Medical Record and Practice Management System
9 # Copyright (C) 1999-2006 FreeMED Software Foundation
10 #
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
15 #
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  
25 #
26 # FreeMED::Relay package for communicating with FreeMED 0.9.x+
27 #
28  
29 package FreeMED::Relay;
30  
31 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 0 ▶use JSON; # objToJson(), jsonToObj()
Subroutine BEGIN: called CC=1 SCAR=2.6
34 use HTTP::Cookies;
35  
36 use vars qw{ $VERSION };
37 BEGIN {
38 $VERSION = '0.2';
39 }
40  
41 sub new {
42 my $class = shift;
43 my $debug = shift;
44 my $self = {};
45 bless $self, $class;
46 $self->{'debug'} = $debug;
47 $self->_init();
48 $JSON::UnMapping = 1;
49 $JSON::QuotApos = 1;
50 $JSON::BareKey = 1;
51 return $self;
52 }
53  
54 sub set_credentials {
55 my $self = shift;
56  
57 my ( $base_uri, $username, $password ) = @_;
58 print "base_uri = $base_uri\n" if $self->{'debug'};
59 print "username = $username\n" if $self->{'debug'};
60 print "password = $password\n" if $self->{'debug'};
61 $self->{base_uri} = $base_uri;
62 $self->{username} = $username;
63 $self->{password} = $password;
64 }
65  
66 sub login {
67 my $self = shift;
68 if (!defined($self->{username}) or !defined($self->{password})) {
69 die "login(): Must set credentials before logging in\n";
70 }
71  
72 $self->_init() if (!defined($self->{ua}));
73 my $login = $self->call(
74 'org.freemedsoftware.public.Login.Validate',
75 $self->{'username'},
76 $self->{'password'},
77 );
78 $self->{'logged_in'} = true;
79 return $login;
80 }
81  
82 sub call {
83 my $self = shift;
84 my $method = shift;
85 my @params = @_;
86  
87 if (!($method =~ /public/) && !$self->{'logged_in'}) {
88 print "Must be logged in first\n" if $self->{'debug'};
89 return undef;
90 }
91  
92 my $count = 0; my %p;
93 foreach my $param (@params) {
94 print "param = '$param'\n";
95 if ( $param =~ /^HASH\(/ && $param->{'@var'} ) {
96 print "Found file upload var in $param->{'@var'}\n" if (@self->{debug});
97 # Add file transfer under @var = var, @filename = filename
98 $p{$param->{'@var'}} = [ $param->{'@filename'} ];
99 } else {
100 my $json = objToJson( $param );
101 print "param = $param, count = $count, json = $json\n" if $self->{'debug'};
102 $p{"param${count}"} = ($json ? $json : $param );
103 $count++;
104 }
105 }
106 my $res = $self->{ua}->request(
107 POST $self->{base_uri}."/relay.php/json/${method}",
108 Content_Type => 'form-data',
109 Content => [ %p ]
110 );
111 $self->{'cookie_jar'}->save();
112 print "content : ".$res->content."\n" if ($self->{debug});
113 return jsonToObj ( $res->content );
114 }
115  
116 sub _init {
117 my $self = shift;
118 $self->{'ua'} = LWP::UserAgent->new;
119 $self->{'cookie_jar'} = HTTP::Cookies->new( 'autosave' => 1 );
120 $self->{'ua'}->cookie_jar( $self->{'cookie_jar'} );
121 }
122  
123 1;
124 __END__
↑ Summary