Skip to content

Commit d9a7fef

Browse files
authored
[SYNPY-1623] Factory function for get and creation of link entity (#1243)
* Create factory operations for get, as well as creating the Link Entity class
1 parent ae01f46 commit d9a7fef

33 files changed

+4042
-289
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ jobs:
4949
needs: [pre-commit]
5050

5151
strategy:
52+
fail-fast: false
5253
matrix:
5354
os: [ubuntu-22.04, macos-13, windows-2022]
5455

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ coverage.xml
3333
.ipynb_checkpoints
3434
*.ipynb
3535
.env
36+
test.synapseConfig
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Synapse Factory Operations
2+
3+
Contained within this file are experimental interfaces for working with the Synapse Python
4+
Client. Unless otherwise noted these interfaces are subject to change at any time. Use
5+
at your own risk.
6+
7+
## API Reference
8+
9+
[](){ #factory-get-async }
10+
::: synapseclient.operations.get_async
11+
12+
[](){ #factory-file-options-async }
13+
::: synapseclient.operations.FileOptions
14+
options:
15+
inherited_members: true
16+
17+
[](){ #factory-activity-options-async }
18+
::: synapseclient.operations.ActivityOptions
19+
options:
20+
inherited_members: true
21+
22+
[](){ #factory-table-options-async }
23+
::: synapseclient.operations.TableOptions
24+
options:
25+
inherited_members: true
26+
27+
[](){ #factory-link-options-async }
28+
::: synapseclient.operations.LinkOptions
29+
options:
30+
inherited_members: true
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[](){ #link-async }
2+
# Link
3+
4+
Contained within this file are experimental interfaces for working with the Synapse Python
5+
Client. Unless otherwise noted these interfaces are subject to change at any time. Use
6+
at your own risk.
7+
8+
## API Reference
9+
10+
::: synapseclient.models.Link
11+
options:
12+
inherited_members: true
13+
members:
14+
- get_async
15+
- store_async
16+
---
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Synapse Factory Operations
2+
3+
Contained within this file are experimental interfaces for working with the Synapse Python
4+
Client. Unless otherwise noted these interfaces are subject to change at any time. Use
5+
at your own risk.
6+
7+
## API Reference
8+
9+
[](){ #factory-get-sync }
10+
::: synapseclient.operations.get
11+
12+
[](){ #factory-file-options-sync }
13+
::: synapseclient.operations.FileOptions
14+
options:
15+
inherited_members: true
16+
17+
[](){ #factory-activity-options-sync }
18+
::: synapseclient.operations.ActivityOptions
19+
options:
20+
inherited_members: true
21+
22+
[](){ #factory-table-options-sync }
23+
::: synapseclient.operations.TableOptions
24+
options:
25+
inherited_members: true
26+
27+
[](){ #factory-link-options-sync }
28+
::: synapseclient.operations.LinkOptions
29+
options:
30+
inherited_members: true
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[](){ #link-sync }
2+
# Link
3+
4+
Contained within this file are experimental interfaces for working with the Synapse Python
5+
Client. Unless otherwise noted these interfaces are subject to change at any time. Use
6+
at your own risk.
7+
8+
## API Reference
9+
10+
::: synapseclient.models.Link
11+
options:
12+
inherited_members: true
13+
members:
14+
- get
15+
- store
16+
---

mkdocs.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ nav:
8080
- Core: reference/core.md
8181
- REST Apis: reference/rest_apis.md
8282
- Experimental:
83+
- Factory Operations: reference/experimental/sync/factory_operations.md
8384
- Agent: reference/experimental/sync/agent.md
8485
- Project: reference/experimental/sync/project.md
8586
- Folder: reference/experimental/sync/folder.md
@@ -94,8 +95,10 @@ nav:
9495
- Activity: reference/experimental/sync/activity.md
9596
- Team: reference/experimental/sync/team.md
9697
- UserProfile: reference/experimental/sync/user_profile.md
98+
- Link: reference/experimental/sync/link_entity.md
9799
- Functional Interfaces: reference/experimental/functional_interfaces.md
98100
- Asynchronous:
101+
- Factory Operations: reference/experimental/async/factory_operations.md
99102
- Agent: reference/experimental/async/agent.md
100103
- Project: reference/experimental/async/project.md
101104
- Folder: reference/experimental/async/folder.md
@@ -110,6 +113,7 @@ nav:
110113
- Activity: reference/experimental/async/activity.md
111114
- Team: reference/experimental/async/team.md
112115
- UserProfile: reference/experimental/async/user_profile.md
116+
- Link: reference/experimental/async/link_entity.md
113117
- Mixins:
114118
- AccessControllable: reference/experimental/mixins/access_controllable.md
115119
- StorableContainer: reference/experimental/mixins/storable_container.md

synapseclient/api/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
delete_entity_generated_by,
3232
delete_entity_provenance,
3333
get_activity,
34+
get_child,
3435
get_children,
3536
get_entities_by_md5,
3637
get_entity,
@@ -167,6 +168,7 @@
167168
"get_activity",
168169
"create_activity",
169170
"update_activity",
171+
"get_child",
170172
"get_children",
171173
"post_entity_acl",
172174
"put_entity_acl",

synapseclient/api/entity_factory.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ class type. This will also download the file if `download_file` is set to True.
340340
EntityView,
341341
File,
342342
Folder,
343+
Link,
343344
MaterializedView,
344345
Project,
345346
SubmissionView,
@@ -377,6 +378,7 @@ class type. This will also download the file if `download_file` is set to True.
377378
concrete_types.MATERIALIZED_VIEW: MaterializedView,
378379
concrete_types.SUBMISSION_VIEW: SubmissionView,
379380
concrete_types.VIRTUAL_TABLE: VirtualTable,
381+
concrete_types.LINK_ENTITY: Link,
380382
}
381383

382384
entity_class = ENTITY_TYPE_MAP.get(entity["concreteType"], None)

synapseclient/api/entity_services.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,6 +1264,99 @@ async def main():
12641264
yield child
12651265

12661266

1267+
async def get_child(
1268+
entity_name: str,
1269+
parent_id: Optional[str] = None,
1270+
*,
1271+
synapse_client: Optional["Synapse"] = None,
1272+
) -> Optional[str]:
1273+
"""
1274+
Retrieve an entityId for a given parent ID and entity name.
1275+
1276+
This service can also be used to lookup projectId by setting the parentId to None.
1277+
1278+
This calls to the REST API found here: <https://rest-docs.synapse.org/rest/POST/entity/child.html>
1279+
1280+
Arguments:
1281+
entity_name: The name of the entity to find
1282+
parent_id: The parent ID. Set to None when looking up a project by name.
1283+
synapse_client: If not passed in and caching was not disabled by
1284+
`Synapse.allow_client_caching(False)` this will use the last created
1285+
instance from the Synapse class constructor.
1286+
1287+
Returns:
1288+
The entity ID if found, None if not found.
1289+
1290+
Raises:
1291+
SynapseHTTPError: If there's an error other than "not found" (404).
1292+
1293+
Example: Getting a child entity ID
1294+
Find a file by name within a folder:
1295+
1296+
```python
1297+
import asyncio
1298+
from synapseclient import Synapse
1299+
from synapseclient.api import get_child
1300+
1301+
syn = Synapse()
1302+
syn.login()
1303+
1304+
async def main():
1305+
entity_id = await get_child(
1306+
entity_name="my_file.txt",
1307+
parent_id="syn123456"
1308+
)
1309+
if entity_id:
1310+
print(f"Found entity: {entity_id}")
1311+
else:
1312+
print("Entity not found")
1313+
1314+
asyncio.run(main())
1315+
```
1316+
1317+
Example: Getting a project by name
1318+
Find a project by name:
1319+
1320+
```python
1321+
import asyncio
1322+
from synapseclient import Synapse
1323+
from synapseclient.api import get_child
1324+
1325+
syn = Synapse()
1326+
syn.login()
1327+
1328+
async def main():
1329+
project_id = await get_child(
1330+
entity_name="My Project",
1331+
parent_id=None # None for projects
1332+
)
1333+
if project_id:
1334+
print(f"Found project: {project_id}")
1335+
1336+
asyncio.run(main())
1337+
```
1338+
"""
1339+
from synapseclient import Synapse
1340+
1341+
client = Synapse.get_client(synapse_client=synapse_client)
1342+
1343+
entity_lookup_request = {
1344+
"parentId": parent_id,
1345+
"entityName": entity_name,
1346+
}
1347+
1348+
try:
1349+
response = await client.rest_post_async(
1350+
uri="/entity/child", body=json.dumps(entity_lookup_request)
1351+
)
1352+
return response.get("id")
1353+
except SynapseHTTPError as e:
1354+
if e.response.status_code == 404:
1355+
# Entity not found
1356+
return None
1357+
raise
1358+
1359+
12671360
async def set_entity_permissions(
12681361
entity_id: str,
12691362
principal_id: Optional[str] = None,

0 commit comments

Comments
 (0)