Skip to content
This repository was archived by the owner on May 5, 2025. It is now read-only.

Commit 1002569

Browse files
authored
Merge pull request #36 from kevinsteves/summit-credentials
Add initial Credentials() class support.
2 parents 52c9434 + 84151fb commit 1002569

File tree

1 file changed

+81
-31
lines changed

1 file changed

+81
-31
lines changed

bin/summit.py

Lines changed: 81 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@
4242
libpath = os.path.dirname(os.path.abspath(__file__))
4343
sys.path[:0] = [os.path.join(libpath, os.pardir)]
4444

45-
from pancloud import HTTPClient, LoggingService, EventService, \
46-
DirectorySyncService, __version__
45+
from pancloud import Credentials, HTTPClient, LoggingService, \
46+
EventService, DirectorySyncService, __version__
4747

4848
INDENT = 2
4949
LOGGING_SERVICE_EPOCH = 1504224000 # 2017-09-01T00:00:00+00:00
@@ -70,21 +70,74 @@ def main():
7070
file=sys.stderr)
7171
sys.exit(1)
7272

73+
if options['credentials']:
74+
credentials_ = credentials(options)
75+
else:
76+
credentials_ = None
77+
78+
if options['http_client']:
79+
httpclient_ = httpclient(options, credentials_)
80+
else:
81+
httpclient_ = None
82+
7383
if options['logging_api']:
74-
logging(options)
84+
logging(options, httpclient_)
7585

7686
if options['event_api']:
7787
if options['id'] is None:
7888
options['id'] = DEFAULT_EVENT_CHANNEL_ID
79-
event(options)
89+
event(options, httpclient_)
8090

8191
if options['directory_sync_api']:
82-
directory_sync(options)
92+
directory_sync(options, httpclient_)
8393

8494
sys.exit(0)
8595

8696

87-
def logging(options):
97+
def credentials(options):
98+
def write_credentials(c, options):
99+
action = inspect.stack()[0][3]
100+
k = 'Credentials:write'
101+
102+
R = options['R']
103+
try:
104+
c.write_credentials(**R['R2_obj'][k])
105+
except Exception as e:
106+
print_exception(action, e)
107+
sys.exit(1)
108+
109+
action = inspect.stack()[0][3]
110+
k = 'Credentials'
111+
112+
R = options['R']
113+
try:
114+
x = Credentials(**R['R0_obj'][k])
115+
except Exception as e:
116+
print_exception(action, e)
117+
sys.exit(1)
118+
119+
if options['write']:
120+
write_credentials(x, options)
121+
122+
return x
123+
124+
125+
def httpclient(options, c):
126+
action = inspect.stack()[0][3]
127+
k = 'HTTPClient'
128+
129+
R = options['R']
130+
try:
131+
x = HTTPClient(credentials=c,
132+
**R['R0_obj'][k])
133+
except Exception as e:
134+
print_exception(action, e)
135+
sys.exit(1)
136+
137+
return x
138+
139+
140+
def logging(options, session):
88141
def query(api, options):
89142
action = inspect.stack()[0][3]
90143
k = 'LoggingService:query'
@@ -171,14 +224,10 @@ def write(api, options):
171224
action = inspect.stack()[0][3]
172225
k = 'LoggingService'
173226

227+
R = options['R']
174228
try:
175-
R = options['R']
176-
if options['http_client']:
177-
session = HTTPClient(**R['R0_obj']['HTTPClient'])
178-
api = LoggingService(session=session,
179-
**R['R0_obj'][k])
180-
else:
181-
api = LoggingService(**R['R0_obj'][k])
229+
api = LoggingService(session=session,
230+
**R['R0_obj'][k])
182231
except Exception as e:
183232
print_exception(action, e)
184233
sys.exit(1)
@@ -205,7 +254,7 @@ def write(api, options):
205254
write(api, options)
206255

207256

208-
def event(options):
257+
def event(options, session):
209258
def generic(api, options, func, action, k):
210259
R = options['R']
211260
try:
@@ -286,14 +335,10 @@ def nack(api, options):
286335
action = inspect.stack()[0][3]
287336
k = 'EventService'
288337

