E5Network/E5XmlRpcClient.py

Fri, 15 May 2015 19:31:14 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Fri, 15 May 2015 19:31:14 +0200
changeset 4258
d141e2bb94cc
child 4326
e52318f11812
permissions
-rw-r--r--

Added a Qt based xmlrpc client.

4258
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
3 # Copyright (c) 2015 Detlev Offenbach <detlev@die-offenbachs.de>
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing a xmlrpc client for Qt.
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10 from __future__ import unicode_literals
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11 try:
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12 import xmlrpclib as xmlrpc
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13 except ImportError:
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14 import xmlrpc.client as xmlrpc
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16 from PyQt5.QtCore import QObject, QUrl, QByteArray
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17 from PyQt5.QtNetwork import QNetworkAccessManager, \
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18 QNetworkRequest, QNetworkReply
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20 from E5Network.E5NetworkProxyFactory import proxyAuthenticationRequired
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 try:
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22 from E5Network.E5SslErrorHandler import E5SslErrorHandler
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23 SSL_AVAILABLE = True
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 except ImportError:
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 SSL_AVAILABLE = False
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 class E5XmlRpcClient(QObject):
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 """
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30 Class implementing a xmlrpc client for Qt.
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 """
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 def __init__(self, url, parent=None):
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 """
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 Constructor
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36 @param url xmlrpc handler URL (string or QUrl)
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 @param parent parent object (QObject)
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38 """
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 super(E5XmlRpcClient, self).__init__(parent)
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 # attributes for the network objects
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 self.__networkManager = QNetworkAccessManager(self)
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 self.__networkManager.proxyAuthenticationRequired.connect(
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 proxyAuthenticationRequired)
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 self.__networkManager.finished.connect(self.__replyFinished)
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 if SSL_AVAILABLE:
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 self.__sslErrorHandler = E5SslErrorHandler(self)
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48 self.__networkManager.sslErrors.connect(self.__sslErrors)
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50 self.__callmap = {}
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52 self.__request = QNetworkRequest(QUrl(url))
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53 self.__request.setRawHeader("User-Agent", "E5XmlRpcClient/1.0")
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 self.__request.setHeader(QNetworkRequest.ContentTypeHeader, "text/xml")
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56 def setUrl(self, url):
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57 """
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58 Public slot to set the xmlrpc handler URL.
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60 @param url xmlrpc handler URL (string or QUrl)
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61 """
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62 url = QUrl(url)
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63 if url.isValid():
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64 self.__request.setUrl(url)
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66 def call(self, method, args, responseCallback, errorCallback):
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67 """
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 Public method to call the remote server.
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70 @param method name of the remote method to be called (string)
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71 @param args tuple of method arguments (tuple)
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
72 @param responseCallback method to be called with the returned
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73 result as a tuple (function)
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74 @param errorCallback method to be called in case of an error
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
75 with error code and error string (function)
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
76 """
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77 assert isinstance(args, tuple), \
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78 "argument must be tuple or Fault instance"
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80 data = xmlrpc.dumps(args, method).encode("utf-8")
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81 reply = self.__networkManager.post(
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
82 self.__request, QByteArray(data))
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83 self.__callmap[reply] = (responseCallback, errorCallback)
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
84
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
85 def abort(self):
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
86 """
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87 Public method to abort all calls.
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88 """
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89 for reply in list(self.__callmap):
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90 if reply.isRunning():
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
91 reply.abort()
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
92
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93 def __sslErrors(self, reply, errors):
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
94 """
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95 Private slot to handle SSL errors.
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
97 @param reply reference to the reply object (QNetworkReply)
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
98 @param errors list of SSL errors (list of QSslError)
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
99 """
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
100 ignored = self.__sslErrorHandler.sslErrorsReply(reply, errors)[0]
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
101 if ignored == E5SslErrorHandler.NotIgnored and reply in self.__callmap:
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
102 self.__callmap[reply][1](xmlrpc.TRANSPORT_ERROR, self.tr(
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
103 "SSL Error"))
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
104
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
105 def __replyFinished(self, reply):
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
106 """
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
107 Private slot handling the receipt of a reply.
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
108
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
109 @param reply reference to the finished reply (QNetworkReply)
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
110 """
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
111 if reply not in self.__callmap:
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
112 return
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
113
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
114 if reply.error() != QNetworkReply.NoError:
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
115 self.__callmap[reply][1](xmlrpc.TRANSPORT_ERROR,
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
116 reply.errorString())
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
117 else:
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
118 data = bytes(reply.readAll()).decode("utf-8")
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
119 try:
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
120 data = xmlrpc.loads(data)[0]
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
121 self.__callmap[reply][0](data)
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
122 except xmlrpc.Fault as fault:
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
123 self.__callmap[reply][1](fault.faultCode, fault.faultString)
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
124
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
125 reply.deleteLater()
d141e2bb94cc Added a Qt based xmlrpc client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
126 del self.__callmap[reply]

eric ide

mercurial