Skip to content

Adding patch endpoint #385

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
533 changes: 261 additions & 272 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ python = ">=3.8.0,<4.0"
requests = "*"
shed = "*"
numpydoc = "*"
typeguard = "*"
typeguard = "~2.13.3"
tqdm = "*"
click = ">=8.0"

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

# ---

requires = ["requests", "numpydoc", "click<9.0,>=7.0", "shed", "typeguard", "tqdm"]
requires = ["requests", "numpydoc", "click<9.0,>=7.0", "shed", "typeguard~2.13.3", "tqdm"]

extras_require = {"dataframe": ["numpy >= 1.13.0", "pandas >= 0.23.0"]}

Expand Down
46 changes: 45 additions & 1 deletion terminusdb_client/client/Client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2187,6 +2187,50 @@ def patch(
)
return json.loads(result)

def patch_resource(
self,
patch: Patch,
branch=None,
message=None,
author=None,
match_final_state=True,
):
"""Apply the patch object to the given resource

Do not connect when using public API.

Returns
-------
dict
After object

Examples
--------
>>> client = Client("http://127.0.0.1:6363/")
>>> client.connect(user="admin", key="root", team="admin", db="some_db")
>>> patch_obj = Patch(json='{"name" : { "@op" : "ValueSwap", "@before" : "Jane", "@after": "Janine" }}')
>>> result = client.patch_resource(patch_obj,branch="main")
>>> print(result)
'["Person/Jane"]'"""
commit_info = self._generate_commit(message, author)
request_dict = {
"patch": patch.content,
"message" : commit_info["message"],
"author" : commit_info["author"],
"match_final_state" : match_final_state
}
patch_url = self._branch_base("patch", branch)

result = _finish_response(
self._session.post(
patch_url,
headers=self._default_headers,
json=request_dict,
auth=self._auth(),
)
)
return json.loads(result)

def clonedb(
self, clone_source: str, newid: str, description: Optional[str] = None
) -> None:
Expand Down Expand Up @@ -2954,7 +2998,7 @@ def _apply_url(self, branch: Optional[str] = None):
return self._branch_base("apply", branch)

def _patch_url(self):
return self._branch_base("patch")
return f"{self.api}/patch"

def _push_url(self):
return self._branch_base("push")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ volumes:

services:
terminusdb-server:
image: terminusdb/terminusdb-server:v11.0.0-rc1
image: terminusdb/terminusdb-server:dev
container_name: terminusdb-server
hostname: terminusdb-server
tty: true
Expand Down
28 changes: 27 additions & 1 deletion terminusdb_client/tests/integration_tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,10 +383,36 @@ def test_add_get_remove_user(docker_url):
user = client.get_user("test")


def test_patch(docker_url):
# create client
client = Client(docker_url, user_agent=test_user_agent)
client.connect(user="admin", team="admin")
client.create_database("patch")
schema = [{"@id" : "Person",
"@type" : "Class",
"name" : "xsd:string"}]
instance = [{"@type" : "Person",
"@id" : "Person/Jane",
"name" : "Jane"}]
client.insert_document(schema, graph_type="schema")
client.insert_document(instance)

patch = Patch(
json='{"@id": "Person/Jane", "name" : { "@op" : "SwapValue", "@before" : "Jane", "@after": "Janine" }}'
)

client.patch_resource(patch)
doc = client.get_document('Person/Jane')
assert doc == {"@type" : "Person",
"@id" : "Person/Jane",
"name" : "Janine"}
client.delete_database("patch", "admin")


def test_diff_ops(docker_url, test_schema):
# create client and db
client = Client(docker_url, user_agent=test_user_agent)
client.connect()
client.connect(user="admin", team="admin")
client.create_database("test_diff_ops")
public_diff = Client(
"https://cloud.terminusdb.com/jsondiff", user_agent=test_user_agent
Expand Down