mirror of
https://github.com/ThunixdotNet/thunix_api.git
synced 2026-01-24 13:10:18 +00:00
Refractor thunix_api to use flask_rest package
This commit is contained in:
5
endpoints/disk.py
Normal file
5
endpoints/disk.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from flask_restful import Resource, abort
|
||||
|
||||
class Disk(Resource):
|
||||
def get(self):
|
||||
abort(501, message="Not currently implemented.")
|
||||
11
endpoints/home.py
Normal file
11
endpoints/home.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from flask_restful import Resource
|
||||
|
||||
class Home(Resource):
|
||||
def get(self):
|
||||
payload = [
|
||||
{
|
||||
"Description": "The Thunix API. Please see https://wiki.thunix.net/wiki/api for more information."
|
||||
}
|
||||
]
|
||||
|
||||
return payload
|
||||
32
endpoints/ip_info.py
Normal file
32
endpoints/ip_info.py
Normal file
@@ -0,0 +1,32 @@
|
||||
from flask_restful import Resource
|
||||
import psutil
|
||||
|
||||
class Ip_Info(Resource):
|
||||
def get_ip_addresses(self, family):
|
||||
ip_addresses = []
|
||||
for interface, snics in psutil.net_if_addrs().items():
|
||||
for snic in snics:
|
||||
if snic.family == family:
|
||||
ip_addresses.append(
|
||||
{
|
||||
# We use caps against convention here to make it easier to append
|
||||
# into the JSON payload
|
||||
"Interface": interface,
|
||||
"Address": snic.address,
|
||||
"Netmask": snic.netmask
|
||||
}
|
||||
)
|
||||
return ip_addresses
|
||||
|
||||
def get(self):
|
||||
ipv4 = self.get_ip_addresses(socket.AF_INET)
|
||||
|
||||
payload = [
|
||||
{
|
||||
"Interfaces": []
|
||||
}
|
||||
]
|
||||
for addr in ipv4:
|
||||
payload[0]["Interfaces"].append(addr)
|
||||
|
||||
return payload
|
||||
14
endpoints/load.py
Normal file
14
endpoints/load.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from flask_restful import Resource
|
||||
import psutil
|
||||
|
||||
class Load(Resource):
|
||||
def get(self):
|
||||
loadavg = psutil.getloadavg()
|
||||
payload = [
|
||||
{
|
||||
"1min": loadavg[0],
|
||||
"5min": loadavg[1],
|
||||
"15min": loadavg[2]
|
||||
}
|
||||
]
|
||||
return payload
|
||||
7
endpoints/memory.py
Normal file
7
endpoints/memory.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from flask_restful import Resource, abort
|
||||
|
||||
class Memory(Resource):
|
||||
def get(self):
|
||||
vmem_usage = psutil.virtual_memory()
|
||||
smem_usage = psutil.swap_memory()
|
||||
abort(501, message="Not currently implemented.")
|
||||
12
endpoints/teapot.py
Normal file
12
endpoints/teapot.py
Normal file
@@ -0,0 +1,12 @@
|
||||
from flask_restful import Resource
|
||||
|
||||
class Teapot(Resource):
|
||||
def get(self):
|
||||
payload = [
|
||||
{
|
||||
"tea": "available",
|
||||
"height": "short",
|
||||
"width": "stout"
|
||||
}
|
||||
]
|
||||
return payload
|
||||
19
endpoints/uptime.py
Normal file
19
endpoints/uptime.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from flask_restful import Resource
|
||||
import datetime
|
||||
|
||||
class Uptime(Resource):
|
||||
def get(self):
|
||||
with open("/proc/uptime", "r") as f:
|
||||
secs = float(f.readline().split()[0])
|
||||
delta = datetime.timedelta(seconds=secs)
|
||||
|
||||
payload = [
|
||||
{
|
||||
"days": delta.days,
|
||||
"hours": delta.seconds // 3600,
|
||||
"minutes": delta.seconds // 60,
|
||||
"seconds": delta.seconds
|
||||
}
|
||||
]
|
||||
|
||||
return payload
|
||||
Reference in New Issue
Block a user