11"""Timbr Python SQLAlchemy connector."""
22
33__version__ = "2.0.0"
4+
5+ try :
6+ from thrift .transport import THttpClient
7+ def timbr_flush (self ):
8+ if self .__http_response is not None :
9+ self .__http_response .close ()
10+ self .__http_response = None
11+
12+ if not self .isOpen ():
13+ self .open ()
14+
15+ # Pull data out of buffer
16+ data = self .__wbuf .getvalue ()
17+ self .__wbuf = BytesIO ()
18+
19+ # HTTP request
20+ if self .using_proxy () and self .scheme == "http" :
21+ # need full URL of real host for HTTP proxy here (HTTPS uses CONNECT tunnel)
22+ self .__http .putrequest ('POST' , "http://%s:%s%s" %
23+ (self .realhost , self .realport , self .path ))
24+ else :
25+ self .__http .putrequest ('POST' , self .path )
26+
27+ # Write headers
28+ self .__http .putheader ('Content-Type' , 'application/x-thrift' )
29+ self .__http .putheader ('Content-Length' , str (len (data )))
30+ if self .using_proxy () and self .scheme == "http" and self .proxy_auth is not None :
31+ self .__http .putheader ("Proxy-Authorization" , self .proxy_auth )
32+
33+ if not self .__custom_headers or 'User-Agent' not in self .__custom_headers :
34+ user_agent = 'Python/THttpClient'
35+ script = os .path .basename (sys .argv [0 ])
36+ if script :
37+ user_agent = '%s (%s)' % (user_agent , urllib .parse .quote (script ))
38+ self .__http .putheader ('User-Agent' , user_agent )
39+
40+ if self .__custom_headers :
41+ for key , val in six .iteritems (self .__custom_headers ):
42+ self .__http .putheader (key , val )
43+
44+ # Saves the cookie sent by the server in the previous response.
45+ # HTTPConnection.putheader can only be called after a request has been
46+ # started, and before it's been sent.
47+ if self .headers and 'Set-Cookie' in self .headers :
48+ self .__http .putheader ('Cookie' , self .headers ['Set-Cookie' ])
49+
50+ self .__http .endheaders ()
51+
52+ # Write payload
53+ self .__http .send (data )
54+
55+ # Get reply to flush the request
56+ self .__http_response = self .__http .getresponse ()
57+ self .code = self .__http_response .status
58+ self .message = self .__http_response .reason
59+ self .headers = self .__http_response .msg
60+
61+ THttpClient .flush = timbr_flush
62+ except :
63+ pass
0 commit comments