From f903f4e10c534114fb263e5382e74083cb77194b Mon Sep 17 00:00:00 2001 From: Tyler Perkins Date: Fri, 24 Feb 2023 11:34:28 -0500 Subject: [PATCH] Add psuedorandom endpoint --- src/api/get.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/api/get.py b/src/api/get.py index e2554a1..2ccc41a 100644 --- a/src/api/get.py +++ b/src/api/get.py @@ -43,3 +43,31 @@ class getRandomFile(Resource): choice = random.choice(tuple(client.getCurrentMemeList())) return make_response(client.getMeme(choice)) +@api.route('/psuedorandom') +@api.doc(description="Returns a psuedorandom meme. Will not return the same meme for a set number of requests") +class getRandomFile(Resource): + cache = [] + maxSize = 100 + @api.doc('get') + @api.response(200, 'Sucess') + @api.response(500, 'S3 Error') + def get(self): + client = getClientSafely() + if client is None: + return { + "message" : "Error connecting to S3" + }, 500 + + choice = random.choice(tuple(client.getCurrentMemeList())) + while choice in self.cache: + choice = random.choice(tuple(client.getCurrentMemeList())) + + self.cache.append(choice) + + if len(self.cache) > self.maxSize: + self.cache.pop() + + logging.debug("Contents of cache : " + str(self.cache)) + + return make_response(client.getMeme(choice)) +