7 Module implementing a synchronous network request handler for translation |
7 Module implementing a synchronous network request handler for translation |
8 requests. |
8 requests. |
9 """ |
9 """ |
10 |
10 |
11 from PyQt6.QtCore import QObject, QEventLoop, QByteArray |
11 from PyQt6.QtCore import QObject, QEventLoop, QByteArray |
12 from PyQt6.QtNetwork import ( |
12 from PyQt6.QtNetwork import QNetworkAccessManager, QNetworkRequest, QNetworkReply |
13 QNetworkAccessManager, QNetworkRequest, QNetworkReply |
|
14 ) |
|
15 |
13 |
16 from EricNetwork.EricNetworkProxyFactory import proxyAuthenticationRequired |
14 from EricNetwork.EricNetworkProxyFactory import proxyAuthenticationRequired |
17 |
15 |
18 |
16 |
19 class TranslatorRequest(QObject): |
17 class TranslatorRequest(QObject): |
20 """ |
18 """ |
21 Class implementing a synchronous network request handler for translation |
19 Class implementing a synchronous network request handler for translation |
22 requests. |
20 requests. |
23 """ |
21 """ |
|
22 |
24 def __init__(self, parent=None): |
23 def __init__(self, parent=None): |
25 """ |
24 """ |
26 Constructor |
25 Constructor |
27 |
26 |
28 @param parent reference to the parent object |
27 @param parent reference to the parent object |
29 @type QObject |
28 @type QObject |
30 """ |
29 """ |
31 super().__init__(parent) |
30 super().__init__(parent) |
32 |
31 |
33 self.__contentTypes = { |
32 self.__contentTypes = { |
34 "form": b"application/x-www-form-urlencoded", |
33 "form": b"application/x-www-form-urlencoded", |
35 "json": b"application/json", |
34 "json": b"application/json", |
36 } |
35 } |
37 |
36 |
38 self.__networkManager = QNetworkAccessManager(self) |
37 self.__networkManager = QNetworkAccessManager(self) |
39 self.__networkManager.proxyAuthenticationRequired.connect( |
38 self.__networkManager.proxyAuthenticationRequired.connect( |
40 proxyAuthenticationRequired) |
39 proxyAuthenticationRequired |
41 |
40 ) |
|
41 |
42 self.__loop = QEventLoop() |
42 self.__loop = QEventLoop() |
43 self.__networkManager.finished.connect(self.__loop.quit) |
43 self.__networkManager.finished.connect(self.__loop.quit) |
44 |
44 |
45 def get(self, requestUrl, extraHeaders=None): |
45 def get(self, requestUrl, extraHeaders=None): |
46 """ |
46 """ |
47 Public method to issue a GET request. |
47 Public method to issue a GET request. |
48 |
48 |
49 @param requestUrl URL of the request |
49 @param requestUrl URL of the request |
50 @type QUrl |
50 @type QUrl |
51 @param extraHeaders list of tuples of additional headers giving |
51 @param extraHeaders list of tuples of additional headers giving |
52 header name and header value |
52 header name and header value |
53 @type tuple of (bytes, bytes) |
53 @type tuple of (bytes, bytes) |
63 self.__loop.exec() |
63 self.__loop.exec() |
64 if reply.error() != QNetworkReply.NetworkError.NoError: |
64 if reply.error() != QNetworkReply.NetworkError.NoError: |
65 return reply.errorString(), False |
65 return reply.errorString(), False |
66 else: |
66 else: |
67 return reply.readAll(), True |
67 return reply.readAll(), True |
68 |
68 |
69 def post(self, requestUrl, requestData, dataType="form", |
69 def post(self, requestUrl, requestData, dataType="form", extraHeaders=None): |
70 extraHeaders=None): |
|
71 """ |
70 """ |
72 Public method to issue a POST request. |
71 Public method to issue a POST request. |
73 |
72 |
74 @param requestUrl URL of the request |
73 @param requestUrl URL of the request |
75 @type QUrl |
74 @type QUrl |
76 @param requestData data of the request |
75 @param requestData data of the request |
77 @type QByteArray |
76 @type QByteArray |
78 @param dataType type of the request data |
77 @param dataType type of the request data |
83 @return tuple of server response (string) and flag indicating |
82 @return tuple of server response (string) and flag indicating |
84 success (boolean) |
83 success (boolean) |
85 @rtype tuple of (str, bool) |
84 @rtype tuple of (str, bool) |
86 """ |
85 """ |
87 request = QNetworkRequest(requestUrl) |
86 request = QNetworkRequest(requestUrl) |
88 request.setRawHeader(b"User-Agent", |
87 request.setRawHeader(b"User-Agent", b"Mozilla/5.0") |
89 b"Mozilla/5.0") |
88 request.setRawHeader(b"Content-Type", self.__contentTypes[dataType]) |
90 request.setRawHeader(b"Content-Type", |
89 request.setRawHeader(b"Content-Length", QByteArray.number(requestData.size())) |
91 self.__contentTypes[dataType]) |
|
92 request.setRawHeader(b"Content-Length", |
|
93 QByteArray.number(requestData.size())) |
|
94 if extraHeaders: |
90 if extraHeaders: |
95 for name, value in extraHeaders: |
91 for name, value in extraHeaders: |
96 request.setRawHeader(name, value) |
92 request.setRawHeader(name, value) |
97 request.setUrl(requestUrl) |
93 request.setUrl(requestUrl) |
98 reply = self.__networkManager.post(request, requestData) |
94 reply = self.__networkManager.post(request, requestData) |