diff --git a/README.md b/README.md index 95dece8..da1990d 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ # OHLQ-api +API wrapper to scrape the [Ohio Liquour Agency](https://www.ohlq.com) diff --git a/src/api/__init__.py b/src/api/__init__.py new file mode 100644 index 0000000..a32c747 --- /dev/null +++ b/src/api/__init__.py @@ -0,0 +1,10 @@ +from flask_restx import Api +from .search import api as search_ns + +api = Api( + title='OHLQ', + version='1.0', + description='Programatic interface to OHLQ' + ) + +api.add_namespace(search_ns) diff --git a/src/api/availability.py b/src/api/availability.py new file mode 100644 index 0000000..e69de29 diff --git a/src/api/search.py b/src/api/search.py new file mode 100644 index 0000000..c38504d --- /dev/null +++ b/src/api/search.py @@ -0,0 +1,39 @@ +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('/') +@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() diff --git a/src/app.py b/src/app.py new file mode 100755 index 0000000..21b46b3 --- /dev/null +++ b/src/app.py @@ -0,0 +1,21 @@ +#!/usr/bin/python +# Entry point + +from flask import Flask +from api import api +from config import config, setLogLevel +import os +import logging + +setLogLevel() + +port = config["server.port"] + +app = Flask(__name__) + +api.init_app(app) + +app.run(host='0.0.0.0', port=port) + + + diff --git a/src/config.py b/src/config.py new file mode 100644 index 0000000..d372a68 --- /dev/null +++ b/src/config.py @@ -0,0 +1,19 @@ +# Load configuration yaml + +import envyaml +import os +import logging + +config = envyaml.EnvYAML(os.environ.get('CONFIG_PATH', 'config.yaml')) + +def setLogLevel(): + """ + Set the logging level based on the config + """ + logLevel = config["log.level"] + if logLevel == "info": + logging.basicConfig(level=logging.INFO) + elif logLevel == "debug": + logging.basicConfig(level=logging.DEBUG) + + logging.warn("Logging level set to %s", logLevel) diff --git a/src/config.yaml b/src/config.yaml new file mode 100644 index 0000000..b0bcda9 --- /dev/null +++ b/src/config.yaml @@ -0,0 +1,5 @@ +log: + level: "debug" + +server: + port: 8080 diff --git a/src/requirements.txt b/src/requirements.txt new file mode 100644 index 0000000..e69de29