/examples/jsonrpc/JSONRPCExample.py
Python | 95 lines | 76 code | 18 blank | 1 comment | 6 complexity | 89162b67b570eb935c1ac44278675ee6 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
1from ui import RootPanel, TextArea, Label, Button, HTML, VerticalPanel, HorizontalPanel, ListBox 2from JSONService import JSONProxy 3 4class JSONRPCExample: 5 def onModuleLoad(self): 6 self.TEXT_WAITING = "Waiting for response..." 7 self.TEXT_ERROR = "Server Error" 8 self.METHOD_ECHO = "Echo" 9 self.METHOD_REVERSE = "Reverse" 10 self.METHOD_UPPERCASE = "UPPERCASE" 11 self.METHOD_LOWERCASE = "lowercase" 12 self.methods = [self.METHOD_ECHO, self.METHOD_REVERSE, self.METHOD_UPPERCASE, self.METHOD_LOWERCASE] 13 14 self.remote_php = EchoServicePHP() 15 self.remote_py = EchoServicePython() 16 17 self.status=Label() 18 self.text_area = TextArea() 19 self.text_area.setText(r"{'Test'} [\"String\"]") 20 self.text_area.setCharacterWidth(80) 21 self.text_area.setVisibleLines(8) 22 23 self.method_list = ListBox() 24 self.method_list.setVisibleItemCount(1) 25 for method in self.methods: 26 self.method_list.addItem(method) 27 self.method_list.setSelectedIndex(0) 28 29 method_panel = HorizontalPanel() 30 method_panel.add(HTML("Remote string method to call: ")) 31 method_panel.add(self.method_list) 32 method_panel.setSpacing(8) 33 34 self.button_php = Button("Send to PHP Service", self) 35 self.button_py = Button("Send to Python Service", self) 36 37 buttons = HorizontalPanel() 38 buttons.add(self.button_php) 39 buttons.add(self.button_py) 40 buttons.setSpacing(8) 41 42 info = r"<h2>JSON-RPC Example</h2><p>This example demonstrates the calling of server services with <a href=\"http://json-rpc.org/\">JSON-RPC</a>." 43 info += "<p>Enter some text below, and press a button to send the text to an Echo service on your server. An echo service simply sends the exact same text back that it receives." 44 45 panel = VerticalPanel() 46 panel.add(HTML(info)) 47 panel.add(self.text_area) 48 panel.add(method_panel) 49 panel.add(buttons) 50 panel.add(self.status) 51 52 RootPanel().add(panel) 53 54 def onClick(self, sender): 55 self.status.setText(self.TEXT_WAITING) 56 method = self.methods[self.method_list.getSelectedIndex()] 57 text = self.text_area.getText() 58 59 # demonstrate proxy & callMethod() 60 if sender == self.button_php: 61 if method == self.METHOD_ECHO: 62 id = self.remote_php.echo(text, self) 63 elif method == self.METHOD_REVERSE: 64 id = self.remote_php.callMethod("reverse", [text], self) 65 elif method == self.METHOD_UPPERCASE: 66 id = self.remote_php.uppercase(text, self) 67 elif method == self.METHOD_LOWERCASE: 68 id = self.remote_php.lowercase(text, self) 69 else: 70 if method == self.METHOD_ECHO: 71 id = self.remote_py.echo(text, self) 72 elif method == self.METHOD_REVERSE: 73 id = self.remote_py.reverse(text, self) 74 elif method == self.METHOD_UPPERCASE: 75 id = self.remote_py.uppercase(text, self) 76 elif method == self.METHOD_LOWERCASE: 77 id = self.remote_py.lowercase(text, self) 78 if id<0: 79 self.status.setText(self.TEXT_ERROR) 80 81 def onRemoteResponse(self, response, request_info): 82 self.status.setText(response) 83 84 def onRemoteError(self, code, message, request_info): 85 self.status.setText("Server Error or Invalid Response: ERROR " + code + " - " + message) 86 87 88class EchoServicePHP(JSONProxy): 89 def __init__(self): 90 JSONProxy.__init__(self, "services/EchoService.php", ["echo", "reverse", "uppercase", "lowercase"]) 91 92 93class EchoServicePython(JSONProxy): 94 def __init__(self): 95 JSONProxy.__init__(self, "services/EchoService.py", ["echo", "reverse", "uppercase", "lowercase"])