Forked mumble-django project from https://bitbucket.org/Svedrin/mumble-django
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

200 lines
6.3 KiB

  1. # -*- coding: utf-8 -*-
  2. # kate: space-indent on; indent-width 4; replace-tabs on;
  3. """
  4. * Copyright (C) 2010, Michael "Svedrin" Ziegler <diese-addy@funzt-halt.net>
  5. *
  6. * djExtDirect is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This package is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. """
  16. try:
  17. import simplejson
  18. except ImportError:
  19. import json as simplejson
  20. import httplib
  21. from threading import Lock
  22. from urlparse import urljoin, urlparse
  23. def lexjs(javascript):
  24. """ Parse the given javascript and return a dict of variables defined in there. """
  25. ST_NAME, ST_ASSIGN = range(2)
  26. state = ST_NAME
  27. foundvars = {}
  28. buf = ""
  29. name = ""
  30. for char in javascript:
  31. if state == ST_NAME:
  32. if char == ' ':
  33. continue
  34. elif char == '=':
  35. state = ST_ASSIGN
  36. name = buf
  37. buf = ""
  38. elif char == ';':
  39. state = ST_NAME
  40. buf = ""
  41. else:
  42. buf += char
  43. elif state == ST_ASSIGN:
  44. if char == ';':
  45. state = ST_NAME
  46. foundvars[name] = simplejson.loads(buf)
  47. name = ""
  48. buf = ""
  49. else:
  50. buf += char
  51. return foundvars
  52. class RequestError(Exception):
  53. """ Raised if the request returned a status code other than 200. """
  54. pass
  55. class ReturnedError(Exception):
  56. """ Raised if the "type" field in the response is "exception". """
  57. pass
  58. class Client(object):
  59. """ Ext.Direct client side implementation.
  60. This class handles parsing an API specification, building proxy objects from it,
  61. and making calls to the router specified in the API.
  62. Instantiation:
  63. >>> cli = Client( "http://localhost:8000/mumble/api/api.js", "Ext.app.REMOTING_API" )
  64. The apiname parameter defaults to ``Ext.app.REMOTING_API`` and is used to select
  65. the proper API variable from the API source.
  66. The client will then create proxy objects for each action defined in the URL,
  67. which are accessible as properties of the Client instance. Suppose your API defines
  68. the ``Accounts`` and ``Mumble`` actions, then the client will provide those as such:
  69. >>> cli.Accounts
  70. <client.AccountsPrx object at 0x93d9e2c>
  71. >>> cli.Mumble
  72. <client.MumblePrx object at 0x93d9a2c>
  73. These objects provide native Python methods for each method defined in the actions:
  74. >>> cli.Accounts.login
  75. <bound method AccountsPrx.login of <client.AccountsPrx object at 0x93d9e2c>>
  76. So, in order to make a call over Ext.Direct, you would simply call the proxy method:
  77. >>> cli.Accounts.login( "svedrin", "passwort" )
  78. {'success': True}
  79. """
  80. def __init__( self, apiurl, apiname="Ext.app.REMOTING_API", cookie=None ):
  81. self.apiurl = apiurl
  82. self.apiname = apiname
  83. self.cookie = cookie
  84. purl = urlparse( self.apiurl )
  85. conn = {
  86. "http": httplib.HTTPConnection,
  87. "https": httplib.HTTPSConnection
  88. }[purl.scheme.lower()]( purl.netloc )
  89. conn.putrequest( "GET", purl.path )
  90. conn.endheaders()
  91. resp = conn.getresponse()
  92. foundvars = lexjs( resp.read() )
  93. conn.close()
  94. self.api = foundvars[apiname]
  95. self.routerurl = urljoin( self.apiurl, self.api["url"] )
  96. self._tid = 1
  97. self._tidlock = Lock()
  98. for action in self.api['actions']:
  99. setattr( self, action, self.get_object(action) )
  100. @property
  101. def tid( self ):
  102. """ Thread-safely get a new TID. """
  103. self._tidlock.acquire()
  104. self._tid += 1
  105. newtid = self._tid
  106. self._tidlock.release()
  107. return newtid
  108. def call( self, action, method, *args ):
  109. """ Make a call to Ext.Direct. """
  110. reqtid = self.tid
  111. data=simplejson.dumps({
  112. 'tid': reqtid,
  113. 'action': action,
  114. 'method': method,
  115. 'data': args,
  116. 'type': 'rpc'
  117. })
  118. purl = urlparse( self.routerurl )
  119. conn = {
  120. "http": httplib.HTTPConnection,
  121. "https": httplib.HTTPSConnection
  122. }[purl.scheme.lower()]( purl.netloc )
  123. conn.putrequest( "POST", purl.path )
  124. conn.putheader( "Content-Type", "application/json" )
  125. conn.putheader( "Content-Length", str(len(data)) )
  126. if self.cookie:
  127. conn.putheader( "Cookie", self.cookie )
  128. conn.endheaders()
  129. conn.send( data )
  130. resp = conn.getresponse()
  131. if resp.status != 200:
  132. raise RequestError( resp.status, resp.reason )
  133. respdata = simplejson.loads( resp.read() )
  134. if respdata['type'] == 'exception':
  135. raise ReturnedError( respdata['message'], respdata['where'] )
  136. if respdata['tid'] != reqtid:
  137. raise RequestError( 'TID mismatch' )
  138. cookie = resp.getheader( "set-cookie" )
  139. if cookie:
  140. self.cookie = cookie.split(';')[0]
  141. conn.close()
  142. return respdata['result']
  143. def get_object( self, action ):
  144. """ Return a proxy object that has methods defined in the API. """
  145. def makemethod( methspec ):
  146. def func( self, *args ):
  147. if len(args) != methspec['len']:
  148. raise TypeError( '%s() takes exactly %d arguments (%d given)' % (
  149. methspec['name'], methspec['len'], len(args)
  150. ) )
  151. return self._cli.call( action, methspec['name'], *args )
  152. func.__name__ = methspec['name']
  153. return func
  154. def init( self, cli ):
  155. self._cli = cli
  156. attrs = {
  157. '__init__': init
  158. }
  159. for methspec in self.api['actions'][action]:
  160. attrs[methspec['name']] = makemethod( methspec )
  161. return type( action+"Prx", (object,), attrs )( self )