Add random, amnesiac method #10
@ -43,3 +43,31 @@ class getRandomFile(Resource):
|
|||||||
choice = random.choice(tuple(client.getCurrentMemeList()))
|
choice = random.choice(tuple(client.getCurrentMemeList()))
|
||||||
return make_response(client.getMeme(choice))
|
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))
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user