Braintree提供Python library用于与其API进行交互。
但是,启用日志记录后,可以看到每个API调用都会协商新的SSL连接。
braintree.Customer.create({'first_name': 'Alice'})
braintree.Customer.create({'first_name': 'Bob'})
INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): api.sandbox.braintreegateway.com
DEBUG:requests.packages.urllib3.connectionpool:"POST /merchants/hx4tr43ms83q8x9g/customers HTTP/1.1" 201 None
INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): api.sandbox.braintreegateway.com
DEBUG:requests.packages.urllib3.connectionpool:"POST /merchants/hx4tr43ms83q8x9g/customers HTTP/1.1" 201 None
通过大量通话,这会浪费大量浪费的资源 - 特别是时间。
有没有办法将braintree配置为重用/池连接,底层请求模块应该支持哪些?
答案 0 :(得分:2)
它没有得到官方支持,因为它覆盖了私有方法,但您可以提供备用HTTP实现。这个将通过会话发送所有API请求。
class SessionHttp(braintree.util.http.Http):
session = requests.Session()
def __init__(self, config, environment=None):
super(SessionHttp, self).__init__(config, environment)
def _Http__request_function(self, method):
if method == "GET":
return SessionHttp.session.get
elif method == "POST":
return SessionHttp.session.post
elif method == "PUT":
return SessionHttp.session.put
elif method == "DELETE":
return SessionHttp.session.delete
braintree.Configuration.configure(
# ...
http_strategy=SessionHttp
)
braintree.Customer.create({'first_name': 'Alice'})
braintree.Customer.create({'first_name': 'Bob'})
INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): api.sandbox.braintreegateway.com
DEBUG:requests.packages.urllib3.connectionpool:"POST /merchants/hx4tr43ms83q8x9g/customers HTTP/1.1" 201 None
DEBUG:requests.packages.urllib3.connectionpool:"POST /merchants/hx4tr43ms83q8x9g/customers HTTP/1.1" 201 None