]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/python/osrf/apps/example.py
Lp 1860068: Clarify README for Debian Buster
[OpenSRF.git] / src / python / osrf / apps / example.py
1 # -----------------------------------------------------------------------
2 # Copyright (C) 2008  Equinox Software, Inc.
3 # Bill Erickson <erickson@esilibrary.com>
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  
18 # 02110-1301, USA
19 # -----------------------------------------------------------------------
20 import os
21 import osrf.log
22 from osrf.app import Application
23
24 class Example(Application):
25     ''' Example OpenSRF application. '''
26
27     # ---------------------------------------------------------
28     # Register a new method for this application
29     # ---------------------------------------------------------
30     Application.register_method(
31         api_name = 'opensrf.py-example.reverse', # published API name for the method
32         method = 'reverse', # name of def that implements this method
33         argc = 1, # expects a single argument
34         stream = True # returns a stream of results.  can be called atomic-ly
35     )
36
37     # ---------------------------------------------------------
38     # This method implements the API call registered above
39     # ---------------------------------------------------------
40     def reverse(self, request, message=''):
41         ''' Returns the given string in reverse order one character at a time
42             @param type:string Message to reverse 
43             @return type:string The reversed message, one character at a time.  '''
44         idx = len(message) - 1
45         while idx >= 0:
46             request.respond(message[idx])
47             idx -= 1
48
49     # ---------------------------------------------------------
50     # Session data test
51     # ---------------------------------------------------------
52
53     Application.register_method(
54         api_name = 'opensrf.stateful_session_test',
55         method = 'session_test',
56         argc = 0
57     )
58
59     def session_test(self, request):
60         c = request.session.session_data.get('counter', 0) + 1
61         request.session.session_data['counter'] = c
62         return c
63
64     # ---------------------------------------------------------
65     # Session callbacks test
66     # ---------------------------------------------------------
67     Application.register_method(
68         api_name = 'opensrf.session_callback_test',
69         method = 'callback_test',
70         argc = 0
71     )
72
73     def callback_test(self, request):
74         
75         def pre_req_cb(ses):
76             osrf.log.log_info("running pre_request callback")
77
78         def post_req_cb(ses):
79             osrf.log.log_info("running post_request callback")
80
81         def disconnect_cb(ses):
82             osrf.log.log_info("running disconnect callback")
83
84         def death_cb(ses):
85             osrf.log.log_info("running death callback")
86
87         ses = request.session
88
89         ses.register_callback('pre_request', pre_req_cb)
90         ses.register_callback('post_request', post_req_cb)
91         ses.register_callback('disconnect', disconnect_cb)
92         ses.register_callback('death', death_cb)
93
94         c = ses.session_data.get('counter', 0) + 1
95         ses.session_data['counter'] = c
96         return c
97
98
99     # ---------------------------------------------------------
100     # These example methods override methods from 
101     # osrf.app.Application.  They are not required.
102     # ---------------------------------------------------------
103     def global_init(self):
104         osrf.log.log_debug("Running global init handler for %s" % __name__)
105
106     def child_init(self):
107         osrf.log.log_debug("Running child init handler for process %d" % os.getpid())
108
109     def child_exit(self):
110         osrf.log.log_debug("Running child exit handler for process %d" % os.getpid())
111
112
113 # ---------------------------------------------------------
114 # Now register an instance of this class as an application
115 # ---------------------------------------------------------
116 Application.register_app(Example())
117
118