40 lines
1.3 KiB
Python
40 lines
1.3 KiB
Python
from flask_restx import Namespace, Resource, fields
|
|
from flask_restx import reqparse
|
|
from flask import make_response, abort, request, Response
|
|
from functools import lru_cache
|
|
import requests
|
|
import logging
|
|
|
|
api = Namespace('search',
|
|
description='Search the OHLQ website')
|
|
|
|
@api.route('/<string:query>')
|
|
@api.doc(description='Search the OHLQ website. This is equivilant to the search box on the home page.')
|
|
class Search(Resource):
|
|
@api.doc('get')
|
|
@api.response(200, 'Success')
|
|
@api.response(500, 'Failure to retrieve data')
|
|
def get(self, query):
|
|
try:
|
|
reply = performSearch(query)
|
|
except Exception as e:
|
|
abort(500, str(e))
|
|
return reply
|
|
|
|
|
|
# We define the caching mechanism for web requests
|
|
@lru_cache(maxsize=32)
|
|
def performSearch(query, page=1, pagesize=10):
|
|
params = {
|
|
"query": query,
|
|
"page": page,
|
|
"pagesize": pagesize
|
|
}
|
|
response = requests.get("https://www.ohlq.com/api/search/site", params=params)
|
|
|
|
if response.status_code != 200:
|
|
logging.warn("Unable to perform search, got non 200 response code, recieved response %s", response.status_code)
|
|
raise Exception("Unable to perform search, got non 200 response code")
|
|
|
|
return response.json()
|