88import logging
99import unittest
1010
11+ from binascii import hexlify
12+
13+ from .backend import get_backend
1114from .compat import urlretrieve
12- from .models import get_backend
1315from .server import app
1416
1517
@@ -56,10 +58,12 @@ def index_file(index, filename, url):
5658
5759 with app .app_context ():
5860 backend = get_backend ()
61+ patients = backend .get_manager ('patients' )
62+ vocabularies = backend .get_manager ('vocabularies' )
5963 index_funcs = {
60- 'hpo' : backend . vocabularies .index_hpo ,
61- 'genes' : backend . vocabularies .index_genes ,
62- 'patients' : backend . patients .index ,
64+ 'hpo' : vocabularies .index_hpo ,
65+ 'genes' : vocabularies .index_genes ,
66+ 'patients' : patients .index_file ,
6367 }
6468 index_funcs [index ](filename = filename )
6569
@@ -73,11 +77,87 @@ def fetch_resource(filename, url):
7377 logger .info ('Saved file to: {}' .format (filename ))
7478
7579
80+ def list_servers (direction = 'out' ):
81+ with app .app_context ():
82+ backend = get_backend ()
83+ servers = backend .get_manager ('servers' )
84+ response = servers .list (direction = direction )
85+ # print header
86+ fields = response ['fields' ]
87+ print ('\t ' .join (fields ))
88+
89+ for server in response .get ('rows' , []):
90+ print ('\t ' .join ([repr (server [field ]) for field in fields ]))
91+
92+ def list_clients ():
93+ return list_servers (direction = 'in' )
94+
95+ def add_server (id , direction = 'out' , key = None , label = None , base_url = None ):
96+ if not label :
97+ label = id
98+
99+ if direction == 'out' and not base_url :
100+ raise Exception ('base-url must be specified for outgoing servers' )
101+
102+ with app .app_context ():
103+ backend = get_backend ()
104+ servers = backend .get_manager ('servers' )
105+ # Generate a random key if one was not provided
106+ if key is None :
107+ key = hexlify (os .urandom (30 )).decode ()
108+ servers .add (server_id = id , server_key = key , direction = direction , server_label = label , base_url = base_url )
109+
110+ def add_client (id , key = None , label = None ):
111+ add_server (id , 'in' , key = key , label = label )
112+
113+ def remove_server (id , direction = 'out' ):
114+ with app .app_context ():
115+ backend = get_backend ()
116+ servers = backend .get_manager ('servers' )
117+ servers .remove (server_id = id , direction = direction )
118+
119+ def remove_client (id ):
120+ remove_server (id , direction = 'in' )
121+
122+
76123def run_tests ():
77124 suite = unittest .TestLoader ().discover ('.' .join ([__package__ , 'tests' ]))
78125 unittest .TextTestRunner ().run (suite )
79126
80127
128+ def add_server_subcommands (parser , direction ):
129+ """Add subparser for incoming or outgoing servers
130+
131+ direction - 'in': incoming servers, 'out': outgoing servers
132+ """
133+ server_type = 'client' if direction == 'in' else 'server'
134+ subparsers = parser .add_subparsers (title = 'subcommands' )
135+ subparser = subparsers .add_parser ('add' , description = "Add {} authorization" .format (server_type ))
136+ subparser .add_argument ("id" , help = "A unique {} identifier" .format (server_type ))
137+ if server_type == 'server' :
138+ subparser .add_argument ("base_url" , help = "The base HTTPS URL for sending API requests to the server (e.g., <base-url>/match should be a valid endpoint)." )
139+
140+ subparser .add_argument ("--key" , help = "The secret key used to authenticate requests to/from the {} (default: randomly generate a secure key)" .format (server_type ))
141+ subparser .add_argument ("--label" , help = "The display name for the {}" .format (server_type ))
142+ if server_type == 'server' :
143+ subparser .set_defaults (function = add_server )
144+ else :
145+ subparser .set_defaults (function = add_client )
146+
147+ subparser = subparsers .add_parser ('rm' , description = "Remove {} authorization" .format (server_type ))
148+ subparser .add_argument ("id" , help = "The {} identifier" .format (server_type ))
149+ if server_type == 'server' :
150+ subparser .set_defaults (function = remove_server )
151+ else :
152+ subparser .set_defaults (function = remove_client )
153+
154+ subparser = subparsers .add_parser ('list' , description = "List {} authorizations" .format (server_type ))
155+ if server_type == 'server' :
156+ subparser .set_defaults (function = list_servers )
157+ else :
158+ subparser .set_defaults (function = list_clients )
159+
160+
81161def parse_args (args ):
82162 from argparse import ArgumentParser
83163
@@ -116,6 +196,12 @@ def parse_args(args):
116196 help = "The host the server will listen to (0.0.0.0 to listen globally; 127.0.0.1 to listen locally; default: %(default)s)" )
117197 subparser .set_defaults (function = app .run )
118198
199+ subparser = subparsers .add_parser ('servers' , description = "Server authorization sub-commands" )
200+ add_server_subcommands (subparser , direction = 'out' )
201+
202+ subparser = subparsers .add_parser ('clients' , description = "Client authorization sub-commands" )
203+ add_server_subcommands (subparser , direction = 'in' )
204+
119205 subparser = subparsers .add_parser ('test' , description = "Run tests" )
120206 subparser .set_defaults (function = run_tests )
121207
0 commit comments