- commit
- 064cc5d5cb7870621660c7dc4d3be2a43d92efdd
- parent
- bd0cbefebd9d191c24bd1e0bd065aade6c66d7b5
- Author
- Tobias Bengfort <tobias.bengfort@gmx.net>
- Date
- 2015-12-06 21:26
join api and __init__
Diffstat
| M | jsonproxy/__init__.py | 77 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- |
| D | jsonproxy/api.py | 79 | ------------------------------------------------------------ |
2 files changed, 73 insertions, 83 deletions
diff --git a/jsonproxy/__init__.py b/jsonproxy/__init__.py
@@ -3,17 +3,87 @@ from __future__ import absolute_import 3 3 import os 4 4 import sys 5 5 -1 6 try: -1 7 from urllib.request import urlopen as _urlopen -1 8 from urllib.error import HTTPError -1 9 except ImportError: -1 10 from urllib2 import urlopen as _urlopen -1 11 from urllib2 import HTTPError -1 12 -1 13 from flask import abort -1 14 from flask import current_app 6 15 from flask import Flask -1 16 from flask import jsonify -1 17 from flask import make_response -1 18 from flask import render_template -1 19 from flask import request -1 20 import cachetools 7 218 -1 from .api import api9 -1 from .lib import parse_args10 22 from .lib import check_config -1 23 from .lib import _doc -1 24 from .lib import ENDPOINTS -1 25 from .lib import parse_args -1 26 from .lib import scrape -1 27 -1 28 app = Flask(__name__) -1 29 -1 30 -1 31 def get_config(endpoint): -1 32 try: -1 33 return current_app.config[ENDPOINTS][endpoint] -1 34 except KeyError: -1 35 abort(404) -1 36 -1 37 -1 38 @cachetools.ttl_cache() -1 39 def urlopen(url): -1 40 try: -1 41 current_app.logger.info('fetching %s' % url) -1 42 original = _urlopen(url) -1 43 -1 44 body = original.read() -1 45 code = original.getcode() -1 46 headers = original.headers.items() -1 47 -1 48 return body, code, headers -1 49 except HTTPError as error: -1 50 abort(error.code) -1 51 -1 52 -1 53 @app.route('/<endpoint>/<path:path>', methods=['GET']) -1 54 def handle(endpoint, path): -1 55 config = get_config(endpoint) -1 56 url = request.url.replace(request.host_url + endpoint + '/', config['host']) -1 57 -1 58 body, code, headers = urlopen(url) -1 59 -1 60 if 'fields' in config: -1 61 response = jsonify(scrape(url, body, config)) -1 62 else: -1 63 response = make_response(body, code) -1 64 -1 65 if current_app.config.get('ALLOW_CORS', False): -1 66 response.headers['Access-Control-Allow-Origin'] = '*' -1 67 -1 68 return response -1 69 -1 70 -1 71 @app.route('/', methods=['GET']) -1 72 def index(): -1 73 config = current_app.config[ENDPOINTS] -1 74 data = [_doc(config[endpoint], endpoint) for endpoint in config] -1 75 return render_template('index.html', endpoints=data) -1 76 -1 77 -1 78 @app.route('/<endpoint>/', methods=['GET']) -1 79 def doc(endpoint): -1 80 config = get_config(endpoint) -1 81 return render_template('index.html', endpoints=[_doc(config, endpoint)]) 11 82 12 83 13 84 def main(): 14 85 args = parse_args() 15 8616 -1 app = Flask(__name__)17 87 app.config.from_pyfile(os.path.abspath(args.config)) 18 88 app.debug = args.debug 19 89 @@ -23,7 +93,6 @@ def main(): 23 93 app.logger.error(error) 24 94 sys.exit(1) 25 9526 -1 app.register_blueprint(api)27 96 app.run(host=args.host, port=args.port) 28 97 29 98
diff --git a/jsonproxy/api.py b/jsonproxy/api.py
@@ -1,79 +0,0 @@1 -1 from __future__ import absolute_import2 -13 -1 try:4 -1 from urllib.request import urlopen as _urlopen5 -1 from urllib.error import HTTPError6 -1 except ImportError:7 -1 from urllib2 import urlopen as _urlopen8 -1 from urllib2 import HTTPError9 -110 -1 from flask import Blueprint11 -1 from flask import request12 -1 from flask import current_app13 -1 from flask import abort14 -1 from flask import jsonify15 -1 from flask import make_response16 -1 from flask import render_template17 -1 import cachetools18 -119 -1 from .lib import _doc20 -1 from .lib import ENDPOINTS21 -1 from .lib import scrape22 -123 -1 api = Blueprint('api', __name__, static_folder='static')24 -125 -126 -1 def get_config(endpoint):27 -1 try:28 -1 return current_app.config[ENDPOINTS][endpoint]29 -1 except KeyError:30 -1 abort(404)31 -132 -133 -1 @cachetools.ttl_cache()34 -1 def urlopen(url):35 -1 try:36 -1 current_app.logger.info('fetching %s' % url)37 -1 original = _urlopen(url)38 -139 -1 body = original.read()40 -1 code = original.getcode()41 -1 headers = original.headers.items()42 -143 -1 return body, code, headers44 -1 except HTTPError as error:45 -1 abort(error.code)46 -147 -148 -1 @api.route('/<endpoint>/<path:path>', methods=['GET'])49 -1 def main(endpoint, path):50 -1 config = get_config(endpoint)51 -1 url = request.url.replace(request.host_url + endpoint + '/', config['host'])52 -153 -1 body, code, headers = urlopen(url)54 -155 -1 if 'fields' in config:56 -1 if code == 200:57 -1 response = jsonify(scrape(url, body, config))58 -1 else:59 -1 abort(code)60 -1 else:61 -1 response = make_response(body, code)62 -163 -1 if current_app.config.get('ALLOW_CORS', False):64 -1 response.headers['Access-Control-Allow-Origin'] = '*'65 -166 -1 return response67 -168 -169 -1 @api.route('/', methods=['GET'])70 -1 def index():71 -1 config = current_app.config[ENDPOINTS]72 -1 data = [_doc(config[endpoint], endpoint) for endpoint in config]73 -1 return render_template('index.html', endpoints=data)74 -175 -176 -1 @api.route('/<endpoint>/', methods=['GET'])77 -1 def doc(endpoint):78 -1 config = get_config(endpoint)79 -1 return render_template('index.html', endpoints=[_doc(config, endpoint)])