本文整理汇总了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)