Add exact finding service #9

Merged
tyler merged 1 commits from exact-file-return into master 2023-02-24 16:17:04 +00:00
5 changed files with 64 additions and 14 deletions
Showing only changes of commit c46cd94daa - Show all commits

View File

@ -1,10 +1,12 @@
from flask_restx import Api from flask_restx import Api
from .search import api as searchNamespace from .search import api as searchNamespace
from .get import api as getNamespace
api = Api( api = Api(
title='Search', title='Memes',
version=1.0, version=1.0,
description='Searching the collection' description='A programatic interface to my meme collection'
) )
api.add_namespace(searchNamespace) 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 Namespace, Resource, fields
from flask_restx import reqparse from flask_restx import reqparse
import s3Client from api.clientGetter import getClientSafely
import logging import logging
# Exported namespace # Exported namespace
api = Namespace('search', description='Searching for memes') 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.route('/exact/<string:query>')
@api.doc(params={ @api.doc(params={
@ -26,7 +16,6 @@ class exactSearch(Resource):
@api.response(200, 'Sucess') @api.response(200, 'Sucess')
@api.response(500, 'S3 Error') @api.response(500, 'S3 Error')
def get(self, query): def get(self, query):
logging.debug("Getting a client safely...")
client = getClientSafely() client = getClientSafely()
if client is None: if client is None:
return { return {

View File

@ -14,6 +14,7 @@ S3_UN = ""
S3_PW = "" S3_PW = ""
S3_TLS = True S3_TLS = True
S3_BUCKET = "" S3_BUCKET = ""
CACHE_TTL = 10
gclient = None gclient = None
@ -86,6 +87,9 @@ class Client:
raise Exception("Improper object passed for client!") raise Exception("Improper object passed for client!")
def getCurrentMemeList(self, force=False): def getCurrentMemeList(self, force=False):
"""
Get a list of all memes in the bucket
"""
now = datetime.now() now = datetime.now()
if (now - self.lastCheckedAllMemes).seconds > 300 or force: if (now - self.lastCheckedAllMemes).seconds > 300 or force:
logging.info("Enough time has elapsed, refreshing meme cache...") logging.info("Enough time has elapsed, refreshing meme cache...")
@ -102,4 +106,19 @@ class Client:
return self.allMemes 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