Error handling
This commit is contained in:
parent
9258334583
commit
5440409b34
19
twvdscli.py
19
twvdscli.py
@ -1,6 +1,8 @@
|
|||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
import sys
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
import typer
|
import typer
|
||||||
import os
|
import os
|
||||||
@ -20,6 +22,8 @@ class Server:
|
|||||||
url=url,
|
url=url,
|
||||||
headers=reqHeader
|
headers=reqHeader
|
||||||
)
|
)
|
||||||
|
if not result.ok:
|
||||||
|
return None
|
||||||
return result.json()
|
return result.json()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@ -49,18 +53,30 @@ class Server:
|
|||||||
|
|
||||||
def get_balance():
|
def get_balance():
|
||||||
response = requests.get("https://public-api.timeweb.com/api/v1/accounts/finances", headers=reqHeader)
|
response = requests.get("https://public-api.timeweb.com/api/v1/accounts/finances", headers=reqHeader)
|
||||||
|
if not response.ok:
|
||||||
|
print(typer.style("Error", fg=typer.colors.RED))
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
return response.json()
|
return response.json()
|
||||||
|
|
||||||
|
|
||||||
@servers_app.command("start")
|
@servers_app.command("start")
|
||||||
def vds_start(vds_id: int = typer.Argument(...)):
|
def vds_start(vds_id: int = typer.Argument(...)):
|
||||||
result = Server.start(vds_id)
|
result = Server.start(vds_id)
|
||||||
|
if result is None:
|
||||||
|
print(typer.style("Error", fg=typer.colors.RED))
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
print(result)
|
print(result)
|
||||||
|
|
||||||
|
|
||||||
@servers_app.command("stop")
|
@servers_app.command("stop")
|
||||||
def vds_stop(vds_id: int = typer.Argument(...)):
|
def vds_stop(vds_id: int = typer.Argument(...)):
|
||||||
result = Server.stop(vds_id)
|
result = Server.stop(vds_id)
|
||||||
|
if result is None:
|
||||||
|
print(typer.style("Error", fg=typer.colors.RED))
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
print(result)
|
print(result)
|
||||||
|
|
||||||
|
|
||||||
@ -68,6 +84,9 @@ def vds_stop(vds_id: int = typer.Argument(...)):
|
|||||||
def vds_list():
|
def vds_list():
|
||||||
list_of_servers = Server.get_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'))
|
# 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']:
|
for i in list_of_servers['servers']:
|
||||||
if i['status'] == 'on':
|
if i['status'] == 'on':
|
||||||
state = typer.style("Running", fg=typer.colors.GREEN)
|
state = typer.style("Running", fg=typer.colors.GREEN)
|
||||||
|
Loading…
Reference in New Issue
Block a user