]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/perl/lib/OpenSRF/Application/Demo/Math.pm
LP#1631522: include example of ->dispatch in example app
[OpenSRF.git] / src / perl / lib / OpenSRF / Application / Demo / Math.pm
1 package OpenSRF::Application::Demo::Math;
2 use base qw/OpenSRF::Application/;
3 use OpenSRF::Application;
4 use OpenSRF::Utils::Logger qw/:level/;
5 use OpenSRF::DomainObject::oilsResponse;
6 use OpenSRF::EX qw/:try/;
7 use strict;
8 use warnings;
9
10
11 sub DESTROY{}
12
13 our $log = 'OpenSRF::Utils::Logger';
14
15 sub send_request {
16         my $self = shift;
17         my $client = shift;
18
19         my $method_name = shift;
20         my @params = @_;
21
22         my $session = OpenSRF::AppSession->create( "opensrf.dbmath" );
23         my $request = $session->request( "$method_name", @params );
24         my $response = $request->recv();
25         if(!$response) { return undef; }
26         if($response->isa("Error")) {throw $response ($response->stringify);}
27         $session->finish();
28
29         return $response->content;
30
31 }
32 __PACKAGE__->register_method( method => 'send_request', api_name => '_send_request' );
33
34 __PACKAGE__->register_method( method => 'add_1', api_name => 'add' );
35 sub add_1 {
36         my $self = shift;
37         my $client = shift;
38         my @args = @_;
39
40         # use ->dispatch rather than run; results of the delegated
41         # method will be directly passed to the caller
42         return $self->method_lookup('_send_request')->dispatch('add', @args);
43 }
44
45 __PACKAGE__->register_method( method => 'sub_1', api_name => 'sub' );
46 sub sub_1 {
47         my $self = shift;
48         my $client = shift;
49         my @args = @_;
50
51         my $meth = $self->method_lookup('_send_request');
52         my ($result) = $meth->run('sub',@args);
53
54         return $result;
55 }
56
57 __PACKAGE__->register_method( method => 'mult_1', api_name => 'mult' );
58 sub mult_1 {
59         my $self = shift;
60         my $client = shift;
61         my @args = @_;
62
63         my $meth = $self->method_lookup('_send_request');
64         my ($result) = $meth->run('mult',@args);
65
66         return $result;
67 }
68
69 __PACKAGE__->register_method( method => 'div_1', api_name => 'div' );
70 sub div_1 {
71         my $self = shift;
72         my $client = shift;
73         my @args = @_;
74
75         my $meth = $self->method_lookup('_send_request');
76         my ($result) = $meth->run('div',@args);
77
78         return $result;
79 }
80
81
82 1;