Rewrite basic structure

This commit:
- Adds OgREST class as a wrapper for HTTP REST requests.
- Adds objects folder which will contain available functions for each object in
  the OpenGnsys ecosystem (e.g. client, image, etc.).
master
Roberto Hueso Gómez 2020-06-19 12:23:59 +02:00
parent d7b54d08bb
commit dbdfc106bf
4 changed files with 35 additions and 23 deletions

View File

@ -14,7 +14,7 @@ class CLI():
self.cfg = json.load(json_file)
except json.JSONDecodeError:
sys.exit(f'ERROR: Failed parse malformed JSON file '
f'{OG_CLI_CFG_PATH}')
f'{OG_CLI_CFG_PATH}')
except:
sys.exit(f'ERROR: cannot open {OG_CLI_CFG_PATH}')
@ -24,20 +24,12 @@ class CLI():
parser.add_argument('command', help='Subcommand to run')
args = parser.parse_args(sys.argv[1:2])
if not hasattr(self, args.command):
if not hasattr(self.ogcli, args.command):
parser.print_help()
sys.exit('Unknown command')
# Call the command with the same name.
getattr(self, args.command)(sys.argv[2:])
def list(self, args):
parser = argparse.ArgumentParser()
parser.add_argument('item', choices=['clients'])
parser.parse_args(args)
if parser.item == 'clients':
self.ogcli.client_list()
getattr(self.ogcli, args.command)(sys.argv[2:])
if __name__ == "__main__":
CLI()

View File

View File

@ -0,0 +1,6 @@
class OgClient():
@staticmethod
def list_clients(rest):
r = rest.get('/clients')
print(r.json())

View File

@ -1,18 +1,32 @@
from ogcli.objects.og_client import OgClient
import argparse
import requests
import sys
class OgREST():
def __init__(self, ip, port, api_token):
self.URL = f'http://{ip}:{port}'
self.HEADERS = {'Authorization' : api_token}
def get(self, path):
try:
r = requests.get(f'{self.URL}/clients',
headers=self.HEADERS)
if r.status_code != 200:
sys.exit(f"Cannot connect to ogServer: "
f"{r.status_code} HTTP status code")
except IOError as e:
sys.exit(f"Cannot connect to ogServer: {e}")
return r
class OgCLI():
def __init__(self, cfg):
self.api_token = cfg['api_token']
self.rest = OgREST(cfg['ip'], cfg['port'], cfg['api_token'])
def client_list(self):
headers = {'Authorization' : self.api_token}
try:
r = requests.get('http://127.0.0.1:8888/clients',
headers=headers)
if r.status_code != 200:
sys.exit(f"Cannot connect to ogServer: "
f"{r.status_code} HTTP status code")
except IOError as e:
sys.exit(f"Cannot connect to ogServer: {e}")
def list(self, args):
parser = argparse.ArgumentParser()
parser.add_argument('item', choices=['clients'])
args = parser.parse_args(args)
print(r.json())
if args.item == 'clients':
OgClient.list_clients(self.rest)