# Store the key and address
self.key = key
+
+ # Fetch the client IP address
self.address, port = self.request.client
+ # Check if the user is authenticated
+ self.api_key = self.request.headers.get("X-Api-Key")
+
+ # Make the bucket
+ # Use the API key for authenticated users,
+ # or otherwise use the client IP address
+ if self.api_key:
+ self.bucket = self.api_key
+ else:
+ self.bucket = "%s" % self.address
+
# What is the current time?
self.now = datetime.datetime.utcnow()
.where(
ratelimiter.c.key == self.key,
ratelimiter.c.timestamp >= since,
- ratelimiter.c.bucket == "%s" % self.address,
+ ratelimiter.c.bucket == self.bucket,
)
)
.values({
"key" : self.key,
"timestamp" : now,
- "bucket" : "%s" % self.address,
+ "bucket" : self.bucket,
"requests" : 1,
"expires_at" : expires_at,
})