Add psuedorandom endpoint

This commit is contained in:
Tyler Perkins 2023-02-24 11:34:28 -05:00
parent 9eee22a4e5
commit f903f4e10c
1 changed files with 28 additions and 0 deletions

View File

@ -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))