forked from pablin87/rtb_exchange_sim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest_factory.py
98 lines (90 loc) · 3.23 KB
/
request_factory.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from mako.template import Template
import random
import logging
from request import RTBRequest
from response import RTBResponse
class RTBRequestFactory(object):
'''
Handles creation and mapping of requests
'''
def __init__(self, template_file):
self.template_file = template_file
self.template = None
self.plugin_instance = None
def initialize(self):
# read the template file
self.template = Template(filename=self.template_file)
def set_parameter_plug(self, plugin, adserver, config):
'''
Set a param class plugin, this must implement
the ParameterPlugin interface
'''
self.plugin_instance = plugin()
self.plugin_instance.initialize(adserver, config)
def create_request(self, set_aid=True):
'''
Create a request and call the parameter plugin
'''
logging.debug('create_request')
aid = None
req_line = ''
headers = {}
body = ''
if self.plugin_instance :
req_line, headers, body = \
self.plugin_instance.get_request()
req = RTBRequest(self.template,
req_line,
headers,
body)
return req.build()
def receive_response(self, buf):
'''
Receives a response buffer and checks if
it has a full HTTP response, if it does
it invokes the plugin and returns an empty
buffer, otherwise the buffer is returned as
it was passed
'''
logging.debug('receive_response')
response = RTBResponse()
ok, parser = response.receive_buffer(buf)
if ok :
if self.plugin_instance :
win, req_line, headers, body = \
self.plugin_instance.receive_response(
parser.get_status_code(),
parser.get_headers(),
parser.recv_body())
return ('', win, req_line, headers, body)
else:
return (buf, None, None, None, None)
def create_win_request(self, req_line, headers, body):
'''
Creates a win request and returns the buffer
to be sent by the connection
'''
logging.debug('create_win_request')
req = RTBRequest(self.template,
req_line,
headers,
body)
return req.build()
def receive_win_response(self, buf):
'''
Receives a win response buffer and checks if
it has a full HTTP response, if it does
it invokes the plugin and returns an empty
buffer, otherwise the buffer is returned as
it was passed
'''
logging.debug('receive_win_response')
response = RTBResponse()
ok, parser = response.receive_buffer(buf)
if ok and self.plugin_instance :
self.plugin_instance.receive_win_response(
parser.get_status_code(),
parser.get_headers(),
parser.recv_body())
return ''
return buf