Add exact finding service

This commit is contained in:
Tyler Perkins 2023-02-24 11:14:49 -05:00
parent 92a0323be7
commit c46cd94daa
5 changed files with 64 additions and 14 deletions

View File

@ -1,10 +1,12 @@
from flask_restx import Api
from .search import api as searchNamespace
from .get import api as getNamespace
api = Api(
title='Search',
title='Memes',
version=1.0,
description='Searching the collection'
description='A programatic interface to my meme collection'
)
api.add_namespace(searchNamespace)
api.add_namespace(getNamespace)

12
src/api/clientGetter.py Normal file
View File

@ -0,0 +1,12 @@
import logging
import s3Client
def getClientSafely():
logging.debug("Getting a client safely...")
client = None
try:
client = s3Client.getClient()
except Exception as e:
logging.critical("Failed to retrive a client : " + str(e))
return client

28
src/api/get.py Normal file
View File

@ -0,0 +1,28 @@
from flask_restx import Namespace, Resource, fields
from flask_restx import reqparse
from flask import make_response
from api.clientGetter import getClientSafely
import logging
# Exported namespace
api = Namespace('resource', description='Interact with the raw underlying resource')
@api.route('/exact/<string:file_name>')
@api.doc(description="Returns the raw file. This endpoint expects exact files, NOT json")
class getExactFile(Resource):
@api.doc('get')
@api.response(200, 'Sucess')
@api.response(500, 'S3 Error')
@api.response(404, 'Requested file not found')
def get(self, file_name):
client = getClientSafely()
if client is None:
return {
"message" : "Error connecting to S3"
}, 500
if file_name in client.getCurrentMemeList():
return make_response(client.getMeme(file_name))
else:
return {
"message": "Requested file '" + file_name + "' not found"
}, 404

View File

@ -1,21 +1,11 @@
from flask_restx import Namespace, Resource, fields
from flask_restx import reqparse
import s3Client
from api.clientGetter import getClientSafely
import logging
# Exported namespace
api = Namespace('search', description='Searching for memes')
def getClientSafely():
logging.debug("Getting a client safely...")
client = None
try:
client = s3Client.getClient()
except Exception as e:
logging.critical("Failed to retrive a client : " + str(e))
return client
@api.route('/exact/<string:query>')
@api.doc(params={
@ -26,7 +16,6 @@ class exactSearch(Resource):
@api.response(200, 'Sucess')
@api.response(500, 'S3 Error')
def get(self, query):
logging.debug("Getting a client safely...")
client = getClientSafely()
if client is None:
return {

View File

@ -14,6 +14,7 @@ S3_UN = ""
S3_PW = ""
S3_TLS = True
S3_BUCKET = ""
CACHE_TTL = 10
gclient = None
@ -86,6 +87,9 @@ class Client:
raise Exception("Improper object passed for client!")
def getCurrentMemeList(self, force=False):
"""
Get a list of all memes in the bucket
"""
now = datetime.now()
if (now - self.lastCheckedAllMemes).seconds > 300 or force:
logging.info("Enough time has elapsed, refreshing meme cache...")
@ -102,4 +106,19 @@ class Client:
return self.allMemes
def getMeme(self, memeName: str):
"""
Return a meme with the exact given name, or raise an exception
"""
if not isinstance(memeName, str):
raise Exception("paramater memeName is of improper type, expected a str")
memeSet = self.getCurrentMemeList()
if memeName in memeSet:
reply = self.client.get_object(bucket_name=S3_BUCKET,
object_name=memeName)
return reply.read()
else:
raise Exception("Requested meme '" + memeName + "' not found")
return None