-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
211 lines (184 loc) · 5.6 KB
/
main.py
File metadata and controls
211 lines (184 loc) · 5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
from fastapi import FastAPI
from pydantic import BaseModel
import platform
import subprocess
import requests
from uuid import uuid4
import os
import shutil
from typing import Optional
import json
app = FastAPI()
class UserData(BaseModel):
name: str
age: int
class ListDirInput(BaseModel):
input: str
class TaskData(BaseModel):
id: Optional[str] = None
title: Optional[str] = None
description: Optional[str] = None
status: Optional[str] = None
assignee: Optional[str] = None
with open('tasks.json', 'r') as f:
tasks = json.load(f)
f.close()
@app.get("/hello")
def hello():
return {"message": "Hello, world!"}
@app.post("/hello")
def create_user(user: UserData):
print(user.name, user.age)
return {"message": f"Hello, {user.name}. Your age is {user.age}!"}
# Yunus's task 2
@app.post('/generate_uuid_with_data/')
def generate_uuid_with_name(user: UserData):
try:
generated_uuid = uuid4()
return {
"message": f"Hello, {user.name}, Your age is {user.age}",
"uuid": str(generated_uuid)
}
except:
return {"message": "Error generating UUID and obtaining user data"}
# Somon's task
@app.get('/ping/{hostname}')
def ping_host(hostname: str) -> bool:
try:
# -n for Windows, -c for Linux/macOS.
if platform.system().lower() == 'windows':
param = '-n'
else:
param = '-c'
command = ['ping', param, '1', hostname]
result = subprocess.run(command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
if result.returncode == 0:
return True
else:
return False
except Exception as e:
return False
# Yunus's task
@app.get('/generate_uuid/')
def generate_uuid():
try:
generated_uuid = uuid4()
return {"message": f"Your UUID is: {generated_uuid}"}
except:
return {"message": "Error generating UUID"}
# Elsu's task
@app.post("/listdir")
def list_dir(path: ListDirInput):
try:
files = os.listdir(path.input)
return {"files": files}
except:
return {
"Error": "There is some error, try again"
}
# Jonathan's Task
@app.get("/get_public_ip")
def get_ip():
try:
pub_ip = requests.get('https://api.ipify.org')
pub_ip = pub_ip.text
return {"message": f"Your public IP address is: {pub_ip}"}
except:
return {"message": "Error fetching public IP"}
# Naza's Task - read_log_tail(filepath, lines)
@app.get("/read_log_tail(filepath, lines)")
def read_log_tail(filepath: str, lines: int = 5):
try:
# this opens the file for reading. "r" means read.
file = open(filepath, "r")
content = file.readlines()
last_lines = content[-lines:]
return {"lines": last_lines}
except FileNotFoundError:
return {"error": "File not found. Check the path."}
# Meerim's task check_disk_usage(path)
@app.get("/disk-usage/")
def check_disk_usage(path: str = "/"):
try:
total, used, free = shutil.disk_usage(path)
return {
"path": path,
"total_GB": round(total / (2**30), 2),
"used_GB": round(used / (2**30), 2),
"free_GB": round(free / (2**30), 2)
}
except FileNotFoundError:
return {"error": "Path does not exist"}
## abdul's task
@app.get("/tasks")
def list_tasks_data():
try:
return tasks
except Exception as e:
return {"error": str(e)}
@app.get("/tasks/{task_id}")
def get_task(task_id: str):
try:
task = tasks.get(task_id)
if not task:
return tasks
return task
except Exception as e:
return {"error": str(e)}
@app.post("/tasks")
def get_task(task: TaskData):
try:
if task.id is not None:
return {"error": f"Task with task ID {task.id} exists"}
else:
task.id = str(uuid4())
tasks[task.id] = task.model_dump()
with open('tasks.json', 'w') as f:
f.write(json.dumps(tasks))
f.close()
return {"message": f"Task with id {task.id} created successfully", "task": tasks[task.id]}
except Exception as e:
return {"error": str(e)}
@app.put("/tasks/{task_id}")
def update_task(task_id: str, task: TaskData):
try:
if task_id not in tasks:
return {"error": f"Task with id {task_id} not found"}
for field, value in task.model_dump().items():
if value is not None and field != "id":
tasks[task_id][field] = value
with open('tasks.json', 'w') as f:
f.write(json.dumps(tasks))
return {"message": f"Task with id {task_id} updated successfully", "task": tasks[task_id]}
except Exception as e:
return {"error": str(e)}
# shah's Task
@app.get("/system_info")
def system_information():
sys_info = {
'System information':platform.system(),
'Python Version':platform.python_version(),
'Architecture': platform.architecture()
}
return(sys_info)
# Tugs's task
@app.get("/cpuLoadAverage")
def cpu_t():
try:
load_ave = os.getloadavg()
print(f"Cpu load average last 1 minutes, 5 minutes, 15 minutes: {load_ave}")
return f"Cpu load average last 1 minutes, 5 minutes, 15 minutes: {load_ave}"
except:
print("Try again")
return "it should be good"
# Tugs task2
@app.post("/addnum")
def number(n1: int, n2: int) -> int:
try:
n1 = 100
n2 = 101
return n1 + n2
print(n1 + n2)
except ValueError:
return
print("Enter integer")