]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/support-scripts/oils_header.pl
added fieldmapper IDL import
[Evergreen.git] / Open-ILS / src / support-scripts / oils_header.pl
1 #!/usr/bin/perl
2
3 #----------------------------------------------------------------
4 # Generic header for tesing OpenSRF methods
5 #----------------------------------------------------------------
6
7 use strict;
8 use warnings;
9 use JSON;
10 use Data::Dumper;
11 use OpenSRF::System;
12 use OpenSRF::AppSession;
13 use OpenSRF::EX qw(:try);
14 use Time::HiRes qw/time/;
15 use Digest::MD5 qw(md5_hex);
16 use OpenILS::Utils::Fieldmapper;
17 use OpenILS::Application::AppUtils;
18 use OpenSRF::Utils::SettingsClient;
19 use OpenSRF::Utils::Logger qw/:logger/;
20
21
22 # Some useful objects
23 our $apputils = "OpenILS::Application::AppUtils";
24 our $memcache;
25 our $user;
26 our $authtoken;
27 our $authtime;
28
29 # Some constants for our services
30 our $AUTH               = 'open-ils.auth';
31 our $STORAGE    = 'open-ils.storage';
32 our $SEARCH             = 'open-ils.search';
33 our $CIRC               = 'open-ils.circ';
34 our $CAT                        = 'open-ils.cat';
35 our $MATH               = 'opensrf.math';
36 our $SETTINGS   = 'opensrf.settings';
37 our $ACTOR              = 'open-ils.actor';
38
39 sub AUTH                { return $AUTH; }
40 sub STORAGE { return $STORAGE; }
41 sub SEARCH      { return $SEARCH; }
42 sub CIRC                { return $CIRC; }
43 sub CAT         { return $CAT; }
44 sub MATH                { return $MATH; }
45 sub SETTINGS { return $SETTINGS; }
46 sub ACTOR       { return $ACTOR; }
47
48
49 #----------------------------------------------------------------
50 # Exit a script
51 #----------------------------------------------------------------
52 sub err { 
53         my ($pkg, $file, $line, $sub)  = _caller(); 
54         no warnings;
55         die "Script halted with error ".
56                 "($pkg : $file : $line : $sub):\n" . shift() . "\n"; 
57 }
58
59 #----------------------------------------------------------------
60 # Print with newline
61 #----------------------------------------------------------------
62 sub printl { print "@_\n"; }
63
64 #----------------------------------------------------------------
65 # Print with Data::Dumper
66 #----------------------------------------------------------------
67 sub debug { printl(Dumper(@_)); }
68
69
70 #----------------------------------------------------------------
71 # This is not the function you're looking for
72 #----------------------------------------------------------------
73 sub _caller {
74         my ($pkg, $file, $line, $sub)  = caller(2);
75         if(!$line) {
76                 ($pkg, $file, $line)  = caller(1);
77                 $sub = "";
78         }
79         return ($pkg, $file, $line, $sub);
80 }
81
82
83 #----------------------------------------------------------------
84 # Connect to the servers
85 #----------------------------------------------------------------
86 sub osrf_connect {
87         my $config = shift;
88         err("Bootstrap config required") unless $config;
89         OpenSRF::System->bootstrap_client( config_file => $config );
90         Fieldmapper->import(IDL =>
91                 OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
92
93                 
94 }
95
96 #----------------------------------------------------------------
97 # Get a handle for the memcache object
98 #----------------------------------------------------------------
99 sub osrf_cache { 
100         eval 'use OpenSRF::Utils::Cache;';
101         $memcache = OpenSRF::Utils::Cache->new('global') unless $memcache;
102         return $memcache;
103 }
104
105 #----------------------------------------------------------------
106 # Is the given object an OILS event?
107 #----------------------------------------------------------------
108 sub oils_is_event {
109         my $e = shift;
110         if( $e and ref($e) eq 'HASH' ) {
111                 return 1 if defined($e->{ilsevent});
112         }
113         return 0;       
114 }
115
116 sub oils_event_equals {
117         my( $e, $name ) = @_;
118         return 1 if (oils_is_event($e) and ($e->{textcode} eq $name));
119         return 0;
120 }
121
122 #----------------------------------------------------------------
123 # If the given object is an event, this prints the event info 
124 # and exits the script
125 #----------------------------------------------------------------
126 sub oils_event_die {
127         my $evt = shift;
128         my ($pkg, $file, $line, $sub)  = _caller();
129         if(oils_is_event($evt)) {
130                 if($evt->{ilsevent}) {
131                         printl("\nReceived Event($pkg : $file : $line : $sub): \n" . Dumper($evt));
132                         exit 1;
133                 }
134         }
135 }
136
137
138 #----------------------------------------------------------------
139 # Login to the auth server and set the global $authtoken var
140 #----------------------------------------------------------------
141 sub oils_login {
142         my( $username, $password, $type ) = @_;
143
144         $type |= "staff";
145
146         my $seed = $apputils->simplereq( $AUTH, 
147                 'open-ils.auth.authenticate.init', $username );
148         err("No auth seed") unless $seed;
149
150         my $response = $apputils->simplereq( $AUTH, 
151                 'open-ils.auth.authenticate.complete', 
152                 {       username => $username, 
153                         password => md5_hex($seed . md5_hex($password)), 
154                         type => $type });
155
156         err("No auth response returned on login") unless $response;
157
158         oils_event_die($response);
159
160         $authtime = $response->{payload}->{authtime};
161         $authtoken = $response->{payload}->{authtoken};
162         return $authtoken;
163 }
164
165
166 #----------------------------------------------------------------
167 # Destroys the login session on the server
168 #----------------------------------------------------------------
169 sub oils_logout {
170         $apputils->simplereq(
171                 'open-ils.auth',
172                 'open-ils.auth.session.delete', $authtoken );
173 }
174
175 #----------------------------------------------------------------
176 # Fetches the user object and sets the global $user var
177 #----------------------------------------------------------------
178 sub oils_fetch_session {
179         my $ses = shift;
180         my $resp = $apputils->simplereq( $AUTH, 
181                 'open-ils.auth.session.retrieve', $ses, 'staff' );
182         oils_event_die($resp);
183         return $user = $resp;
184 }
185
186 #----------------------------------------------------------------
187 # var $response = simplereq( $service, $method, @params );
188 #----------------------------------------------------------------
189 sub simplereq { return $apputils->simplereq(@_); }
190 sub osrf_request { return $apputils->simplereq(@_); }
191
192 1;