如何使 Braintree 的 python 客户端重用连接?
How to make Braintree's python client reuse connections?
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 配置为 reuse/pool 连接,底层请求模块应该支持?
它不受官方支持,因为它覆盖了私有方法,但您可以提供备用 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
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 配置为 reuse/pool 连接,底层请求模块应该支持?
它不受官方支持,因为它覆盖了私有方法,但您可以提供备用 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