55
66import re
77from dataclasses import dataclass , field
8- from typing import Any , Optional , Protocol
8+ from typing import Any , AsyncGenerator , Generator , Optional , Protocol
99
1010from synapseclient import Synapse
1111from synapseclient .api import (
2020 list_organizations_sync ,
2121 update_organization_acl ,
2222)
23- from synapseclient .core .async_utils import async_to_sync
23+ from synapseclient .core .async_utils import (
24+ async_to_sync ,
25+ skip_async_to_sync ,
26+ wrap_async_generator_to_sync_generator ,
27+ )
2428from synapseclient .core .constants .concrete_types import CREATE_SCHEMA_REQUEST
2529from synapseclient .models .mixins .asynchronous_job import AsynchronousCommunicator
2630from synapseclient .models .mixins .json_schema import JSONSchemaVersionInfo
@@ -119,9 +123,9 @@ def delete(self, synapse_client: Optional["Synapse"] = None) -> None:
119123 """
120124 return None
121125
122- def get_json_schema_list (
126+ def get_json_schemas (
123127 self , synapse_client : Optional ["Synapse" ] = None
124- ) -> list ["JSONSchema" ]:
128+ ) -> Generator ["JSONSchema" , None , None ]:
125129 """
126130 Gets the list of JSON Schemas that are part of this organization
127131
@@ -142,11 +146,16 @@ def get_json_schema_list(
142146 syn = Synapse()
143147 syn.login()
144148
145- org = SchemaOrganization("my.org.name")
146- org.get_json_schema_list()
149+ org = SchemaOrganization("dpetest")
150+ js_generator = org.get_json_schemas()
151+ for item in js_generator:
152+ print(item)
147153 ```
148154 """
149- return []
155+ yield from wrap_async_generator_to_sync_generator (
156+ async_gen_func = self .get_json_schemas_async ,
157+ synapse_client = synapse_client ,
158+ )
150159
151160 def get_acl (self , synapse_client : Optional ["Synapse" ] = None ) -> dict [str , Any ]:
152161 """
@@ -382,13 +391,14 @@ async def delete_org():
382391 await self .get_async (synapse_client = synapse_client )
383392 await delete_organization (self .id , synapse_client = synapse_client )
384393
385- async def get_json_schema_list_async (
394+ @skip_async_to_sync
395+ async def get_json_schemas_async (
386396 self , synapse_client : Optional ["Synapse" ] = None
387- ) -> list ["JSONSchema" ]:
397+ ) -> AsyncGenerator ["JSONSchema" , None ]:
388398 """
389- Gets the list of JSON Schemas that are part of this organization
399+ Gets the JSON Schemas that are part of this organization
390400
391- Returns: A list of JSONSchema objects
401+ Returns: An AsyncGenerator of JSONSchema objects
392402
393403 Raises:
394404 ValueError: If the name has not been set
@@ -406,21 +416,29 @@ async def get_json_schema_list_async(
406416 from synapseclient import Synapse
407417 import asyncio
408418
409- syn = Synapse()
410- syn.login()
419+ async def get_schemas():
411420
412- org = SchemaOrganization("dpetest")
413- schemas = asyncio.run(org.get_json_schema_list_async())
421+ syn = Synapse()
422+ syn.login()
423+
424+ org = SchemaOrganization("dpetest")
425+ js_generator = org.get_json_schemas_async()
426+ js_list = []
427+ async for item in js_generator:
428+ js_list.append(item)
429+ return js_list
430+
431+ js_list = asyncio.run(get_schemas())
432+ for item in js_list:
433+ print(item)
414434 ```
415435
416436 """
417437 if not self .name :
418438 raise ValueError ("SchemaOrganization must have a name" )
419439 response = list_json_schemas (self .name , synapse_client = synapse_client )
420- schemas = []
421440 async for item in response :
422- schemas .append (JSONSchema ().fill_from_dict (item ))
423- return schemas
441+ yield JSONSchema ().fill_from_dict (item )
424442
425443 async def get_acl_async (
426444 self , synapse_client : Optional ["Synapse" ] = None
@@ -654,17 +672,17 @@ def delete(self) -> None:
654672
655673 def get_versions (
656674 self , synapse_client : Optional ["Synapse" ] = None
657- ) -> list ["JSONSchemaVersionInfo" ]:
675+ ) -> Generator ["JSONSchemaVersionInfo" , None , None ]:
658676 """
659- Gets a list of all versions of this JSONSchema
677+ Gets all versions of this JSONSchema
660678
661679 Arguments:
662680 synapse_client: If not passed in and caching was not disabled by
663681 `Synapse.allow_client_caching(False)` this will use the last created
664682 instance from the Synapse class constructor
665683
666684 Returns:
667- A JSONSchemaVersionInfo for each version of this schema
685+ A Generator containing the JSONSchemaVersionInfo for each version of this schema
668686
669687 Example: Get all versions of the JSONSchema
670688
@@ -676,10 +694,15 @@ def get_versions(
676694 syn = Synapse()
677695 syn.login()
678696
679- js = JSONSchema("my.schema.name", "my.org.name")
680- versions = get_versions()
697+ schema = JSONSchema(organization_name="dpetest", name="test.schematic.Biospecimen")
698+ version_generator = schema.get_versions()
699+ for item in version_generator:
700+ print(item)
681701 """
682- return []
702+ yield from wrap_async_generator_to_sync_generator (
703+ async_gen_func = self .get_versions_async ,
704+ synapse_client = synapse_client ,
705+ )
683706
684707 def get_body (
685708 self , version : Optional [str ] = None , synapse_client : Optional ["Synapse" ] = None
@@ -929,19 +952,20 @@ async def delete_schema():
929952
930953 await delete_json_schema (self .uri , synapse_client = synapse_client )
931954
955+ @skip_async_to_sync
932956 async def get_versions_async (
933957 self , synapse_client : Optional ["Synapse" ] = None
934- ) -> list [JSONSchemaVersionInfo ]:
958+ ) -> AsyncGenerator [JSONSchemaVersionInfo , None ]:
935959 """
936- Gets a list of all versions of this JSONSchema
960+ Gets all versions of this JSONSchema
937961
938962 Arguments:
939963 synapse_client: If not passed in and caching was not disabled by
940964 `Synapse.allow_client_caching(False)` this will use the last created
941965 instance from the Synapse class constructor
942966
943967 Returns:
944- A JSONSchemaVersionInfo for each version of this schema
968+ A generator containing each version of this schema
945969
946970 Example: Get all the versions of the JSONSchema
947971
@@ -951,24 +975,32 @@ async def get_versions_async(
951975 from synapseclient import Synapse
952976 import asyncio
953977
954- syn = Synapse()
955- syn.login()
978+ async def get_versions():
956979
957- js = JSONSchema("my.schema.name", "my.org.name")
958- versions = asyncio.run(get_versions_async())
980+ syn = Synapse()
981+ syn.login()
982+
983+ schema = JSONSchema(organization_name="dpetest", name="test.schematic.Biospecimen")
984+ version_generator = schema.get_versions_async()
985+ version_list = []
986+ async for item in version_generator:
987+ version_list.append(item)
988+ return version_list
989+
990+ version_list = asyncio.run(get_versions())
991+ for item in version_list:
992+ print(item)
959993 ```
960994 """
961995 all_schemas = list_json_schema_versions (
962996 self .organization_name , self .name , synapse_client = synapse_client
963997 )
964- versions = []
965998 async for schema in all_schemas :
966999 # Schemas created without a semantic version will be returned from the API call.
9671000 # Those won't be returned here since they aren't really versions.
9681001 # JSONSchemaVersionInfo.semantic_version could also be changed to optional.
9691002 if "semanticVersion" in schema :
970- versions .append (self ._create_json_schema_version_from_response (schema ))
971- return versions
1003+ yield self ._create_json_schema_version_from_response (schema )
9721004
9731005 async def get_body_async (
9741006 self , version : Optional [str ] = None , synapse_client : Optional ["Synapse" ] = None
0 commit comments