-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest_message_processor.py
55 lines (44 loc) · 1.71 KB
/
request_message_processor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from tornado.httpclient import HTTPRequest, AsyncHTTPClient, HTTPError
from webservice_serial.protocol.request_verb import RequestVerb
from webservice_serial.protocol.response_message import ResponseMessage
from webservice_serial.protocol.response_verb import ResponseVerb
class RequestMessageProcessor(object):
"""
:param port: Port that WebService are executing
"""
def __init__(self, port):
self.http_client = AsyncHTTPClient()
self.url = 'http://localhost:{}'.format(port)
self.processed_listener = lambda message, response: ...
def process(self, message):
"""
:param RequestMessage message:
"""
if message.verb is RequestVerb.SYSTEM:
return
request = HTTPRequest(self.url + message.path, method=message.verb.value)
self.http_client.fetch(
request,
lambda response: self.response(message, response=response)
)
def response(self, message, response):
"""
:param RequestMessage message: Request message
:param HTTPResponse response: Response message
:return:
"""
try:
response_message = ResponseMessage(ResponseVerb.RESPONSE, response.body.decode('utf8'))
except HTTPError as e:
# HTTPError is raised for non-200 responses; the response
# can be found in e.response.
print("Error: " + str(e))
#FIXME
return
except Exception as e:
# Other errors are possible, such as IOError.
print("Error: " + str(e))
return
self.processed_listener(message, response_message)
def close(self):
self.http_client.close()