|
3 | 3 |
|
4 | 4 | import synapseclient |
5 | 5 | from synapseclient.core.utils import make_bogus_data_file |
6 | | -from synapseclient.models import File, Folder |
| 6 | +from synapseclient.models import File, Folder, JSONSchema, Project, SchemaOrganization |
7 | 7 |
|
8 | | -# 1. Set up Synapse Python client and retrieve project |
| 8 | +# 1. Set up Synapse Python client |
9 | 9 | syn = synapseclient.Synapse() |
10 | 10 | syn.login() |
11 | 11 |
|
12 | | -# Retrieve test project |
13 | | -PROJECT_ID = syn.findEntityId( |
14 | | - name="My uniquely named project about Alzheimer's Disease" |
15 | | -) |
16 | | - |
17 | | -# Create a test folder for JSON schema experiments |
18 | | -test_folder = Folder(name="clinical_data_folder", parent_id=PROJECT_ID).store() |
19 | | - |
20 | 12 | # 2. Take a look at the constants and structure of the JSON schema |
| 13 | +# Replace your own project name here |
| 14 | +PROJECT_ENT = Project(name="My uniquely named project about Alzheimer's Disease").get() |
| 15 | +# Replace your own json schema organization name here |
21 | 16 | ORG_NAME = "myUniqueAlzheimersResearchOrgTutorial" |
22 | 17 | VERSION = "0.0.1" |
23 | 18 | NEW_VERSION = "0.0.2" |
24 | 19 |
|
25 | 20 | SCHEMA_NAME = "clinicalObservations" |
26 | 21 |
|
27 | 22 | title = "Alzheimer's Clinical Observation Schema" |
28 | | -schema = { |
| 23 | +schema_body = { |
29 | 24 | "$schema": "http://json-schema.org/draft-07/schema#", |
30 | 25 | "$id": "https://example.com/schema/alzheimers_observation.json", |
31 | 26 | "title": title, |
|
48 | 43 | } |
49 | 44 |
|
50 | 45 | # 3. Try create test organization and json schema if they do not exist |
51 | | -js = syn.service("json_schema") |
52 | | -all_orgs = js.list_organizations() |
53 | | -for org in all_orgs: |
54 | | - if org["name"] == ORG_NAME: |
55 | | - syn.logger.info(f"Organization {ORG_NAME} already exists.") |
56 | | - break |
57 | | -else: |
58 | | - syn.logger.info(f"Creating organization {ORG_NAME}.") |
59 | | - js.create_organization(ORG_NAME) |
60 | | - |
61 | | -my_test_org = js.JsonSchemaOrganization(ORG_NAME) |
62 | | -test_schema = my_test_org.get_json_schema(SCHEMA_NAME) |
63 | | -if not test_schema: |
64 | | - test_schema = my_test_org.create_json_schema(schema, SCHEMA_NAME, VERSION) |
| 46 | +organization = SchemaOrganization(name=ORG_NAME) |
| 47 | +try: |
| 48 | + organization.store() |
| 49 | +except Exception as e: |
| 50 | + organization.get() |
| 51 | + |
| 52 | +schemas = list(organization.get_json_schemas()) |
| 53 | +for schema in schemas: |
| 54 | + print(schema) |
| 55 | + |
| 56 | +schema = JSONSchema(name=SCHEMA_NAME, organization_name=ORG_NAME) |
| 57 | +try: |
| 58 | + schema.get() |
| 59 | +except Exception as e: |
| 60 | + schema.store(schema_body=schema_body, version=VERSION) |
| 61 | + |
| 62 | +schema.get_body() |
65 | 63 |
|
66 | 64 | # If you want to make an update, you can re-register your schema with the organization: |
67 | 65 | updated_schema = { |
|
86 | 84 | } |
87 | 85 |
|
88 | 86 | try: |
89 | | - new_test_schema = my_test_org.create_json_schema( |
90 | | - updated_schema, SCHEMA_NAME, VERSION |
91 | | - ) |
| 87 | + schema.store(schema_body=updated_schema, version=VERSION) |
92 | 88 | except synapseclient.core.exceptions.SynapseHTTPError as e: |
93 | 89 | if e.response.status_code == 400 and "already exists" in e.response.text: |
94 | 90 | syn.logger.warning( |
|
97 | 93 | else: |
98 | 94 | raise e |
99 | 95 |
|
| 96 | +schema.store(schema_body=updated_schema, version=NEW_VERSION) |
| 97 | +schema.get_body(version=VERSION) |
100 | 98 | # 4. Bind the JSON schema to the folder |
101 | | -schema_uri = ORG_NAME + "-" + SCHEMA_NAME + "-" + VERSION |
| 99 | + |
| 100 | +# Create a test folder for JSON schema experiments |
| 101 | +test_folder = Folder(name="test_folder", parent_id=PROJECT_ENT.id).store() |
| 102 | + |
102 | 103 | bound_schema = test_folder.bind_schema( |
103 | | - json_schema_uri=schema_uri, enable_derived_annotations=True |
| 104 | + json_schema_uri=schema.uri, enable_derived_annotations=True |
104 | 105 | ) |
105 | 106 | json_schema_version_info = bound_schema.json_schema_version_info |
106 | 107 | syn.logger.info("JSON schema was bound successfully. Please see details below:") |
|
0 commit comments