summaryrefslogtreecommitdiffstats
path: root/ogcli
diff options
context:
space:
mode:
authorRoberto Hueso Gómez <rhueso@soleta.eu>2020-06-19 12:23:59 +0200
committerRoberto Hueso Gómez <rhueso@soleta.eu>2020-06-19 12:23:59 +0200
commitdbdfc106bfc599ba86c4870b342e684eaf0b2476 (patch)
tree13ac14b82d2b9cb56ae343b656fe56e4fd68b4b0 /ogcli
parentd7b54d08bbcd55029daf57f726784950276accb3 (diff)
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.).
Diffstat (limited to 'ogcli')
-rw-r--r--ogcli/objects/__init__.py0
-rw-r--r--ogcli/objects/og_client.py6
-rw-r--r--ogcli/ogcli.py32
3 files changed, 29 insertions, 9 deletions
diff --git a/ogcli/objects/__init__.py b/ogcli/objects/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/ogcli/objects/__init__.py
diff --git a/ogcli/objects/og_client.py b/ogcli/objects/og_client.py
new file mode 100644
index 0000000..ca91036
--- /dev/null
+++ b/ogcli/objects/og_client.py
@@ -0,0 +1,6 @@
+class OgClient():
+
+ @staticmethod
+ def list_clients(rest):
+ r = rest.get('/clients')
+ print(r.json())
diff --git a/ogcli/ogcli.py b/ogcli/ogcli.py
index d383e2d..37fb2e0 100644
--- a/ogcli/ogcli.py
+++ b/ogcli/ogcli.py
@@ -1,18 +1,32 @@
+from ogcli.objects.og_client import OgClient
+import argparse
import requests
+import sys
-class OgCLI():
- def __init__(self, cfg):
- self.api_token = cfg['api_token']
+class OgREST():
+ def __init__(self, ip, port, api_token):
+ self.URL = f'http://{ip}:{port}'
+ self.HEADERS = {'Authorization' : api_token}
- def client_list(self):
- headers = {'Authorization' : self.api_token}
+ def get(self, path):
try:
- r = requests.get('http://127.0.0.1:8888/clients',
- headers=headers)
+ 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")
+ 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.rest = OgREST(cfg['ip'], cfg['port'], cfg['api_token'])
+
+ 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)