Uploading chapter 11 code examples

This commit is contained in:
muassif
2021-07-08 21:58:21 +04:00
committed by GitHub
parent 6bed575c4a
commit 49e6811b81
9 changed files with 367 additions and 0 deletions
+21
View File
@@ -0,0 +1,21 @@
# Use the official lightweight Python image.
# https://hub.docker.com/_/python
FROM python:3.8-slim
# Allow statements and log messages to immediately appear in the Knative logs
ENV PYTHONUNBUFFERED True
# Copy local code to the container image.
WORKDIR /app
COPY . ./
# Install production dependencies.
RUN pip install -r requirements.txt
RUN pip install Flask gunicorn
# Run the web service on container startup. Here we use the gunicorn
# webserver, with one worker process and 8 threads.
# For environments with multiple CPU cores, increase the number of workers
# to be equal to the cores available.
# Timeout is set to 0 to disable the timeouts of the workers to allow Cloud Run to handle instance scaling.
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 --timeout 0 api_app:app
+84
View File
@@ -0,0 +1,84 @@
# api_app: REST API for student resource
from flask_sqlalchemy import SQLAlchemy
from flask import Flask
from flask_restful import Resource, Api, reqparse
app = Flask(__name__)
api = Api(app)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///student.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
parser = reqparse.RequestParser()
parser.add_argument('name', type=str)
parser.add_argument('grade', type=str)
class Student(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), nullable=False)
grade = db.Column(db.String(20), nullable=True)
def __repr__(self):
return f"{'id':{self.id}, 'name':{self.name},'grade':{self.grade}}"
def serialize(self):
return {
'id': self.id,
'name': self.name,
'grade': self.grade
}
class StudentDao(Resource):
def get(self, student_id):
student = Student.query.filter_by(id=student_id).\
first_or_404(
description='Record with id={} is not available'.format(student_id))
return student.serialize()
def delete(self, student_id):
student = Student.query.filter_by(id=student_id).\
first_or_404(
description='Record with id={} is not available'.format(student_id))
db.session.delete(student)
db.session.commit()
return '', 204
def put(self, student_id):
student = Student.query.filter_by(id=student_id).first_or_404(
description='Record with id={} is not available'.format(student_id))
args = parser.parse_args()
name = args['name']
grade = args['grade']
if (name):
student.name = name
if (grade):
student.grade = grade
db.session.commit()
return student.serialize(), 200
class StudentListDao(Resource):
def get(self):
students = Student.query.all()
return [Student.serialize(student) for student in students]
def post(self):
args = parser.parse_args()
name = args['name']
grade = args['grade']
student = Student(name=name, grade=grade)
db.session.add(student)
db.session.commit()
return student.serialize(), 200
api.add_resource(StudentDao, '/students/<student_id>')
api.add_resource(StudentListDao, '/students')
if __name__ == '__main__':
# app.run(debug=True)
app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))
+23
View File
@@ -0,0 +1,23 @@
aniso8601==9.0.1
attrs==21.2.0
certifi==2021.5.30
chardet==4.0.0
click==8.0.1
Flask==2.0.1
Flask-RESTful==0.3.9
flask-restplus==0.13.0
Flask-SQLAlchemy==2.5.1
greenlet==1.1.0
idna==2.10
itsdangerous==2.0.1
Jinja2==3.0.1
jsonschema==3.2.0
MarkupSafe==2.0.1
pyrsistent==0.17.3
pytz==2021.1
requests==2.25.1
six==1.16.0
SQLAlchemy==1.4.19
urllib3==1.26.6
utils-flask-sqlalchemy==0.1.4
Werkzeug==2.0.1
Binary file not shown.