-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpymauro.py
914 lines (830 loc) · 42.6 KB
/
pymauro.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
"""pymauro is an opensource library that wraps the REST API endpoints of a Mauro Data Mapper instance into a Python
library to allow easy, automated development.
Currently, documentation is lacking but the functionality can be approximately described by viewing the methods
as described in the documentation for the analogous Client library at
https://maurodatamapper.github.io/resources/client/java/#binding-vs-non-binding-clients
Details on the endpoints themselves can be found at:
https://maurodatamapper.github.io/rest-api/introduction/#testing
Classes:
BaseClient
Functions:
test_my_url() - Not proven to work
"""
import requests
def test_my_url(url):
"""
Takes the url (string) and appends /api/test.
This new path is then sent a get request. The response is
returned. This function has not been proven to work - suspected endpoint mistake
:param url: The base url of the Mauro instance
:return: :class:`Response` object
"""
print("This function has not been proven to work - suspected endpoint mistake")
response = requests.get(url + "/api/test")
return response
class BaseClient:
"""
A class to connect to a Mauro Data Mapper instance.
It is recommended that you provide both a username/password and API key as some methods only operate off the
provision of one and not the other.
The user must provide at least a username and password or an API key. A type error will return if the arguments
provided do not abide by this rule. When possible the called method will use the API key over the session id created
by login via username/password to prevent session time-outs.
Attributes
----------
baseurl : str
The base URL of the Mauro instance
username : str
Login username
password : str
Login password
api_key: str
The API key to authenticate
Methods
-------
test_my_connection
check_for_valid_session
logout
admin_check
list_api_keys
create_new_api_key
delete_api_key
enable_api_key
disable_api_key
refresh_api_key
list_folders
get_metadata
permissions
post_metadata
get_classifiers
get_data_classes
get_codesets
get_data_element
get_data_model
get_versioned_folder
build_versioned_folder
create_data_model
build_folder
create_new_data_class
update_data_class
create_data_element
method_constructor
Each method possesses its own docstring.
"""
def __init__(self, baseurl, username=None, password=None, api_key=None):
self._baseURL = baseurl # Non-public to prevent accidental editing
self._username = username # Non-public to prevent accidental editing
self.__password = password # Name mangled to prevent accidental disclosure
self.api_key = api_key
if (self._username is None or self.__password is None) and self.api_key is None \
or self._username is not None and self.__password is None \
or self._username is None and self.__password is not None:
raise TypeError("You must provide at a minimum: the username and password as a pairing or an API Key.")
if self.api_key is not None:
self.headers = dict()
self.headers['apiKey'] = self.api_key
if self._username is not None and 'id' in self.test_my_connection().json().keys():
self.cookie = self.test_my_connection().cookies
else:
self.cookie = None
@property
def username(self):
return self._username
@property
def baseURL(self):
return self._baseURL
def __repr__(self):
return "Mauro Client Object"
def test_my_connection(self):
"""
Executes a post request with username and password as json payload to the baseurl appended with
/api/authentication/login. The response is returned.
:return: :class:`Response' object
"""
if self.username is None:
raise TypeError("You must provide a username and password to access this method")
json_payload = dict(username=self.username, password=self.__password)
response = requests.post(self.baseURL + "/api/authentication/login", json=json_payload)
return response
def check_for_valid_session(self):
"""
Executes a get request to the url + /api/session/isAuthenticated
with the session ID in the cookies header.
The response is returned.
:return: :class:`Response' object
"""
if self.username is None:
raise TypeError("You must provide a username and password to access this method")
response = requests.get(self.baseURL + "/api/session/isAuthenticated", cookies=self.cookie)
return response
def logout(self):
"""
A logout get request is sent to baseurl appended with /api/authentication/logout with the session ID in the
cookies header. The response is returned.
:return: :class:`Response' object
"""
if self.username is None:
raise TypeError("You must provide a username and password to access this method")
response = requests.get(self.baseURL + "/api/authentication/logout", cookies=self.cookie)
return response
def admin_check(self):
"""
Get request to determine whether user is an admin.
:return: :class:`Response' object
"""
if self.api_key is not None:
response = requests.get(self.baseURL + "/api/session/isApplicationAdministration",
headers={'apiKey': self.api_key})
else:
response = requests.get(self.baseURL + "/api/session/isApplicationAdministration",
cookies=self.cookie)
return response
def list_api_keys(self, catalogue_user_id=None):
"""
Lists api keys.
If catalogue_user_id is provided, request will return response for request using the provided catalogue
as opposed to current user's default value.
:param catalogue_user_id: - (optional) A catalogue user id
:return: :class:`Response' object
"""
if self.username is None and catalogue_user_id is None:
raise TypeError("A username/password or an id "
"method argument is required for this method to work")
elif self.api_key is None and catalogue_user_id is None:
default_id = self.test_my_connection().json()['id']
response = requests.get(self.baseURL + "/api/catalogueUsers/" + default_id + "/apiKeys",
cookies=self.cookie)
elif self.api_key is None and catalogue_user_id is not None:
response = requests.get(self.baseURL + "/api/catalogueUsers/" + catalogue_user_id + "/apiKeys",
cookies=self.cookie)
elif self.api_key is not None and catalogue_user_id is None:
default_id = self.test_my_connection().json()['id']
response = requests.get(self.baseURL + "/api/catalogueUsers/" + default_id + "/apiKeys",
headers={'apiKey': self.api_key})
elif self.api_key is not None and catalogue_user_id is not None:
response = requests.get(self.baseURL + "/api/catalogueUsers/" + catalogue_user_id + "/apiKeys",
headers={'apiKey': self.api_key})
return response
def create_new_api_key(self, key_name='My First Key', expiry=365, refreshable=True, catalogue_user_id=None):
"""
Creates a new API key depending on arguments provided,
If catalogue_user_id is provided, request will return response for request using the provided catalogue
as opposed to current user's default value.
:param key_name: The name of the created key. Default value is 'My First Key'
:param expiry: int - Number of days until key expiry. Default value is 365.
:param refreshable: bool - Make key refreshable. Default value is True.
:param catalogue_user_id: - (optional) A catalogue user id
:return: :class:`Response' object
"""
if self.username is None and catalogue_user_id is None:
raise TypeError("A username/password or an id "
"method argument is required for this method to work")
elif self.api_key is not None:
json_payload = dict(name=key_name, expiresInDays=expiry, refreshable=refreshable)
if catalogue_user_id is None:
default_id = self.test_my_connection().json()['id']
response = requests.post(self.baseURL + "/api/catalogueUsers/" + default_id + "/apiKeys",
headers={'apiKey': self.api_key}, json=json_payload)
else:
response = requests.post(self.baseURL + "/api/catalogueUsers/" + catalogue_user_id + "/apiKeys",
headers={'apiKey': self.api_key}, json=json_payload)
else:
if catalogue_user_id is None:
json_payload = dict(name=key_name, expiresInDays=expiry, refreshable=refreshable)
default_id = self.test_my_connection().json()['id']
response = requests.post(self.baseURL + "/api/catalogueUsers/" + default_id + "/apiKeys",
cookies=self.cookie, json=json_payload)
else:
json_payload = dict(name=key_name, expiresInDays=expiry, refreshable=refreshable)
response = requests.post(self.baseURL + "/api/catalogueUsers/" + catalogue_user_id + "/apiKeys",
cookies=self.cookie, json=json_payload)
return response
def delete_api_key(self, key_to_delete, catalogue_user_id=None):
"""
Deletes API key.
If catalogue_user_id is provided, request will return response for request using the provided catalogue
as opposed to current user's default value.
:param key_to_delete: API key id to delete
:param catalogue_user_id: - (optional) A catalogue user id
:return: :class:`Response' object
"""
if self.username is None and catalogue_user_id is None:
raise TypeError("A username/password or an id "
"method argument is required for this method to work")
elif self.api_key is not None:
if catalogue_user_id is None:
default_id = self.test_my_connection().json()['id']
response = requests.delete(
self.baseURL + "/api/catalogueUsers/" + str(default_id) + "/apiKeys/" + str(key_to_delete),
headers={'apiKey': self.api_key})
else:
response = requests.delete(
self.baseURL + "/api/catalogueUsers/" + catalogue_user_id + "/apiKeys/" + str(key_to_delete),
headers={'apiKey': self.api_key})
return response
else:
if catalogue_user_id is None:
default_id = self.test_my_connection().json()['id']
print(default_id)
response = requests.delete(
self.baseURL + "/api/catalogueUsers/" + str(default_id) + "/apiKeys/" + str(key_to_delete),
cookies=self.cookie)
else:
response = requests.delete(
self.baseURL + "/api/catalogueUsers/" + catalogue_user_id + "/apiKeys/" + str(key_to_delete),
cookies=self.cookie)
return response
def disable_api_key(self, key_to_disable, catalogue_user_id=None):
"""
Disables API key
If catalogue_user_id is provided, request will return response for request using the provided catalogue
as opposed to current user's default value.
:param key_to_disable: API key to disable
:param catalogue_user_id: - (optional) A catalogue user id
:return: :class:`Response' object
"""
if self.username is None and catalogue_user_id is None:
raise TypeError("A username/password or an id "
"method argument is required for this method to work")
elif self.api_key is not None:
if catalogue_user_id is None:
default_id = self.test_my_connection().json()['id']
response = requests.put(self.baseURL + "/api/catalogueUsers/" + str(default_id) + "/apiKeys/"
+ str(key_to_disable) + "/disable", headers={'apiKey': self.api_key})
else:
response = requests.put(self.baseURL + "/api/catalogueUsers/" + catalogue_user_id + "/apiKeys/"
+ str(key_to_disable) + "/disable", headers={'apiKey': self.api_key})
return response
else:
if catalogue_user_id is None:
default_id = self.test_my_connection().json()['id']
response = requests.put(self.baseURL + "/api/catalogueUsers/" + str(default_id) + "/apiKeys/"
+ str(key_to_disable) + "/disable", cookies=self.cookie)
else:
response = requests.put(self.baseURL + "/api/catalogueUsers/" + catalogue_user_id + "/apiKeys/"
+ str(key_to_disable) + "/disable", cookies=self.cookie)
return response
def enable_api_key(self, key_to_enable, catalogue_user_id=None):
"""
Disables API key
If catalogue_user_id is provided, request will return response for request using the provided catalogue
as opposed to current user's default value.
:param key_to_enable: API key to enable
:param catalogue_user_id: - (optional) A catalogue user id
:return: :class:`Response' object
"""
if self.username is None and catalogue_user_id is None:
raise TypeError("A username/password or an id argument is required for this method to work")
elif self.api_key is not None:
if catalogue_user_id is None:
default_id = self.test_my_connection().json()['id']
response = requests.put(self.baseURL + "/api/catalogueUsers/" + default_id + "/apiKeys/"
+ str(key_to_enable) + "/enable", headers={'apiKey': self.api_key})
else:
response = requests.put(self.baseURL + "/api/catalogueUsers/" + catalogue_user_id + "/apiKeys/" +
str(key_to_enable) + "/enable", headers={'apiKey': self.api_key})
else:
if catalogue_user_id is None:
default_id = self.test_my_connection().json()['id']
response = requests.put(self.baseURL + "/api/catalogueUsers/" + str(default_id) + "/apiKeys/"
+ str(key_to_enable) + "/enable", cookies=self.cookie)
else:
response = requests.put(self.baseURL + "/api/catalogueUsers/" + catalogue_user_id + "/apiKeys/" +
str(key_to_enable) + "/enable", cookies=self.cookie)
return response
def refresh_api_key(self, key_to_refresh, days_until_expiry=365, catalogue_user_id=None):
"""
Refreshes API key
If catalogue_user_id is provided, request will return response for request using the provided catalogue
as opposed to current user's default value.
:param key_to_refresh: API key to refresh
:param days_until_expiry: int - Number of days until key expiry. Default value is 365
:param catalogue_user_id: - (optional) A catalogue user id
:return: :class:`Response' object
"""
if self.username is None and catalogue_user_id is None:
raise TypeError("A username/password or an id "
"method argument is required for this method to work")
elif self.api_key is not None:
if catalogue_user_id is None:
default_id = self.test_my_connection().json()['id']
response = requests.put(self.baseURL + "/api/catalogueUsers/" + str(default_id) + "/apiKeys/"
+ str(key_to_refresh) + "/refresh/" + str(days_until_expiry),
headers={'apiKey': self.api_key})
else:
response = requests.put(self.baseURL + "/api/catalogueUsers/" + catalogue_user_id + "/apiKeys/" +
str(key_to_refresh) + "/refresh/" + str(days_until_expiry),
headers={'apiKey': self.api_key})
return response
else:
if catalogue_user_id is None:
default_id = self.test_my_connection().json()['id']
response = requests.put(self.baseURL + "/api/catalogueUsers/" + str(default_id) + "/apiKeys/"
+ str(key_to_refresh) + "/refresh/" + str(days_until_expiry),
cookies=self.cookie)
else:
response = requests.put(self.baseURL + "/api/catalogueUsers/" + catalogue_user_id + "/apiKeys/" +
str(key_to_refresh) + "/refresh/" + str(days_until_expiry),
cookies=self.cookie)
return response
def list_folders(self, offset=0, max_limit=10, show_all=False):
"""
Lists the folders present in a Mauro instance.
:param offset: int - pagination offset value. Default value = 0
:param max_limit: int - maximum number of folders returned. Default value = 10
:param show_all: bool - show all folders (overrides max and offset limit). Default value = False
:return: :class:`Response' object
"""
if self.api_key is not None:
if not show_all:
response = requests.get(self.baseURL + "/api/folders?offset=" + str(offset) + "&max=" + str(max_limit),
headers={'apiKey': self.api_key})
else:
response = requests.get(self.baseURL + "/api/folders?all=true", headers={'apiKey': self.api_key})
else:
if not show_all:
response = requests.get(self.baseURL + "/api/folders?offset=" + str(offset) + "&max=" + str(max_limit),
cookies=self.cookie)
else:
response = requests.get(self.baseURL + "/api/folders?all=true", cookies=self.cookie)
return response
def get_metadata(self, catalogue_item_domain_type, catalogue_item_id, metadata_id=None):
"""
Get the metadata information on a catalogue item or metadata item within a catalogue id.
:param catalogue_item_domain_type: Must be one of "folders", "dataModels", "dataClasses", "dataTypes",
"terminologies", "terms" or "referenceDataModels".
:param catalogue_item_id: The id of the catalogue item.
:param metadata_id: - (optional) A catalogue user id.
:return: :class:`Response' object.
"""
val_domain_types = ["folders", "dataModels", "dataClasses", "dataTypes", "terminologies", "terms",
"referenceDataModels"]
if catalogue_item_domain_type not in val_domain_types:
raise ValueError("catalogueItemDomainType must be in " + str(val_domain_types))
if self.api_key is not None:
if metadata_id is None:
response = requests.get(
self.baseURL + "/api/" + str(catalogue_item_domain_type) + "/" + str(
catalogue_item_id) + "/metadata",
headers={'apiKey': self.api_key})
else:
response = requests.get(
self.baseURL + "/api/" + str(catalogue_item_domain_type) + "/" + str(
catalogue_item_id) + "/metadata/" + str(metadata_id),
headers={'apiKey': self.api_key})
return response
else:
if metadata_id is None:
response = requests.get(
self.baseURL + "/api/" + str(catalogue_item_domain_type) + "/" + str(
catalogue_item_id) + "/metadata",
cookies=self.cookie)
else:
response = requests.get(
self.baseURL + "/api/" + str(catalogue_item_domain_type) + "/" + str(
catalogue_item_id) + "/metadata/" + str(metadata_id),
cookies=self.cookie)
return response
def permissions(self, catalogue_item_domain_type, catalogue_item_id):
"""
Get the permissions of a catalogue item id
:param catalogue_item_domain_type: Must be one of "folders", "dataModels", "dataClasses",
"dataTypes", "terminologies", "terms" or "referenceDataModels"
:param catalogue_item_id: The catalogue item id
:return: :class:`Response' object
"""
val_domain_types = ["folders", "dataModels", "dataClasses", "dataTypes", "terminologies", "terms",
"referenceDataModels"]
if catalogue_item_domain_type not in val_domain_types:
raise ValueError("catalogueItemDomainType must be in " + str(val_domain_types))
if self.api_key is not None:
response = requests.get(self.baseURL + "/api/" + str(catalogue_item_domain_type) + "/" +
str(catalogue_item_id) + "/permissions",
headers={'apiKey': self.api_key})
return response
else:
response = requests.get(
self.baseURL + "/api/" + str(catalogue_item_domain_type) + "/" + str(catalogue_item_id) +
"/permissions", cookies=self.cookie)
return response
def post_metadata(self, catalogue_item_domain_type, catalogue_item_id, namespace_inp, key_val, value_inp):
"""
Post metadata
:param catalogue_item_domain_type: Must be one of "folders", "dataModels", "dataClasses",
"dataTypes", "terminologies", "terms" or "referenceDataModels".
:param catalogue_item_id: The catalogue item id.
:param namespace_inp: The namespace
:param key_val: The key
:param value_inp: The value
:return: :class:`Response' object
"""
val_domain_types = ["folders", "dataModels", "dataClasses", "dataTypes", "terminologies", "terms",
"referenceDataModels"]
if catalogue_item_domain_type not in val_domain_types:
raise ValueError("catalogueItemDomainType must be in " + str(val_domain_types))
json_payload = dict(id=catalogue_item_id, namespace=namespace_inp, key=key_val, value=value_inp)
if self.api_key is not None:
response = requests.post(
self.baseURL + "/api/" + str(catalogue_item_domain_type) + "/" + str(catalogue_item_id) + "/metadata",
headers={'apiKey': self.api_key}, json=json_payload)
return response
else:
response = requests.post(
self.baseURL + "/api/" + str(catalogue_item_domain_type) + "/" + str(catalogue_item_id) + "/metadata",
cookies=self.cookie, json=json_payload)
return response
def get_classifiers(self, classifier_id=None, id_input=None):
"""
Get classifiers - paginated list or specific id
:param classifier_id: Parent classifier id
:param id_input: Child classifier id
:return: :class:`Response' object
"""
if self.api_key is not None:
if classifier_id is None and id_input is None:
response = requests.get(
self.baseURL + "/api/classifiers",
headers={'apiKey': self.api_key})
elif classifier_id and id_input is None:
response = requests.get(
self.baseURL + "/api/classifiers/" + str(classifier_id) + "/classifiers",
headers={'apiKey': self.api_key})
elif id_input and classifier_id is None:
response = requests.get(
self.baseURL + "/api/classifiers/" + str(id_input),
headers={'apiKey': self.api_key})
elif id_input and id_input:
response = requests.get(
self.baseURL + "/api/classifiers/" + str(classifier_id) + "/classifiers/" + str(id_input),
headers={'apiKey': self.api_key})
else:
if classifier_id is None and id_input is None:
response = requests.get(
self.baseURL + "/api/classifiers",
cookies=self.cookie)
elif classifier_id and id_input is None:
response = requests.get(
self.baseURL + "/api/classifiers/" + str(classifier_id) + "/classifiers",
cookies=self.cookie)
elif id_input and classifier_id is None:
response = requests.get(
self.baseURL + "/api/classifiers/" + str(id_input),
cookies=self.cookie)
elif id_input and id_input:
response = requests.get(
self.baseURL + "/api/classifiers/" + str(classifier_id) + "/classifiers/" + str(id_input),
cookies=self.cookie)
return response
def get_data_classes(self, data_model_id, data_class_id=None, id_input=None):
"""
Get data classes - paginated list or specific id
:param data_model_id: The data model id
:param data_class_id: The data class id
:param id_input: Specific data class id
:return: :class:`Response' object
"""
if self.api_key is not None:
if data_class_id is None and id_input is None:
response = requests.get(
self.baseURL + "/api/dataModels/" + str(data_model_id) + "/dataClasses",
headers={'apiKey': self.api_key})
elif data_class_id and id_input is None:
response = requests.get(
self.baseURL + "/api/dataModels/" + str(data_model_id) + "/dataClasses/" + str(data_class_id)
+ "/dataClasses",
headers={'apiKey': self.api_key})
elif id_input and data_class_id is None:
response = requests.get(
self.baseURL + "/api/dataModels/" + data_model_id + "/dataClasses/" + str(id_input),
headers={'apiKey': self.api_key})
elif id_input and id_input:
response = requests.get(
self.baseURL + "/api/dataModels/" + str(data_model_id) + "/dataClasses/" + str(data_class_id) +
"/dataClasses/" + str(id_input),
headers={'apiKey': self.api_key})
else:
if data_class_id is None and id_input is None:
response = requests.get(
self.baseURL + "/api/dataModels/" + str(data_model_id) + "/dataClasses",
cookies=self.cookie)
elif data_class_id and id_input is None:
response = requests.get(
self.baseURL + "/api/dataModels/" + str(data_model_id) + "/dataClasses/" + str(data_class_id)
+ "/dataClasses",
cookies=self.cookie)
elif id_input and data_class_id is None:
response = requests.get(
self.baseURL + "/api/dataModels/" + data_model_id + "/dataClasses/" + str(id_input),
cookies=self.cookie)
elif id_input and id_input:
response = requests.get(
self.baseURL + "/api/dataModels/" + str(data_model_id) + "/dataClasses/" + str(data_class_id) +
"/dataClasses/" + str(id_input),
cookies=self.cookie)
return response
def get_codesets(self, folder_id=None, codeset_id=None):
"""
Get codesets - paginated list or specific id
:param folder_id: The folder id
:param codeset_id: Specific codeset id
:return: :class:`Response' object
"""
if self.api_key is not None:
if folder_id is None and codeset_id is None:
response = requests.get(
self.baseURL + "/api/codeSets/",
headers={'apiKey': self.api_key})
elif codeset_id is None:
response = requests.get(
self.baseURL + "/api/folders/" + str(folder_id) + "/codeSets/",
headers={'apiKey': self.api_key})
else:
response = requests.get(
self.baseURL + "/api/codeSets/" + str(codeset_id),
headers={'apiKey': self.api_key})
else:
if folder_id is None and codeset_id is None:
response = requests.get(
self.baseURL + "/api/codeSets/",
cookies=self.cookie)
elif codeset_id is None:
response = requests.get(
self.baseURL + "/api/folders/" + str(folder_id) + "/codeSets/",
cookies=self.cookie)
else:
response = requests.get(
self.baseURL + "/api/codeSets/" + str(codeset_id),
cookies=self.cookie)
return response
def get_data_element(self, data_model_id, data_class_id, id_input=None):
"""
Get data element - paginated list or specific id
:param data_model_id: The data model id
:param data_class_id: The data class id
:param id_input: Specific data element id
:return: :class:`Response' object
"""
if self.api_key is not None:
if id_input is None:
response = requests.get(
self.baseURL + "/api/dataModels/" + str(data_model_id) + "/dataClasses/" + str(data_class_id)
+ "/dataElements",
headers={'apiKey': self.api_key})
else:
response = requests.get(
self.baseURL + "/api/dataModels/" + str(data_model_id) + "/dataClasses/" + str(data_class_id)
+ "/dataElements/" + str(id_input),
headers={'apiKey': self.api_key})
else:
if id_input is None:
response = requests.get(
self.baseURL + "/api/dataModels/" + str(data_model_id) + "/dataClasses/" + str(data_class_id)
+ "/dataElements",
cookies=self.cookie)
else:
response = requests.get(
self.baseURL + "/api/dataModels/" + str(data_model_id) + "/dataClasses/" + str(data_class_id)
+ "/dataElements/" + str(id_input),
cookies=self.cookie)
return response
def get_data_model(self, folder_id=None, id_input=None):
"""
Get data model - paginated list or specific id
:param folder_id: The folder id
:param id_input: Specific data model id
:return: :class:`Response' object
"""
if self.api_key is not None:
if folder_id is None and id_input is None:
response = requests.get(
self.baseURL + "/api/dataModels",
headers={'apiKey': self.api_key})
elif folder_id is None and id_input is not None:
response = requests.get(
self.baseURL + "/api/dataModels/" + str(id_input),
headers={'apiKey': self.api_key})
else:
response = requests.get(
self.baseURL + "/api/folders/" + str(folder_id) + "/dataModels",
headers={'apiKey': self.api_key})
else:
if folder_id is None and id_input is None:
response = requests.get(
self.baseURL + "/api/dataModels",
cookies=self.cookie)
return response
elif folder_id is None and id_input is not None:
response = requests.get(
self.baseURL + "/api/dataModels/" + str(id_input),
cookies=self.cookie)
else:
response = requests.get(
self.baseURL + "/api/folders/" + str(folder_id) + "/dataModels",
cookies=self.cookie)
return response
def get_versioned_folders(self, folder_id=None, id_input=None):
"""
List versioned folders - paginated list or specific id
:param folder_id: The folder id
:param id_input: Specific versioned folder id
:return: :class:`Response' object
"""
if self.api_key is not None:
if folder_id is None and id_input is None:
response = requests.get(
self.baseURL + "/api/versionedFolders",
headers={'apiKey': self.api_key})
elif folder_id is None and id_input is not None:
response = requests.get(
self.baseURL + "/api/versionedFolders" + str(id_input),
headers={'apiKey': self.api_key})
else:
response = requests.get(
self.baseURL + "/api/folders/" + str(folder_id) + "/versionedFolders",
headers={'apiKey': self.api_key})
else:
if folder_id is None and id_input is None:
response = requests.get(
self.baseURL + "/api/versionedFolders",
cookies=self.cookie)
return response
elif folder_id is None and id_input is not None:
response = requests.get(
self.baseURL + "/api/versionedFolders" + str(id_input),
cookies=self.cookie)
else:
response = requests.get(
self.baseURL + "/api/folders/" + str(folder_id) + "/versionedFolders",
cookies=self.cookie)
return response
def build_versioned_folder(self, json_payload):
"""
Builds a versioned folder
:param json_payload: json data to send in the body of the :class: 'Request'
:return: :class:`Response' object
"""
if self.api_key is not None:
response = requests.post(
self.baseURL + "/api/versionedFolders",
headers={'apiKey': self.api_key}, json=json_payload)
else:
response = requests.post(
self.baseURL + "/api/versionedFolders",
cookies=self.cookie, json=json_payload)
return response
def create_data_model(self, folder_id, json_payload):
"""
Creates a data model in the specified folder
:param folder_id: Folder id to create data model in
:param json_payload: json data to send in the body of the :class: 'Request'
:return: :class:`Response' object
"""
if self.api_key is not None:
response = requests.post(
self.baseURL + "/api/folders/" + str(folder_id) + "/dataModels",
headers={'apiKey': self.api_key}, json=json_payload)
else:
response = requests.post(
self.baseURL + "/api/folders/" + str(folder_id) + "/dataModels",
cookies=self.cookie, json=json_payload)
return response
def build_folder(self, json_payload, folder_id=None):
"""
Build a new folder.
:param json_payload: json data to send in the body of the :class: 'Request'
:param folder_id: - (optional) The folder id to build the new folder within
:return: :class:`Response' object
"""
if folder_id is None:
if self.api_key is not None:
response = requests.post(
self.baseURL + "/api/folders",
headers={'apiKey': self.api_key}, json=json_payload)
else:
response = requests.post(
self.baseURL + "/api/folders",
cookies=self.cookie, json=json_payload)
else:
if self.api_key is not None:
response = requests.post(
self.baseURL + "/api/folders/" + folder_id + "/folders",
headers={'apiKey': self.api_key}, json=json_payload)
else:
response = requests.post(
self.baseURL + "/api/folders/" + folder_id + "/folders",
cookies=self.cookie, json=json_payload)
return response
def create_new_data_class(self, json_payload, data_model_id, data_class_id=None):
"""
Create a new data class
:param json_payload: json data to send in the body of the :class: 'Request'
:param data_model_id: The data model id to which the new class will belong
:param data_class_id: - (optional) The data class id to which the new class will belong
:return: :class:`Response' object
"""
if self.api_key is not None:
if data_class_id is None:
response = requests.post(
self.baseURL + "/api/dataModels/" + data_model_id + "/dataClasses",
headers={'apiKey': self.api_key}, json=json_payload)
else:
response = requests.post(
self.baseURL + "/api/dataModels/" + data_model_id + "/dataClasses/"
+ data_class_id + "/dataClasses",
headers={'apiKey': self.api_key}, json=json_payload)
else:
if data_class_id is None:
response = requests.post(
self.baseURL + "/api/dataModels/" + data_model_id + "/dataClasses",
cookies=self.cookie, json=json_payload)
else:
response = requests.post(
self.baseURL + "/api/dataModels/" + data_model_id +
"/dataClasses/" + data_class_id + "/dataClasses",
cookies=self.cookie, json=json_payload)
return response
def update_data_class(self, json_payload, data_model_id, data_class_id=None):
"""
Updates a data class
:param json_payload: json data to send in the body of the :class: 'Request'
:param data_model_id: The data model id to which the class belongs
:param data_class_id: The data class to update
:return: :class:`Response' object
"""
if self.api_key is not None:
if data_class_id is None:
response = requests.put(
self.baseURL + "/api/dataModels/" + data_model_id,
headers={'apiKey': self.api_key}, json=json_payload)
else:
response = requests.put(
self.baseURL + "/api/dataModels/" + data_model_id + "/dataClasses/" + data_class_id,
headers={'apiKey': self.api_key}, json=json_payload)
else:
if data_class_id is None:
response = requests.put(
self.baseURL + "/api/dataModels/" + data_model_id,
cookies=self.cookie, json=json_payload)
else:
response = requests.put(
self.baseURL + "/api/dataModels/" + data_model_id + "/dataClasses/" + data_class_id,
cookies=self.cookie, json=json_payload)
return response
def create_data_element(self, json_payload, data_model_id, data_class_id):
"""
Creates a data element
:param json_payload: json data to send in the body of the :class: 'Request'
:param data_model_id: The data model id to which the element should belong
:param data_class_id: The data class id to which the element should belong
:return: :class:`Response' object
"""
if self.api_key is not None:
response = requests.post(
self.baseURL + "/api/dataModels/" + data_model_id + "/dataClasses/" + data_class_id + "/dataElements",
headers={'apiKey': self.api_key}, json=json_payload)
else:
response = requests.post(
self.baseURL + "/api/dataModels/" + data_model_id + "/dataClasses/" + data_class_id + "/dataElements",
cookies=self.cookie, json=json_payload)
return response
def method_constructor(self, command, json_payload=None, *args):
"""
A generalised way of creating any endpoint. Any additional arguments provided via args will be appended
to baseurl allowing custom endpoints to be rapidly built.
:param command: String value that must be one of 'put', 'post', 'get' or 'delete'
:param json_payload: - (optional) json data to send in the body of the :class: 'Request'
:param args: - (optional) String to compose endpoints
:return: :class:`Response' object
"""
if command not in ['put', 'post', 'get', "delete"]:
raise ValueError("Must be put, post, delete or get")
append_string = ""
for vals in args:
append_string = append_string + vals
if self.api_key is not None:
if command == "put":
response = requests.put(self.baseURL + append_string,
headers={'apiKey': self.api_key}, json=json_payload)
elif command == "post":
response = requests.post(self.baseURL + append_string,
headers={'apiKey': self.api_key}, json=json_payload)
elif command == "get":
response = requests.get(self.baseURL + append_string,
headers={'apiKey': self.api_key}, json=json_payload)
elif command == "delete":
response = requests.delete(self.baseURL + append_string,
headers={'apiKey': self.api_key}, json=json_payload)
else:
if command == "put":
response = requests.put(self.baseURL + append_string,
cookies=self.cookie, json=json_payload)
elif command == "post":
response = requests.post(self.baseURL + append_string,
cookies=self.cookie, json=json_payload)
elif command == "get":
response = requests.get(self.baseURL + append_string,
cookies=self.cookie, json=json_payload)
elif command == "delete":
response = requests.delete(self.baseURL + append_string,
cookies=self.cookie, json=json_payload)
return response