From ccb7b257a4ab212740c74ced9790e3ac90492b17 Mon Sep 17 00:00:00 2001 From: robeartoe Date: Sun, 1 Oct 2017 12:20:02 -0700 Subject: [PATCH 1/2] python3;added test file --- app.py | 22 ++++++++++------------ joke.py | 10 ++++++---- requirements.txt | 15 +++++++++++++-- test.py | 11 +++++++++++ 4 files changed, 40 insertions(+), 18 deletions(-) create mode 100644 test.py diff --git a/app.py b/app.py index 30b57e3..073063f 100644 --- a/app.py +++ b/app.py @@ -1,23 +1,22 @@ # -*- coding: utf-8 -*- -from flask import Flask +from flask import Flask,request,jsonify from flask_restful import Resource, Api import joke import sys - +import importlib app = Flask(__name__) api = Api(app) +importlib.reload(sys) - -reload(sys) -sys.setdefaultencoding('utf-8') - -class APP(Resource): - def get(self): - return {'this is soon to become an awesome->': 'website'} - - def post(self): +@app.route('/',methods=['POST','GET']) +@app.route('/index', methods=['POST','GET']) +def index(): + if request.method == "GET": + response = "this is soon to become an awesome-> : website" + return jsonify(response) + else: jk = joke.getJoke() jk = jk.encode('ascii', 'ignore').decode('ascii') #jk = jk.encode('utf-8') @@ -30,7 +29,6 @@ def get(self): # jk = jk.encode('utf-8') return jk -api.add_resource(APP, '/') api.add_resource(API, '/api') if __name__ == '__main__': diff --git a/joke.py b/joke.py index 644ef7e..9f70a9b 100644 --- a/joke.py +++ b/joke.py @@ -1,14 +1,16 @@ # -*- coding: utf-8 -*- import json -from random import * +from random import randint def random_digits(): range_start = 1 - range_end = 545 + with open('data.json') as data_file: + data = json.load(data_file) + range_end = len(data) return randint(range_start, range_end) + def getJoke(): with open('data.json') as data_file: data = json.load(data_file) joke = data[random_digits()] - print joke - return joke \ No newline at end of file + return joke diff --git a/requirements.txt b/requirements.txt index 0bd7801..81eebdb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,18 @@ +aniso8601==1.3.0 +certifi==2017.7.27.1 +chardet==3.0.4 +click==6.7 Flask==0.11.1 +Flask-RESTful==0.3.6 gunicorn==19.7.0 httplib2==0.9.2 +idna==2.6 Jinja2==2.8 -virtualenv==15.1.0 +MarkupSafe==1.0 +pkg-resources==0.0.0 +python-dateutil==2.6.1 +pytz==2017.2 +requests==2.18.4 +six==1.11.0 +urllib3==1.22 Werkzeug==0.11.10 -flask-restful \ No newline at end of file diff --git a/test.py b/test.py new file mode 100644 index 0000000..04c18e4 --- /dev/null +++ b/test.py @@ -0,0 +1,11 @@ +import requests + +# Test Website: +r = requests.get('http://localhost:5000') +print(r.text) +r = requests.post('http://localhost:5000') +print(r.text) + +# Test API: +r = requests.get('http://localhost:5000/api') +print(r.text) From 2eacfce91a058f0ee458bef93d0361fa6f160898 Mon Sep 17 00:00:00 2001 From: robeartoe Date: Sun, 1 Oct 2017 20:57:29 -0700 Subject: [PATCH 2/2] fixed requirements.txt --- requirements.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/requirements.txt b/requirements.txt index 81eebdb..9b49a03 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,6 +7,7 @@ Flask-RESTful==0.3.6 gunicorn==19.7.0 httplib2==0.9.2 idna==2.6 +itsdangerous==0.24 Jinja2==2.8 MarkupSafe==1.0 pkg-resources==0.0.0 @@ -15,4 +16,5 @@ pytz==2017.2 requests==2.18.4 six==1.11.0 urllib3==1.22 +virtualenv==15.1.0 Werkzeug==0.11.10