boyska 4 years ago
commit
56f045bfa2
2 changed files with 107 additions and 0 deletions
  1. 100 0
      cli.py
  2. 7 0
      requirements.txt

+ 100 - 0
cli.py

@@ -0,0 +1,100 @@
+#!/usr/bin/env python3
+import sys
+from pprint import pprint
+import requests
+import argparse
+
+try:
+    from colors import color
+except ImportError:
+    def color(text, *args, **kwargs):
+        return text
+
+
+def req(args, location, data=None):
+    kwargs = {}
+    if data is not None:
+        kwargs['json'] = data
+    r = requests.post(args.endpoint + location,
+        auth=tuple(args.auth.split(":", 2)),
+                         **kwargs
+                        )
+    return r
+
+def main_channel_list(args):
+    r = req(args, "/rsGxsChannels/getChannelsSummaries")
+    channels = r.json()['channels']
+    for chan in channels:
+        print(color(chan['mGroupName'], style='bold', fg='green'))
+        print('   ' + color(chan['mGroupId'], style='underline'))
+
+def main_channel_show(args):
+    r = req(
+        args, "/rsGxsChannels/getChannelsInfo",
+        {
+            'chanIds': [args.channel_id]
+        }
+    )
+    data = r.json()
+    channels = data['channelsInfo']
+    for chan in channels:
+        print(color(chan['mMeta']['mGroupName'], style='bold', fg='green'))
+        print('   ' + color(chan['mMeta']['mGroupId'], style='underline'))
+        print('   ' + chan['mDescription'])
+        print(color('Last Post:', style='bold') + ' \t%s' % chan['mMeta']['mLastPost'])
+
+
+def main_channel_post(args):
+    chid = args.channel_id
+    r = req(
+        args, "/rsGxsChannels/createPostV2",
+        {
+            'channelId': args.channel_id,
+            'title': args.post_title,
+            'mBody': args.post_body,
+        }
+    )
+    ret = r.json()
+    if ret.get('retval', True) is False:
+        print(color('ERROR: could not create post', fg='red', style='bold'))
+        print(ret['errorMessage'])
+        pprint(ret)
+        sys.exit(1)
+    pprint(ret)
+
+
+def get_parser():
+    p = argparse.ArgumentParser()
+    p.add_argument("--endpoint", default="http://127.0.0.1:9092")
+    p.add_argument("-u", "--auth", dest="auth")
+
+    p_sub = p.add_subparsers()
+
+    ch = p_sub.add_parser("channel")
+    ch.add_argument("--channel-id")
+    ch_sub = ch.add_subparsers()
+    ch_list = ch_sub.add_parser("list")
+    ch_list.set_defaults(mainfunc=main_channel_list)
+
+    ch_show = ch_sub.add_parser("show")
+    ch_show.set_defaults(mainfunc=main_channel_show)
+
+    ch_post = ch_sub.add_parser("post")
+    ch_post.set_defaults(mainfunc=main_channel_post)
+    ch_post.add_argument('--post-title')
+    ch_post.add_argument('--post-body')
+
+    return p
+
+
+def main():
+    p = get_parser()
+    args = p.parse_args()
+    if getattr(args, "mainfunc", None) is None:
+        print("Not a complete command")
+        sys.exit(2)
+    args.mainfunc(args)
+
+
+if __name__ == "__main__":
+    main()

+ 7 - 0
requirements.txt

@@ -0,0 +1,7 @@
+ansicolors==1.1.8
+certifi==2019.11.28
+chardet==3.0.4
+idna==2.8
+pkg-resources==0.0.0
+requests==2.22.0
+urllib3==1.25.7