Skip to content

Commit 0b950ca

Browse files
committed
Add example on overriding RestfulInstance.getRateLimit from interfaces.py.
1 parent 3705681 commit 0b950ca

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

doc/rest.txt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,6 +1189,45 @@ returns some data about the class::
11891189
Adding other endpoints (e.g. to allow an OPTIONS query against
11901190
``/data/issue/@schema``) is left as an exercise for the reader.
11911191

1192+
Creating Custom Rate Limits
1193+
===========================
1194+
1195+
Using the file ``interfaces.py`` in the tracker home directory, you
1196+
can replace the rate limiter function. This lets you return a
1197+
different rate limiter rather then the one that is provided in the
1198+
rate_limiter.py file. If you add a ``rate_limit_interval`` and
1199+
``rate_limit_calls`` as integers in the user object you can do
1200+
something like this in interfaces.py::
1201+
1202+
from roundup.rest import RestfulInstance, RateLimit
1203+
from datetime import timedelta
1204+
1205+
def grl(self):
1206+
calls = self.db.config.WEB_API_CALLS_PER_INTERVAL
1207+
interval = self.db.config.WEB_API_INTERVAL_IN_SEC
1208+
1209+
if calls and interval: # use to disable all rate limits
1210+
1211+
uid = self.db.getuid()
1212+
class_obj = self.db.getclass('user')
1213+
node = class_obj.getnode(uid)
1214+
1215+
# set value to 0 to use WEB_API_CALLS_PER_INTERVAL
1216+
user_calls = node.__getattr__('rate_limit_calls')
1217+
# set to 0 to use WEB_API_INTERVAL_IN_SEC
1218+
user_interval = node.__getattr__('rate_limit_interval')
1219+
1220+
return RateLimit(user_calls or calls,
1221+
timedelta(seconds=(user_interval or interval)))
1222+
else:
1223+
# disable rate limiting if either parameter is 0
1224+
return None
1225+
1226+
RestfulInstance.getRateLimit = grl
1227+
1228+
this should replace the default getRateLimit with the new grl function
1229+
that looks for per user values for the number of calls and period.
1230+
If either is set to 0, the defaults from ``config.ini`` file are used.
11921231

11931232
Test Examples
11941233
=============

0 commit comments

Comments
 (0)