本文整理匯總了Python中rest_framework.throttling.UserRateThrottle方法的典型用法代碼示例。如果您正苦於以下問題:Python throttling.UserRateThrottle方法的具體用法?Python throttling.UserRateThrottle怎麽用?Python throttling.UserRateThrottle使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類rest_framework.throttling
的用法示例。
在下文中一共展示了throttling.UserRateThrottle方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_throttling
# 需要導入模塊: from rest_framework import throttling [as 別名]
# 或者: from rest_framework.throttling import UserRateThrottle [as 別名]
def test_throttling(self):
"""Test that the rate of requests to the basket creation endpoint is throttled."""
request_limit = UserRateThrottle().num_requests
# Make a number of requests equal to the number of allowed requests
for _ in range(request_limit):
self.create_basket(skus=[self.PAID_SKU])
# Make one more request to trigger throttling of the client
response = self.create_basket(skus=[self.PAID_SKU])
self.assertEqual(response.status_code, 429)
self.assertIn("Request was throttled.", response.data['detail'])
示例2: allow_request
# 需要導入模塊: from rest_framework import throttling [as 別名]
# 或者: from rest_framework.throttling import UserRateThrottle [as 別名]
def allow_request(self, request, view):
"""Returns True if the request is coming from one of the service users
and defaults to UserRateThrottle's configured setting otherwise.
"""
service_users = [settings.ECOMMERCE_SERVICE_WORKER_USERNAME, settings.PROSPECTUS_WORKER_USERNAME]
if request.user.username in service_users:
return True
return super(ServiceUserThrottle, self).allow_request(request, view)
示例3: allow_request
# 需要導入模塊: from rest_framework import throttling [as 別名]
# 或者: from rest_framework.throttling import UserRateThrottle [as 別名]
def allow_request(self, request, view):
"""
Modify throttling for service users.
Updates throttling rate if the request is coming from the service user, and
defaults to UserRateThrottle's configured setting otherwise.
Updated throttling rate comes from `DEFAULT_THROTTLE_RATES` key in `REST_FRAMEWORK`
setting. service user throttling is specified in `DEFAULT_THROTTLE_RATES` by `service_user` key
Example Setting:
```
REST_FRAMEWORK = {
...
'DEFAULT_THROTTLE_RATES': {
...
'service_user': '50/day'
}
}
```
"""
service_users = get_service_usernames()
# User service user throttling rates for service user.
if request.user.username in service_users:
self.update_throttle_scope()
return super(ServiceUserThrottle, self).allow_request(request, view)