]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/support-scripts/oils_header.pl
added a 'no warnings' for cleaner output
[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::Logger qw/:logger/;
19
20
21 # Some useful objects
22 our $apputils = "OpenILS::Application::AppUtils";
23 our $memcache;
24 our $user;
25 our $authtoken;
26 our $authtime;
27
28 # Some constants for our services
29 our $AUTH               = 'open-ils.auth';
30 our $STORAGE    = 'open-ils.storage';
31 our $SEARCH             = 'open-ils.search';
32 our $CIRC               = 'open-ils.circ';
33 our $CAT                        = 'open-ils.cat';
34 our $MATH               = 'opensrf.math';
35 our $SETTINGS   = 'opensrf.settings';
36 our $ACTOR              = 'open-ils.actor';
37
38
39 #----------------------------------------------------------------
40 # Exit a script
41 #----------------------------------------------------------------
42 sub err { 
43         my ($pkg, $file, $line, $sub)  = _caller(); 
44         no warnings;
45         die "Script halted with error ".
46                 "($pkg : $file : $line : $sub):\n" . shift() . "\n"; 
47 }
48
49 #----------------------------------------------------------------
50 # Print with newline
51 #----------------------------------------------------------------
52 sub printl { print "@_\n"; }
53
54 #----------------------------------------------------------------
55 # Print with Data::Dumper
56 #----------------------------------------------------------------
57 sub debug { printl(Dumper(@_)); }
58
59
60 #----------------------------------------------------------------
61 # This is not the function you're looking for
62 #----------------------------------------------------------------
63 sub _caller {
64         my ($pkg, $file, $line, $sub)  = caller(2);
65         if(!$line) {
66                 ($pkg, $file, $line)  = caller(1);
67                 $sub = "";
68         }
69         return ($pkg, $file, $line, $sub);
70 }
71
72
73 #----------------------------------------------------------------
74 # Connect to the servers
75 #----------------------------------------------------------------
76 sub osrf_connect {
77         my $config = shift;
78         err("Bootstrap config required") unless $config;
79         OpenSRF::System->bootstrap_client( config_file => $config );
80 }
81
82 #----------------------------------------------------------------
83 # Get a handle for the memcache object
84 #----------------------------------------------------------------
85 sub osrf_cache { 
86         eval 'use OpenSRF::Utils::Cache;';
87         $memcache = OpenSRF::Utils::Cache->new('global') unless $memcache;
88         return $memcache;
89 }
90
91 #----------------------------------------------------------------
92 # Is the given object an OILS event?
93 #----------------------------------------------------------------
94 sub oils_is_event {
95         my $e = shift;
96         if( $e and ref($e) eq 'HASH' ) {
97                 return 1 if defined($e->{ilsevent});
98         }
99         return 0;       
100 }
101
102 #----------------------------------------------------------------
103 # If the given object is an event, this prints the event info 
104 # and exits the script
105 #----------------------------------------------------------------
106 sub oils_event_die {
107         my $evt = shift;
108         my ($pkg, $file, $line, $sub)  = _caller();
109         if(oils_is_event($evt)) {
110                 if($evt->{ilsevent}) {
111                         printl("\nReceived Event($pkg : $file : $line : $sub): \n" . Dumper($evt));
112                         exit 1;
113                 }
114         }
115 }
116
117
118 #----------------------------------------------------------------
119 # Login to the auth server and set the global $authtoken var
120 #----------------------------------------------------------------
121 sub oils_login {
122         my( $username, $password ) = @_;
123
124         my $seed = $apputils->simplereq( $AUTH, 
125                 'open-ils.auth.authenticate.init', $username );
126         err("No auth seed") unless $seed;
127
128         my $response = $apputils->simplereq( $AUTH, 
129                 'open-ils.auth.authenticate.complete', $username, 
130                 md5_hex($seed . md5_hex($password)));
131         err("No auth response returned on login") unless $response;
132
133         oils_event_die($response);
134
135         $authtime = $response->{payload}->{authtime};
136         $authtoken = $response->{payload}->{authtoken};
137         return $authtoken;
138 }
139
140 #----------------------------------------------------------------
141 # Fetches the user object and sets the global $user var
142 #----------------------------------------------------------------
143 sub oils_fetch_session {
144         my $ses = shift;
145         my $resp = $apputils->simplereq( $AUTH, 
146                 'open-ils.auth.session.retrieve', $ses, 'staff' );
147         oils_event_die($resp);
148         return $user = $resp;
149 }
150
151 #----------------------------------------------------------------
152 # var $response = simplereq( $service, $method, @params );
153 #----------------------------------------------------------------
154 sub simplereq { return $apputils->simplereq(@_); }
155
156
157 1;