Compare commits

...

2 Commits

Author SHA1 Message Date
3898468084 Balance 2022-04-07 15:41:50 +03:00
5440409b34 Error handling 2022-04-07 15:38:11 +03:00

View File

@ -1,6 +1,8 @@
#!/usr/bin/python3
import json
import sys
import requests
import typer
import os
@ -20,6 +22,8 @@ class Server:
url=url,
headers=reqHeader
)
if not result.ok:
return None
return result.json()
@staticmethod
@ -47,20 +51,35 @@ class Server:
return result.json()
@app.command("balance")
def get_balance():
response = requests.get("https://public-api.timeweb.com/api/v1/accounts/finances", headers=reqHeader)
return response.json()
if not response.ok:
print(typer.style("Error", fg=typer.colors.RED))
sys.exit(1)
else:
response = response.json()
print('{0:6} {1:}'.format('balance', 'monthly_cost'))
print('{0:6} {1:}'.format(response['finances']['balance'], response['finances']['monthly_cost']))
@servers_app.command("start")
def vds_start(vds_id: int = typer.Argument(...)):
result = Server.start(vds_id)
if result is None:
print(typer.style("Error", fg=typer.colors.RED))
sys.exit(1)
print(result)
@servers_app.command("stop")
def vds_stop(vds_id: int = typer.Argument(...)):
result = Server.stop(vds_id)
if result is None:
print(typer.style("Error", fg=typer.colors.RED))
sys.exit(1)
print(result)
@ -68,6 +87,9 @@ def vds_stop(vds_id: int = typer.Argument(...)):
def vds_list():
list_of_servers = Server.get_list()
# print('{0:7} {1:17} {5:16} {2:2} {3:5} {4:4}'.format('state', 'name', 'vcpus', 'memory', 'disk', 'ip'))
if list_of_servers is None:
print(typer.style("Error", fg=typer.colors.RED))
sys.exit(1)
for i in list_of_servers['servers']:
if i['status'] == 'on':
state = typer.style("Running", fg=typer.colors.GREEN)