Mercurial > p > roundup > code
comparison doc/rest.txt @ 5737:07d8bbf6ee5f
Add example on overriding RestfulInstance.getRateLimit from interfaces.py.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Sun, 26 May 2019 21:55:02 -0400 |
| parents | 59a3bbd3603a |
| children | 9777d7311bc0 |
comparison
equal
deleted
inserted
replaced
| 5736:1b5bcc5d745f | 5737:07d8bbf6ee5f |
|---|---|
| 1187 | 1187 |
| 1188 | 1188 |
| 1189 Adding other endpoints (e.g. to allow an OPTIONS query against | 1189 Adding other endpoints (e.g. to allow an OPTIONS query against |
| 1190 ``/data/issue/@schema``) is left as an exercise for the reader. | 1190 ``/data/issue/@schema``) is left as an exercise for the reader. |
| 1191 | 1191 |
| 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. | |
| 1192 | 1231 |
| 1193 Test Examples | 1232 Test Examples |
| 1194 ============= | 1233 ============= |
| 1195 | 1234 |
| 1196 Rate limit tests: | 1235 Rate limit tests: |
