Last active
January 29, 2018 09:21
-
-
Save viveksb007/9a524ff585625678de9c0faa36d7c7df to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, jsonify, request, Response | |
import json | |
app = Flask(__name__) | |
@app.route('/response', methods=['GET']) | |
def api_response(): | |
data = { | |
'data': 'test data', | |
'id': 1 | |
} | |
js = json.dumps(data) | |
resp = Response(js, status=200, mimetype='application/json') | |
return resp | |
@app.route('/check, methods=['GET', 'POST', 'PUT', 'DELETE']) | |
def check(): | |
if request.method == 'GET': | |
return "REQUEST TYPE: GET" | |
elif request.method == 'POST': | |
return "REQUEST TYPE: POST" | |
elif request.method == 'PUT': | |
return "REQUEST TYPE: PUT" | |
elif request.method == 'DELETE': | |
return "REQUEST TYPE: DELETE" | |
@app.route('/', methods=['GET']) | |
def index(): | |
return jsonify({ | |
'author': 'Vivek Singh Bhadauria', | |
'author_url': 'http://viveksb007.wordpress.com/', | |
'base_url': 'zeolearn.com/', | |
'endpoints': { | |
'Returns URLS of images': '/magazines/photos/{number of photos}', | |
} | |
}) | |
@app.route('/magazines/photos/<number_of_images>', methods=['GET']) | |
def get_image_links(number_of_images): | |
image_list = [] | |
# Code for scraping zeolearn magazine section and appending links in image_list | |
response = requests.get('https://www.zeolearn.com/magazine/') | |
soup = BeautifulSoup(response.text, 'html.parser') | |
for tag in soup.find_all('img'): | |
image_list.append(tag['src']) | |
image_data = {} | |
if int(number_of_images) < len(image_list): | |
for i in range(0, int(number_of_images)): | |
image_data[str(i)] = image_list[i] | |
else: | |
for i in range(0, len(image_list)): | |
image_data[str(i)] = image_list[i] | |
return jsonify(image_data) | |
@app.route('/messages', methods = ['POST']) | |
def api_message(): | |
if request.headers['Content-Type'] == 'text/plain': | |
return "Text Message: " + request.data | |
elif request.headers['Content-Type'] == 'application/json': | |
return "JSON Message: " + json.dumps(request.json) | |
else: | |
return "415 Unsupported Media Type ;)" | |
if __name__ == '__main__': | |
app.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, jsonify | |
app = Flask(__name__) | |
@app.route('/', methods=['GET']) | |
def index(): | |
return jsonify({ | |
'author': 'Vivek Singh Bhadauria', | |
'author_url': 'http://viveksb007.wordpress.com/', | |
'base_url': 'zeolearn.com/', | |
'endpoints': { | |
'Returns URLS of images': '/magazines/photos/{number of photos}', | |
} | |
}) | |
if __name__ == '__main__': | |
app.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import request | |
@app.route('/check, methods=['GET', 'POST', 'PUT', 'DELETE']) | |
def check(): | |
if request.method == 'GET': | |
return "REQUEST TYPE: GET" | |
elif request.method == 'POST': | |
return "REQUEST TYPE: POST" | |
elif request.method == 'PUT': | |
return "REQUEST TYPE: PUT" | |
elif request.method == 'DELETE': | |
return "REQUEST TYPE: DELETE" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Response | |
import json | |
@app.route('/response', methods=['GET']) | |
def api_response(): | |
data = { | |
'data': 'test data', | |
'id': 1 | |
} | |
js = json.dumps(data) | |
resp = Response(js, status=200, mimetype='application/json') | |
return resp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@app.route('/magazines/photos/<number_of_images>', methods=['GET']) | |
def get_image_links(number_of_images): | |
image_list = [] | |
# Code for scraping zeolearn magazine section and appending links in image_list | |
response = requests.get('https://www.zeolearn.com/magazine/') | |
soup = BeautifulSoup(response.text, 'html.parser') | |
for tag in soup.find_all('img'): | |
image_list.append(tag['src']) | |
image_data = {} | |
if int(number_of_images) < len(image_list): | |
for i in range(0, int(number_of_images)): | |
image_data[str(i)] = image_list[i] | |
else: | |
for i in range(0, len(image_list)): | |
image_data[str(i)] = image_list[i] | |
return jsonify(image_data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import json | |
@app.route('/messages', methods = ['POST']) | |
def api_message(): | |
if request.headers['Content-Type'] == 'text/plain': | |
return "Text Message: " + request.data | |
elif request.headers['Content-Type'] == 'application/json': | |
return "JSON Message: " + json.dumps(request.json) | |
else: | |
return "415 Unsupported Media Type ;)" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
curl -H "Content-type: application/json" -X POST http://127.0.0.1:5000/messages -d '{ "message" : "Test Data" }' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment