Skip to content

Commit 1aed1b9

Browse files
authored
[SYNPY-1625] Swap list functions to generators (#1228)
* Add synchronous versions of JSON schema listing functions and move to generators
1 parent 7fde663 commit 1aed1b9

File tree

3 files changed

+128
-19
lines changed

3 files changed

+128
-19
lines changed

synapseclient/api/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@
7171
get_organization,
7272
get_organization_acl,
7373
list_json_schema_versions,
74+
list_json_schema_versions_sync,
7475
list_json_schemas,
76+
list_json_schemas_sync,
7577
list_organizations,
7678
list_organizations_sync,
7779
update_organization_acl,
@@ -189,7 +191,9 @@
189191
"get_organization_acl",
190192
"update_organization_acl",
191193
"list_json_schemas",
194+
"list_json_schemas_sync",
192195
"list_json_schema_versions",
196+
"list_json_schema_versions_sync",
193197
"get_json_schema_body",
194198
"delete_json_schema",
195199
# api client

synapseclient/api/json_schema_services.py

Lines changed: 85 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ async def update_organization_acl(
521521

522522
async def list_json_schemas(
523523
organization_name: str, *, synapse_client: Optional["Synapse"] = None
524-
) -> List[Dict[str, Any]]:
524+
) -> AsyncGenerator[Dict[str, Any], None]:
525525
"""
526526
List all JSON schemas for an organization.
527527
@@ -535,7 +535,7 @@ async def list_json_schemas(
535535
instance from the Synapse class constructor
536536
537537
Returns:
538-
A list of JsonSchemaInfo objects, each containing:
538+
A generator of JsonSchemaInfo objects, each containing:
539539
- organizationId: The Synapse issued numeric identifier for the organization.
540540
- organizationName: The name of the organization to which this schema belongs.
541541
- schemaId: The Synapse issued numeric identifier for the schema.
@@ -549,22 +549,52 @@ async def list_json_schemas(
549549

550550
request_body = {"organizationName": organization_name}
551551

552-
results = []
553-
554552
async for item in rest_post_paginated_async(
555553
"/schema/list", body=request_body, synapse_client=client
556554
):
557-
results.append(item)
555+
yield item
558556

559-
return results
557+
558+
def list_json_schemas_sync(
559+
organization_name: str, *, synapse_client: Optional["Synapse"] = None
560+
) -> Generator[Dict[str, Any], None, None]:
561+
"""
562+
List all JSON schemas for an organization.
563+
564+
Retrieves all JSON schemas that belong to the specified organization. This operation
565+
does not require authentication and will return all publicly visible schemas.
566+
567+
Arguments:
568+
organization_name: The name of the organization to list schemas for
569+
synapse_client: If not passed in and caching was not disabled by
570+
`Synapse.allow_client_caching(False)` this will use the last created
571+
instance from the Synapse class constructor
572+
573+
Returns:
574+
A generator of JsonSchemaInfo objects, each containing:
575+
- organizationId: The Synapse issued numeric identifier for the organization.
576+
- organizationName: The name of the organization to which this schema belongs.
577+
- schemaId: The Synapse issued numeric identifier for the schema.
578+
- schemaName: The name of the this schema.
579+
- createdOn: The date this JSON schema was created.
580+
- createdBy: The ID of the user that created this JSON schema.
581+
"""
582+
from synapseclient import Synapse
583+
584+
client = Synapse.get_client(synapse_client=synapse_client)
585+
586+
request_body = {"organizationName": organization_name}
587+
588+
for item in client._POST_paginated("/schema/list", body=request_body):
589+
yield item
560590

561591

562592
async def list_json_schema_versions(
563593
organization_name: str,
564594
json_schema_name: str,
565595
*,
566596
synapse_client: Optional["Synapse"] = None,
567-
) -> List[Dict[str, Any]]:
597+
) -> AsyncGenerator[Dict[str, Any], None]:
568598
"""
569599
List version information for a JSON schema.
570600
@@ -579,7 +609,52 @@ async def list_json_schema_versions(
579609
instance from the Synapse class constructor
580610
581611
Returns:
582-
A list of JsonSchemaVersionInfo objects, each containing:
612+
A generator of JsonSchemaVersionInfo objects, each containing:
613+
- organizationId: The Synapse issued numeric identifier for the organization.
614+
- organizationName: The name of the organization to which this schema belongs.
615+
- schemaName: The name of the this schema.
616+
- schemaId: The Synapse issued numeric identifier for the schema.
617+
- versionId: The Synapse issued numeric identifier for this version.
618+
- $id: The full '$id' of this schema version
619+
- semanticVersion: The semantic version label provided when this version was created. Can be null if a semantic version was not provided when this version was created.
620+
- createdOn: The date this JSON schema version was created.
621+
- createdBy: The ID of the user that created this JSON schema version.
622+
- jsonSHA256Hex: The SHA-256 hexadecimal hash of the UTF-8 encoded JSON schema.
623+
624+
Object matching <https://rest-docs.synapse.org/rest/org/sagebionetworks/repo/model/schema/JsonSchemaVersionInfo.html>
625+
"""
626+
request_body = {
627+
"organizationName": organization_name,
628+
"schemaName": json_schema_name,
629+
}
630+
631+
async for item in rest_post_paginated_async(
632+
"/schema/version/list", body=request_body, synapse_client=synapse_client
633+
):
634+
yield item
635+
636+
637+
def list_json_schema_versions_sync(
638+
organization_name: str,
639+
json_schema_name: str,
640+
*,
641+
synapse_client: Optional["Synapse"] = None,
642+
) -> Generator[Dict[str, Any], None, None]:
643+
"""
644+
List version information for a JSON schema.
645+
646+
Retrieves version information for all versions of the specified JSON schema within
647+
an organization. This shows the history and available versions of a schema.
648+
649+
Arguments:
650+
organization_name: The name of the organization containing the schema
651+
json_schema_name: The name of the JSON schema to list versions for
652+
synapse_client: If not passed in and caching was not disabled by
653+
`Synapse.allow_client_caching(False)` this will use the last created
654+
instance from the Synapse class constructor
655+
656+
Returns:
657+
A generator of JsonSchemaVersionInfo objects, each containing:
583658
- organizationId: The Synapse issued numeric identifier for the organization.
584659
- organizationName: The name of the organization to which this schema belongs.
585660
- schemaName: The name of the this schema.
@@ -602,14 +677,8 @@ async def list_json_schema_versions(
602677
"schemaName": json_schema_name,
603678
}
604679

605-
results = []
606-
607-
async for item in rest_post_paginated_async(
608-
"/schema/version/list", body=request_body, synapse_client=client
609-
):
610-
results.append(item)
611-
612-
return results
680+
for item in client._POST_paginated("/schema/version/list", body=request_body):
681+
yield item
613682

614683

615684
async def get_json_schema_body(

tests/unit/synapseclient/api/unit_test_json_schema_services.py

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,9 @@ async def async_gen():
200200
yield {"schemaId": "2"}
201201

202202
mock_rest_post.return_value = async_gen()
203-
result = await jss.list_json_schemas("org", synapse_client=None)
203+
result = []
204+
async for item in jss.list_json_schemas("org", synapse_client=None):
205+
result.append(item)
204206
assert result == [{"schemaId": "1"}, {"schemaId": "2"}]
205207
mock_rest_post.assert_called_once()
206208

@@ -218,13 +220,47 @@ async def async_gen():
218220
yield {"versionId": "2"}
219221

220222
mock_rest_post.return_value = async_gen()
221-
result = await jss.list_json_schema_versions(
223+
result = []
224+
async for item in jss.list_json_schema_versions(
222225
"org", "schema", synapse_client=None
223-
)
226+
):
227+
result.append(item)
224228
assert result == [{"versionId": "1"}, {"versionId": "2"}]
225229
mock_rest_post.assert_called_once()
226230

227231

232+
@patch("synapseclient.Synapse")
233+
def test_list_json_schemas_sync(mock_synapse):
234+
mock_client = MagicMock()
235+
mock_synapse.get_client.return_value = mock_client
236+
mock_client._POST_paginated.return_value = iter(
237+
[
238+
{"schemaId": "1"},
239+
{"schemaId": "2"},
240+
]
241+
)
242+
result = list(jss.list_json_schemas_sync("org", synapse_client=None))
243+
assert result == [{"schemaId": "1"}, {"schemaId": "2"}]
244+
mock_client._POST_paginated.assert_called_once()
245+
246+
247+
@patch("synapseclient.Synapse")
248+
def test_list_json_schema_versions_sync(mock_synapse):
249+
mock_client = MagicMock()
250+
mock_synapse.get_client.return_value = mock_client
251+
mock_client._POST_paginated.return_value = iter(
252+
[
253+
{"versionId": "1"},
254+
{"versionId": "2"},
255+
]
256+
)
257+
result = list(
258+
jss.list_json_schema_versions_sync("org", "schema", synapse_client=None)
259+
)
260+
assert result == [{"versionId": "1"}, {"versionId": "2"}]
261+
mock_client._POST_paginated.assert_called_once()
262+
263+
228264
@patch("synapseclient.Synapse")
229265
async def test_get_json_schema_body(mock_synapse):
230266
mock_client = AsyncMock()

0 commit comments

Comments
 (0)