gpt4 book ai didi

python - 如何让Braintree的python客户端重用连接?

转载 作者:行者123 更新时间:2023-12-01 03:54:48 24 4
gpt4 key购买 nike

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 配置为重用/池连接,底层请求模块应该支持这些连接?

最佳答案

它不受官方支持,因为它覆盖了私有(private)方法,但您可以提供替代的 HTTP 实现。这将通过 session 发送所有 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

关于python - 如何让Braintree的python客户端重用连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37654878/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com