338+
R = options['R']
289339
try:
290-
R = options['R']
291-
if options['http_client']:
292-
session = HTTPClient(**R['R0_obj']['HTTPClient'])
293-
api = EventService(session=session,
294-
**R['R0_obj'][k])
295-
else:
296-
api = EventService(**R['R0_obj'][k])
340+
api = EventService(session=session,
341+
**R['R0_obj'][k])
297342
except Exception as e:
298343
print_exception(action, e)
299344
sys.exit(1)
@@ -323,7 +368,7 @@ def nack(api, options):
323368
nack(api, options)
324369

325370

326-
def directory_sync(options):
371+
def directory_sync(options, session):
327372
def generic(api, options, func, action, k):
328373
R = options['R']
329374
try:
@@ -383,14 +428,10 @@ def attributes(api, options):
383428
action = inspect.stack()[0][3]
384429
k = 'DirectorySyncService'
385430

431+
R = options['R']
386432
try:
387-
R = options['R']
388-
if options['http_client']:
389-
session = HTTPClient(**R['R0_obj']['HTTPClient'])
390-
api = DirectorySyncService(session=session,
391-
**R['R0_obj'][k])
392-
else:
393-
api = DirectorySyncService(**R['R0_obj'][k])
433+
api = DirectorySyncService(session=session,
434+
**R['R0_obj'][k])
394435
except Exception as e:
395436
print_exception(action, e)
396437
sys.exit(1)
@@ -666,6 +707,7 @@ def parse_opts():
666707
options_R = {
667708
# class init **kwargs
668709
'R0': {
710+
'Credentials': [],
669711
'HTTPClient': [],
670712
'LoggingService': [],
671713
'EventService': [],
@@ -687,6 +729,8 @@ def parse_opts():
687729
},
688730
# class method **kwargs
689731
'R2': {
732+
'Credentials:write': [],
733+
690734
'LoggingService:delete': [],
691735
'LoggingService:poll': [],
692736
'LoggingService:xpoll': [],
@@ -709,6 +753,7 @@ def parse_opts():
709753

710754
options = {
711755
'R': options_R,
756+
'credentials': False,
712757
'http_client': False,
713758
'logging_api': False,
714759
'directory_sync_api': False,
@@ -755,7 +800,7 @@ def _options_R(k, last_c, last_m, arg):
755800
file=sys.stderr)
756801
sys.exit(1)
757802

758-
short_options = 'HLDEJ:pj'
803+
short_options = 'CHLDEJ:pj'
759804
long_options = [
760805
'delete', 'poll', 'xpoll', 'query', 'write',
761806
'start=', 'end=',
@@ -780,6 +825,9 @@ def _options_R(k, last_c, last_m, arg):
780825
for opt, arg in opts:
781826
if False:
782827
pass
828+
elif opt == '-C':
829+
options['credentials'] = True
830+
last_c = 'Credentials'
783831
elif opt == '-H':
784832
options['http_client'] = True
785833
last_c = 'HTTPClient'
@@ -875,7 +923,7 @@ def _options_R(k, last_c, last_m, arg):
875923
assert False, 'unhandled option %s' % opt
876924

877925
headers = None
878-
if 'ACCESS_TOKEN' in os.environ:
926+
if not options['credentials'] and 'ACCESS_TOKEN' in os.environ:
879927
headers = {
880928
'Authorization': 'Bearer %s' % os.environ['ACCESS_TOKEN'],
881929
'Content-Type': 'application/json',
@@ -930,6 +978,8 @@ def usage():
930978
--attributes get directory attributes
931979
--id class objectClass
932980
-H use HTTPClient() session
981+
-C use Credentials() class
982+
--write write_credentials() method
933983
--R0 json class constructor args (**kwargs)
934984
--R1 json class method body/QUERY_STRING (data/params)
935985
--R2 json class method args (**kwargs)

0 commit comments

Comments
 (0)