我正在进行Braintree订阅搜索,以检索与这些ID相关联的客户ID和订阅价格。在我的代码中,我遵循这个职位的建议。
下面是我代码的摘录:
gateway = braintree.BraintreeGateway(
braintree.Configuration(
environment=braintree.Environment.Production,
merchant_id= 'our_merchant_id',
public_key='our_public_key',
private_key='our_private_key'
)
)
subscriptions = gateway.subscription.search(
braintree.SubscriptionSearch.status.in_list(
braintree.Subscription.Status.Active,
braintree.Subscription.Status.PastDue,
braintree.Subscription.Status.Pending
)
)
result = {}
for subscription in subscriptions.items:
payment_method = gateway.payment_method.find(subscription.payment_method_token)
result[payment_method.customer_id] = subscription.price
"""do something with result """这种方法在BT沙箱和大约100条记录的小查询中工作得很好。但是,每当我试图查询超过120个订阅时,BT服务器都会一致地以504错误进行响应。我想在生产中一次查询大约5000份订阅。有什么建议吗?
回溯:
Traceback (most recent call last):
File "BT_subscrAmount.py", line 22, in <module>
for subscription in subscriptions.items:
File "/home/karen/miniconda3/lib/python3.6/site-
packages/braintree/resource_collection.py", line 38, in items
for item in self.__method(self.__query, batch):
File "/home/karen/miniconda3/lib/python3.6/site-
packages/braintree/subscription_gateway.py", line 79, in __fetch
response = self.config.http().post(self.config.base_merchant_path() +
"/subscriptions/advanced_search", {"search": criteria})
File "/home/karen/miniconda3/lib/python3.6/site-
packages/braintree/util/http.py", line 56, in post
return self.__http_do("POST", path, Http.ContentType.Xml, params)
File "/home/karen/miniconda3/lib/python3.6/site-
packages/braintree/util/http.py", line 86, in __http_do
Http.raise_exception_from_status(status)
File "/home/karen/miniconda3/lib/python3.6/site-
packages/braintree/util/http.py", line 49, in raise_exception_from_status
raise UnexpectedError("Unexpected HTTP_RESPONSE " + str(status))
braintree.exceptions.unexpected_error.UnexpectedError: Unexpected
HTTP_RESPONSE 504发布于 2018-09-26 15:15:19
在一位出色的BT支持工程师的帮助下,我们终于找到了解决问题的方法。
解释如下:
当BT返回搜索结果时,它们收集所有相关数据,并向客户端返回一个大型(序列化)响应对象。在我们的示例中,有些订阅有大量关联事务,这使得获取、序列化和将它们返回客户端的过程变得缓慢。当发出请求时,BT网关以组形式返回页面,默认超时时间为60秒。任何具有多个大型响应对象的组都可能超时并产生504错误。
解决办法:
为了避免这种延迟,我们可以使用ResourceCollections类中的id方法,而不是使用items方法,只返回对象的id。使用单个订阅id,我们可以获取单个订阅,并且只获取我们需要的属性,如下所示:
subscriptions = gateway.subscription.search(
braintree.SubscriptionSearch.status.in_list(
braintree.Subscription.Status.Active
)
)
subscription_ids = subscriptions.ids
def find_customer_id(id):
subscription = gateway.subscription.find(id)
payment_method = gateway.payment_method.find(subscription.payment_method_token)
return (payment_method.customer_id, subscription.price) 希望这能帮到别人!
发布于 2018-09-25 18:15:55
将查询拆分为小批(5000+订阅变成100个订阅中的50个调用),然后在得到响应后进行聚合。许多API都有速率限制和响应限制硬编码。
https://stackoverflow.com/questions/52503289
复制相似问题