Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add basic video store api #34

Merged
merged 2 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions apps/basic-video-store-api-with-db/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
from flask import Flask
from flask_restful import Api, Resource, reqparse, fields, marshal_with, abort
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
api = Api(app)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
db = SQLAlchemy(app)

class VideoModel(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), nullable=False)
views = db.Column(db.Integer, nullable=False)
likes = db.Column(db.Integer, nullable=False)

def __repr__(self):
return f"Video(name = {name}, views = {views}, likes = {likes})"

#with app.app_context():
# db.create_all()

#small healthcheck test when call /healthcheck
class healthcheck(Resource):
def get(self):
return {'hello': 'healthy'}

api.add_resource(healthcheck, '/healthcheck')
#--------------------------------------------#

video_put_args = reqparse.RequestParser()
video_put_args.add_argument("name", type=str, help="Name of the video", required=True)
video_put_args.add_argument("views", type=int, help="Views of the video", required=True)
video_put_args.add_argument("likes", type=int, help="Likes on the video", required=True)

video_update_args = reqparse.RequestParser()
video_update_args.add_argument("name", type=str, help="Name of the video", required=False)
video_update_args.add_argument("views", type=str, help="Views of the video", required=False)
video_update_args.add_argument("likes", type=str, help="Likes on the video", required=False)

resource_fields = {
'id': fields.Integer,
'name': fields.String,
'views': fields.Integer,
'likes': fields.Integer
}
class Video(Resource):
@marshal_with(resource_fields)
def get(self, video_id):
result = VideoModel.query.filter_by(id=video_id).first()
if not result:
abort(404, message="Could not find video with that id")
return result

@marshal_with(resource_fields)
def put(self, video_id):
args = video_put_args.parse_args()
id_check = VideoModel.query.filter_by(id=video_id).first()
if id_check:
abort(409, message="Video ID already exists")
video = VideoModel(id=video_id, name=args['name'], views=args['views'], likes=args['likes'])
db.session.add(video)
db.session.commit()
return video, 201


@marshal_with(resource_fields)
def patch(self, video_id):
args = video_update_args.parse_args()
result = VideoModel.query.filter_by(id=video_id).first()
if not result:
abort(404, message="Could not find video with that id")

if args["name"]:
result.name = args["name"]
if args["views"]:
result.views = args["views"]
if args["likes"]:
result.likes = args["likes"]

db.session.commit()

return result


def delete(self, video_id):
result = VideoModel.query.filter_by(id=video_id).first()
if not result:
abort(404, message="Could not find video with that id")
db.session.delete(result)
db.session.commit()
log = {"message": "Deleted video with id {}".format(video_id)}
return log, 200

api.add_resource(Video, "/video/<int:video_id>")

#--------------------------------------------#

if __name__ == '__main__':
app.run(debug=True)
Fixed Show fixed Hide fixed
11 changes: 11 additions & 0 deletions apps/basic-video-store-api-with-db/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
aniso8601==9.0.1
click==8.1.7
Flask==3.1.0
Flask-RESTful==0.3.10
Flask-SQLAlchemy==3.1.1
itsdangerous==2.2.0
Jinja2==3.1.4
MarkupSafe==3.0.2
pytz==2024.2
SQLAlchemy==2.0.36
Werkzeug==3.1.3
47 changes: 47 additions & 0 deletions apps/basic-video-store-api-with-db/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import requests
import json

BASE = "http://127.0.0.1:5000/"

# Small GET request

#response = requests.get(BASE + "users/user1")

#print(response.json())

#--------------------------------------------#

# Small Hello World
#responseHello = requests.get(BASE + "helloworld")
#responsePOST = requests.post(BASE + "helloworld")

#print(responseHello.json())
#print(responsePOST.json())

#--------------------------------------------#

# Small Video
headers = {"Content-Type": "application/json"}
data = [
{"likes": 10, "name": "Video01", "views": 100},
{"likes": 20, "name": "Video02", "views": 200},
{"likes": 30, "name": "Video03", "views": 300}
]
for i in range(len(data)):

#responseVideo = requests.put(BASE + "video/1", json.dumps({"likes": 10, "name": "Video01", "views": 100}), headers=headers)
responseVideo = requests.put(BASE + "video/" + str(i+1), json.dumps(data[i]), headers=headers)
print(responseVideo.json())

#input()
#responseVideo = requests.delete(BASE + "video/2")
#print(responseVideo)
input()
responseVideo = requests.get(BASE + "video/1")
print(responseVideo.json())
responseVideo = requests.get(BASE + "video/2")
print(responseVideo.json())
responseVideo = requests.patch(BASE + "video/2", json.dumps({"likes": 900}), headers=headers)
print(responseVideo.json())
responseVideo = requests.get(BASE + "video/2")
print(responseVideo.json())
Loading