This repository has been archived by the owner on Aug 11, 2022. It is now read-only.
forked from stenskjaer/lbp-print-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
97 lines (82 loc) · 3.02 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import logging
import json
import os
import subprocess
from logging.config import dictConfig
from flask import Flask, request, make_response, jsonify
from flask.logging import default_handler
from flask import send_from_directory
import lbp_print.core as lbp_print
import lbp_print.config as lbp_config
from processor import handle_job
dictConfig(
{
"version": 1,
"formatters": {
"default": {
"format": "[%(asctime)s] %(name)s - %(levelname)s - %(message)s"
}
},
"handlers": {
"default": {
"level": "DEBUG",
"formatter": "default",
"class": "logging.handlers.RotatingFileHandler",
"filename": "test.log",
"maxBytes": 5000000,
"backupCount": 5,
}
},
"root": {"level": logging.DEBUG, "handlers": ["default"]},
}
)
# App version
#__VERSION__ = subprocess.check_output("git describe --tags", shell=True).decode()
app = Flask(__name__, instance_path=os.getcwd())
logger = logging.getLogger()
logger.addHandler(default_handler)
@app.route("/api/v1/resource")
def process_resource():
logger.debug(f"Received request with args: {request.args}")
resource_id = request.args.get("id")
resource_url = request.args.get("url")
if not resource_id and not resource_url:
error_message = {
"error": "One of the parameters 'id' and 'url' must be given. 'id' must container an SCTA resource id, e.g. scta.info/resource/lectio1. 'url' must contain an http reference to an XML file"
}
return jsonify(error_message)
elif resource_id and resource_url:
error_message = {
"error": "One of the parameters 'id' and 'url' must be given, but not both."
}
return jsonify(error_message)
if resource_id:
resource_value = resource_id
resource_type = "scta"
trans = lbp_print.RemoteResource(resource_id)
else:
resource_value = resource_url
resource_type = "url"
trans = lbp_print.UrlResource(resource_url)
cache = lbp_print.Cache("./cache")
digest = trans.create_hash()
if cache.contains(digest + ".pdf"):
response = {"Status": "Finished", "url": digest + ".pdf"}
else:
response = handle_job(resource_value, resource_type)
#response = handle_job(trans)
#response = handle_job(resource_value, resource_type)
#return jsonify(response)
resp = make_response(jsonify(response),200)
resp.headers['Access-Control-Allow-Origin'] = '*'
return resp
@app.route("/api/v1/cache/<hashwithextension>", methods=['GET'])
def return_cache(hashwithextension):
return send_from_directory("cache", hashwithextension)
@app.route('/api/v1/docs/', defaults={'path': None})
@app.route("/api/v1/docs/<path:path>", methods=['GET'])
def send_docs(path):
path = path if path else "index.html"
return send_from_directory('static/docs', path)
if __name__ == "__main__":
app.run(debug=True, port=5000)