arxiv_metadata/main.py

66 lines
2.1 KiB
Python
Raw Normal View History

2015-12-23 23:58:16 +01:00
#!/usr/bin/env python3
2015-12-24 20:34:34 +01:00
import bottle
2015-12-23 23:58:16 +01:00
from bottle.ext import sqlalchemy
from sqlalchemy import create_engine
2015-12-23 23:58:16 +01:00
2015-12-25 20:11:56 +01:00
import config
2015-12-24 20:34:34 +01:00
import database
import routes
import tools
2015-12-23 23:58:16 +01:00
2015-12-24 20:34:34 +01:00
# Initialize db and include the SQLAlchemy plugin in bottle
2015-12-25 20:11:56 +01:00
engine = create_engine('sqlite:///%s' % (config.database,), echo=True)
2015-12-23 23:58:16 +01:00
2015-12-24 20:34:34 +01:00
app = bottle.Bottle()
plugin = sqlalchemy.Plugin(
# SQLAlchemy engine created with create_engine function.
engine,
# SQLAlchemy metadata, required only if create=True.
database.Base.metadata,
# Keyword used to inject session database in a route (default 'db').
keyword='db',
# If it is true, execute `metadata.create_all(engine)` when plugin is
# applied (default False).
create=True,
# If it is true, plugin commit changes after route is executed (default
# True).
commit=True,
# If it is true and keyword is not defined, plugin uses **kwargs argument
# to inject session database (default False).
use_kwargs=False
)
2015-12-23 23:58:16 +01:00
2015-12-24 20:34:34 +01:00
app.install(plugin)
2015-12-23 23:58:16 +01:00
2015-12-24 20:34:34 +01:00
# Routes
@app.get("/")
def index():
return tools.APIResponse(tools.pretty_json({
"papers": "/papers/?id={id}&doi={doi}&arxiv_id={arxiv_id}",
}))
2015-12-24 20:34:34 +01:00
app.get("/papers", callback=routes.get.fetch_papers)
app.get("/papers/<id:int>", callback=routes.get.fetch_by_id)
app.get("/papers/<id:int>/relationships/<name>",
callback=routes.get.fetch_relationship)
app.get("/papers/<id:int>/<name>",
callback=routes.get.fetch_relationship)
2015-12-25 22:14:10 +01:00
app.route("/papers/<id:int>", method="DELETE",
callback=routes.delete.delete_paper)
app.route("/papers/<id:int>/relationships/<name>", method="DELETE",
callback=routes.delete.delete_relationship)
2015-12-24 20:34:34 +01:00
app.post("/papers", callback=routes.post.create_paper)
app.post("/papers/<id:int>/relationships/<name>",
callback=routes.post.update_relationships)
# Complete replacement of relationships is forbidden
app.route("/papers/<id:int>/relationships/<name>", method="PATCH",
callback=lambda id, name: bottle.HTTPError(403, "Forbidden"))
2015-12-23 23:58:16 +01:00
if __name__ == "__main__":
2015-12-25 20:11:56 +01:00
app.run(host=config.host, port=config.port, debug=(not config.production))