CLI: add optional support to highlight with colors the JSON responses

This commit is contained in:
Nicola Murino 2019-08-08 14:20:42 +02:00
parent a7158634a0
commit 9f5722a894
2 changed files with 40 additions and 13 deletions

View file

@ -1,6 +1,12 @@
## REST API CLI client
`sftpgo_api_cli.py` is a very simple command line client for `SFTPGo` REST API written in python. It requires python3 and the python HTTP library [Requests](https://2.python-requests.org/en/master/ "Requests") to run.
`sftpgo_api_cli.py` is a very simple command line client for `SFTPGo` REST API written in python.
It has the following requirements:
- python3 or python2
- python [Requests](https://2.python-requests.org/en/master/ "Requests") module
- Optionally, if the python module [Pygments](http://pygments.org/ "Pygments") 1.5 or above is installed, the JSON responses will be highlighted with colors.
You can see the usage with the following command:
@ -14,14 +20,15 @@ and
python sftpgo_api_cli.py [sub-command] --help
```
Basically there is a subcommand for each REST API and a some global arguments:
Basically there is a sub command for each REST API and the following global arguments:
- `--debug`, default disabled, print useful debug info.
- `--base-url`, default `http://127.0.0.1:8080`. Base URL for SFTPGo REST API
- `--auth-type`, HTTP auth type. Supported HTTP auth type are `basic` and `digest`. Default none
- `--auth-user`, user for HTTP authentication
- `--auth-password`, password for HTTP authentication
- `--secure`, disable to ignore verifying the SSL certificate. Default enabled
- `--insecure`, enable to ignore verifying the SSL certificate. Default disabled
- `--no-color`, disable color highligth for JSON responses. You need python pygments module 1.5 or above for this to work. Default disabled if pygments is found, enabled if not found
For each subcommand `--help` shows the available arguments, try for example:
@ -119,7 +126,7 @@ Output:
Command:
```
python sftpgo_api_cli.py get_users --limit 1 --offset 0 --username test_username --order DESC
python sftpgo_api_cli.py get-users --limit 1 --offset 0 --username test_username --order DESC
```
Output:

View file

@ -9,10 +9,17 @@ try:
except ImportError:
import urlparse
try:
import pygments
from pygments.lexers import JsonLexer
from pygments.formatters import TerminalFormatter
except ImportError:
pygments = None
class SFTPGoApiRequests:
def __init__(self, debug, baseUrl, authType, authUser, authPassword, secure):
def __init__(self, debug, baseUrl, authType, authUser, authPassword, secure, no_color):
self.userPath = urlparse.urljoin(baseUrl, '/api/v1/user')
self.quotaScanPath = urlparse.urljoin(baseUrl, '/api/v1/quota_scan')
self.activeConnectionsPath = urlparse.urljoin(baseUrl, '/api/v1/sftp_connection')
@ -25,18 +32,27 @@ class SFTPGoApiRequests:
else:
self.auth = None
self.verify = secure
self.no_color = no_color
def formatAsJSON(self, text):
if not text:
return ""
return json.dumps(json.loads(text), indent=2)
json_string = json.dumps(json.loads(text), sort_keys=True, indent=2)
if not self.no_color and pygments:
return pygments.highlight(json_string, JsonLexer(), TerminalFormatter())
return json_string
def printResponse(self, r):
if "content-type" in r.headers and "application/json" in r.headers["content-type"]:
if self.debug:
print("executed request: {} {} - status code: {} request body: {}".format(
r.request.method, r.url, r.status_code, self.formatAsJSON(r.request.body)))
print("got response, status code: {} body:".format(r.status_code))
if pygments is None:
print('')
print('Response color highlight is not available: you need pygments 1.5 or above.')
print('')
print("Executed request: {} {} - request body: {}".format(
r.request.method, r.url, self.formatAsJSON(r.request.body)))
print('')
print("Got response, status code: {} body:".format(r.status_code))
print(self.formatAsJSON(r.text))
else:
print(r.text)
@ -132,14 +148,17 @@ if __name__ == '__main__':
parser.add_argument('-a', '--auth-type', type=str, default=None, choices=['basic', 'digest'],
help='HTTP authentication type. Default: %(default)s')
parser.add_argument("-u", "--auth-user", type=str, default="",
help='User to use for HTTP authentication. Default: %(default)s')
help='User for HTTP authentication. Default: %(default)s')
parser.add_argument('-p', '--auth-password', type=str, default='',
help='Password to use for HTTP authentication. Default: %(default)s')
help='Password for HTTP authentication. Default: %(default)s')
parser.add_argument('-d', '--debug', dest='debug', action='store_true')
parser.set_defaults(debug=False)
parser.add_argument('-s', '--secure', dest='secure', action='store_true',
parser.add_argument('-i', '--insecure', dest='secure', action='store_false',
help='Set to false to ignore verifying the SSL certificate')
parser.set_defaults(secure=True)
parser.add_argument('-t', '--no-color', dest='no_color', action='store_true',
help='Disable color highlight for JSON responses. You need python pygments module 1.5 or above to have highlighted output')
parser.set_defaults(no_color=(pygments is None))
subparsers = parser.add_subparsers(dest='command', help='sub-command --help')
subparsers.required = True
@ -180,7 +199,8 @@ if __name__ == '__main__':
args = parser.parse_args()
api = SFTPGoApiRequests(args.debug, args.base_url, args.auth_type, args.auth_user, args.auth_password, args.secure)
api = SFTPGoApiRequests(args.debug, args.base_url, args.auth_type, args.auth_user, args.auth_password, args.secure,
args.no_color)
if args.command == 'add-user':
api.addUser(args.username, args.password, args.public_keys, args.home_dir,