From 9eee22a4e5003691d5a8d8151de83028c32cd53d Mon Sep 17 00:00:00 2001 From: Tyler Perkins Date: Fri, 24 Feb 2023 11:24:20 -0500 Subject: [PATCH 1/3] Add random, amnesiac method --- src/api/get.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/api/get.py b/src/api/get.py index 46d5dab..e2554a1 100644 --- a/src/api/get.py +++ b/src/api/get.py @@ -3,12 +3,13 @@ from flask_restx import reqparse from flask import make_response from api.clientGetter import getClientSafely import logging +import random # Exported namespace -api = Namespace('resource', description='Interact with the raw underlying resource') +api = Namespace('resource', description='Interact with the raw underlying files. This namespace does NOT speak json, just raw files') @api.route('/exact/') -@api.doc(description="Returns the raw file. This endpoint expects exact files, NOT json") +@api.doc(description="Interact with exact raw files.") class getExactFile(Resource): @api.doc('get') @api.response(200, 'Sucess') @@ -26,3 +27,19 @@ class getExactFile(Resource): return { "message": "Requested file '" + file_name + "' not found" }, 404 + +@api.route('/random') +@api.doc(description="Returns a random meme") +class getRandomFile(Resource): + @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())) + return make_response(client.getMeme(choice)) + -- 2.45.2 From f903f4e10c534114fb263e5382e74083cb77194b Mon Sep 17 00:00:00 2001 From: Tyler Perkins Date: Fri, 24 Feb 2023 11:34:28 -0500 Subject: [PATCH 2/3] 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)) + -- 2.45.2 From 65cde7fcc9bf73e81b91195f82cb7d508ef421d9 Mon Sep 17 00:00:00 2001 From: Tyler Perkins Date: Fri, 24 Feb 2023 11:40:01 -0500 Subject: [PATCH 3/3] Add meme count endpoint --- src/api/__init__.py | 2 ++ src/api/util.py | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 src/api/util.py diff --git a/src/api/__init__.py b/src/api/__init__.py index 4fd5f7b..ef3333a 100644 --- a/src/api/__init__.py +++ b/src/api/__init__.py @@ -1,6 +1,7 @@ from flask_restx import Api from .search import api as searchNamespace from .get import api as getNamespace +from .util import api as utilNamespace api = Api( title='Memes', @@ -10,3 +11,4 @@ api = Api( api.add_namespace(searchNamespace) api.add_namespace(getNamespace) +api.add_namespace(utilNamespace) diff --git a/src/api/util.py b/src/api/util.py new file mode 100644 index 0000000..6293ed5 --- /dev/null +++ b/src/api/util.py @@ -0,0 +1,22 @@ +from flask_restx import Namespace, Resource, fields +from flask_restx import reqparse +from api.clientGetter import getClientSafely +import logging + +# Exported namespace +api = Namespace('util', description='Misc Utilities') + +@api.route('/count') +@api.doc(description="Get number of memes in store") +class getCount(Resource): + def get(self): + client = getClientSafely() + if client is None: + return { + "message": "Error connecting to S3" + }, 500 + + return { + "count" : len(client.getCurrentMemeList()) + }, 200 + -- 2.45.2