diff --git a/.gitignore b/.gitignore index 35b9079..5fde443 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +.tool-versions + # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] diff --git a/example/stream_copy.py b/example/stream_copy.py new file mode 100644 index 0000000..19714a8 --- /dev/null +++ b/example/stream_copy.py @@ -0,0 +1,56 @@ +from gql.transport import transport +from specklepy.api.client import SpeckleClient +from specklepy.api.credentials import ( + StreamWrapper, + get_default_account, + get_local_accounts, +) +from specklepy.api import operations + +if __name__ == "__main__": + # initialise the client + xyz_client = SpeckleClient(host="speckle.xyz") # or whatever your host is + # client = SpeckleClient(host="localhost:3000", use_ssl=False) or use local server + + # authenticate the client with a token + xyz_client.authenticate( + token=[acc for acc in get_local_accounts() if acc.userInfo.id != "4d722975bc"][ + 0 + ].token + ) + + stream = xyz_client.stream.get(id="81d2f2c135") + print(stream) + + local_client = SpeckleClient(host="localhost:3000", use_ssl=False) + local_client.authenticate( + token=[acc for acc in get_local_accounts() if acc.userInfo.id == "4d722975bc"][ + 0 + ].token + ) + + local_stream_id = local_client.stream.create(stream.name) + print(local_stream_id) + + remote_commits = xyz_client.commit.list(stream.id) + + local_wrapper = StreamWrapper(f"http://localhost:3000/streams/{local_stream_id}") + + for remote_commit in remote_commits: + xyz_wrapper = StreamWrapper( + f"https://speckle.xyz/streams/{stream.id}/commits/{remote_commit.id}" + ) + + received_object = operations.receive( + remote_commit.referencedObject, xyz_wrapper.get_transport() + ) + + sent_object_id = operations.send( + received_object, [local_wrapper.get_transport()] + ) + + commit_id = local_client.commit.create(local_stream_id, sent_object_id) + + # xyz_client.object.get() + + print(commit_id) \ No newline at end of file diff --git a/specklepy/api/resources/commit.py b/specklepy/api/resources/commit.py index 7b8e0cb..3d3efe5 100644 --- a/specklepy/api/resources/commit.py +++ b/specklepy/api/resources/commit.py @@ -185,3 +185,40 @@ class Resource(ResourceBase): return self.make_request( query=query, params=params, return_type="commitDelete", parse_response=False ) + + def received( + self, + stream_id: str, + commit_id: str, + source_application: str = "python", + message: Optional[str] = None, + ) -> bool: + """ + Mark a commit object a received by the source application. + """ + query = gql( + """ + mutation CommitReceive($receivedInput:CommitReceivedInput!){ + commitReceive(input:$receivedInput) + } + """ + ) + params = { + "receivedInput": { + "sourceApplication": source_application, + "streamId": stream_id, + "commitId": commit_id, + "message": "message", + } + } + + try: + return self.make_request( + query=query, + params=params, + return_type="commitReceive", + parse_response=False, + ) + except Exception as ex: + print(ex.with_traceback) + return False diff --git a/tests/test_commit.py b/tests/test_commit.py index ef95191..698fd19 100644 --- a/tests/test_commit.py +++ b/tests/test_commit.py @@ -1,7 +1,9 @@ +from typing import Dict, List import pytest from specklepy.api import operations from specklepy.api.models import Commit, Stream from specklepy.transports.server.server import ServerTransport +from gql import gql @pytest.mark.run(order=4) @@ -68,3 +70,58 @@ class TestCommit: deleted = client.commit.delete(stream_id=stream.id, commit_id=commit_id) assert deleted is True + + def _get_commit_activity(self, client, stream_id, commit_id) -> List[Dict]: + return client.httpclient.execute( + gql( + """ + query Commit($stream_id: String!, $commit_id: String!) { + stream(id: $stream_id) { + commit(id: $commit_id) { + id + referencedObject + message + authorId + authorName + authorAvatar + branchName + createdAt + sourceApplication + totalChildrenCount + parents + activity { + items { + userId + actionType + info + } + } + } + } + } + """ + ), + variable_values={"stream_id": stream_id, "commit_id": commit_id}, + )["stream"]["commit"]["activity"] + + def test_commit_marked_as_received(self, client, stream, mesh) -> None: + commit = Commit(message="this commit should be received") + commit.id = client.commit.create( + stream_id=stream.id, + object_id=mesh.id, + message=commit.message, + ) + + activity = self._get_commit_activity(client, stream.id, commit.id) + + assert len(activity) == 1 + assert ( + client.commit.received( + stream.id, + commit.id, + source_application="pytest", + message="testing received", + ) + == True + ) + assert len(activity) == 2