Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
67 changes: 67 additions & 0 deletions final_project.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from flask import Flask, request, jsonify

app = Flask(__name__)
#change for pr
JSON_NUM = 0
tasks = {}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is JSON_NUM capital and tasks small? be consistent (no need to re-submit)


@app.route("/tasks", methods=["GET"])
def getTasks():
return jsonify(tasks), 200

@app.route("/tasks/<task_id>", methods=["GET"])
def getTaskId(task_id):
global tasks
if task_id in tasks:
return jsonify(tasks[task_id]), 200
return jsonify({"error": "Task not found"}), 404

@app.route("/tasks/<task_id>/completed", methods=["PUT"])
def completeTask(task_id):
global tasks
if task_id in tasks:
tasks[task_id]["message"] = "Task marked as completed"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i dont quite understand the stucture of a task in your code. it has a title AND a message field? or the message is only for the REST response?

tasks[task_id]["completed"] = True
return jsonify(tasks[task_id]), 200
return jsonify({"error": "Task not found"}), 404

@app.route("/tasks", methods=["POST"])
def addTasks():
global tasks, JSON_NUM
data = request.get_json()
if not data or "title" not in data or "description" not in data:
return jsonify({"error": "Bad Request, data must include 'title' and 'description' "}), 400
JSON_NUM += 1
new_task = {str(JSON_NUM): {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

"completed": False,
'description': data["description"],
"id": JSON_NUM,
'title': data["title"],
}}
tasks.update(new_task)
return jsonify(new_task), 201

@app.route("/tasks/<task_id>", methods=["PUT"])
def updateTask(task_id):
global tasks
data = request.get_json()
if not data or "title" not in data or "description" not in data:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

duplication. i suggest an function for the "if"

return jsonify({"error": "Bad Request, data must include 'name' and 'age'"}), 400

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checking name and age? this message is from another project :)

if task_id in tasks:
tasks[task_id].update(data)
return jsonify(tasks[task_id]), 200
return jsonify({"error": "Task not found"}), 404

@app.route("/tasks/<task_id>", methods=["DELETE"])
def deleteTask(task_id):
global tasks
if task_id in tasks:
if tasks[task_id]["completed"] is True:
del tasks[task_id]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice idea

return jsonify({"message": "task deleted"}), 200
else:
return jsonify({"error": "Task not completed"}), 404
return jsonify({"error": "Task not found"}), 404

if __name__ == "__main__":
app.run(host="0.0.0.0", port=5050)
124 changes: 124 additions & 0 deletions mid_project.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import requests

#making a change for pr

SERVER_PORT = 5000
SERVER_URL = "http://18.207.203.105:" + str(SERVER_PORT)
STUDENT_API = "students"

def accessServer():
ip_address = input("Please enter your host address: ")
port_num = input("Please enter the port: ")
return ip_address, port_num

def getStudentList(ip, port):
response = requests.get(f"http://{ip}:{port}/{STUDENT_API}")
if response.status_code == 200:
students_list = response.json()
for student in students_list["students"]:
print(f'1. Name: {student["name"]}')
print(f'2. Age: {student["age"]}')
print(f'3. ID: {student["id"]}')
print(10 * "-")
else:
print("Error bad request")

def getStudentInfo(ip, port):
student_id = input("Please enter student id: ")
response = requests.get(f"http://{ip}:{port}/{STUDENT_API}/{student_id}")
if response.status_code == 200:
student_by_id = response.json()
print(f'1. Name: {student_by_id["name"]}')
print(f'2. Age: {student_by_id["age"]}')
print(f'3. ID: {student_by_id["id"]}')
print(10 * "-")
else:
print(f"Error fetching student with id {student_id}")

def saveNewStudent(ip, port):
new_name = input("Please enter student name: ")
new_age = input("Please enter student age: ")
student_data = {
"name": new_name,
"age": new_age
}
response = requests.post(f"http://{ip}:{port}/{STUDENT_API}", json=student_data)
if response.status_code == 201:
print("New student added successfully.")
else:
print("Error adding student.")

def changeStudentName(ip, port):
#put does not work as the server expects name and age keys together
# Error updating student: 400 - {"error":"Bad Request, data must include 'name' and 'age'"}
student_id = input("Please enter student id: ")
new_student_name = input("Please enter new student name: ")
response = requests.get(f"http://{ip}:{port}/{STUDENT_API}/{student_id}")
update_name= response.json()
update_name["name"] = new_student_name
response = requests.put(f"http://{ip}:{port}/{STUDENT_API}/{student_id}", json=update_name)
if response.status_code == 200:
print("Student name updated successfully.")
else:
print(f"Error updating student: {response.status_code} - {response.text}")


def changeStudentAge(ip, port):
student_id = input("Please enter student id: ")
new_student_age = input("Please enter student age: ")
response = requests.get(f"http://{ip}:{port}/{STUDENT_API}/{student_id}")
update_age = response.json()
update_age["age"] = new_student_age
response = requests.put(f"http://{ip}:{port}/{STUDENT_API}/{student_id}", json=update_age)
if response.status_code == 200:
print("Student name updated successfully.")
else:
print(f"Error updating student: {response.status_code} - {response.text}")

def deleteStudent(ip, port):
student_id = input("Please enter student id to erase: ")
response = requests.delete(f"http://{ip}:{port}/{STUDENT_API}/{student_id}")
if response.status_code == 200:
print("Student name deleted successfully.")
else:
print("Error deleting student:")

def serverOptions(ip_address, port_num):
ip, port = ip_address, port_num
options = [
"Get student list",
"Get specific student information",
"Save a new student",
"Change student name",
"Change student age",
"Delete student",
"Exit"
]
options_func_call = {"1": getStudentList,
"2": getStudentInfo,
"3": saveNewStudent,
"4": changeStudentName,
"5": changeStudentAge,
"6": deleteStudent,
"Exit": ""
}
for index, options in enumerate(options, 1):
print(f"{index}. {options}")
selected_option = input("Please select a menu option (1-7): ")
if selected_option == "7":
return False
if selected_option in options_func_call.keys():
options_func_call[selected_option](ip, port)
return True
else:
print("Invalid option, please select again.")
return True

def main():
ip_aws, port_aws = accessServer()
flag = True
while flag:
flag = serverOptions(ip_aws, port_aws)

if __name__ == "__main__":
main()