Skip to content

Commit bca8856

Browse files
Adding patch endpoint (#385)
* Adding patch endpoint --------- Co-authored-by: Robin de Rooij <[email protected]>
1 parent 3868ce2 commit bca8856

File tree

6 files changed

+336
-277
lines changed

6 files changed

+336
-277
lines changed

poetry.lock

+261-272
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ python = ">=3.8.0,<4.0"
1010
requests = "*"
1111
shed = "*"
1212
numpydoc = "*"
13-
typeguard = "*"
13+
typeguard = "~2.13.3"
1414
tqdm = "*"
1515
click = ">=8.0"
1616

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
# ---
2525

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

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

terminusdb_client/client/Client.py

+45-1
Original file line numberDiff line numberDiff line change
@@ -2187,6 +2187,50 @@ def patch(
21872187
)
21882188
return json.loads(result)
21892189

2190+
def patch_resource(
2191+
self,
2192+
patch: Patch,
2193+
branch=None,
2194+
message=None,
2195+
author=None,
2196+
match_final_state=True,
2197+
):
2198+
"""Apply the patch object to the given resource
2199+
2200+
Do not connect when using public API.
2201+
2202+
Returns
2203+
-------
2204+
dict
2205+
After object
2206+
2207+
Examples
2208+
--------
2209+
>>> client = Client("http://127.0.0.1:6363/")
2210+
>>> client.connect(user="admin", key="root", team="admin", db="some_db")
2211+
>>> patch_obj = Patch(json='{"name" : { "@op" : "ValueSwap", "@before" : "Jane", "@after": "Janine" }}')
2212+
>>> result = client.patch_resource(patch_obj,branch="main")
2213+
>>> print(result)
2214+
'["Person/Jane"]'"""
2215+
commit_info = self._generate_commit(message, author)
2216+
request_dict = {
2217+
"patch": patch.content,
2218+
"message" : commit_info["message"],
2219+
"author" : commit_info["author"],
2220+
"match_final_state" : match_final_state
2221+
}
2222+
patch_url = self._branch_base("patch", branch)
2223+
2224+
result = _finish_response(
2225+
self._session.post(
2226+
patch_url,
2227+
headers=self._default_headers,
2228+
json=request_dict,
2229+
auth=self._auth(),
2230+
)
2231+
)
2232+
return json.loads(result)
2233+
21902234
def clonedb(
21912235
self, clone_source: str, newid: str, description: Optional[str] = None
21922236
) -> None:
@@ -2954,7 +2998,7 @@ def _apply_url(self, branch: Optional[str] = None):
29542998
return self._branch_base("apply", branch)
29552999

29563000
def _patch_url(self):
2957-
return self._branch_base("patch")
3001+
return f"{self.api}/patch"
29583002

29593003
def _push_url(self):
29603004
return self._branch_base("push")

terminusdb_client/tests/integration_tests/test-docker-compose.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ volumes:
55

66
services:
77
terminusdb-server:
8-
image: terminusdb/terminusdb-server:v11.0.0-rc1
8+
image: terminusdb/terminusdb-server:dev
99
container_name: terminusdb-server
1010
hostname: terminusdb-server
1111
tty: true

terminusdb_client/tests/integration_tests/test_client.py

+27-1
Original file line numberDiff line numberDiff line change
@@ -383,10 +383,36 @@ def test_add_get_remove_user(docker_url):
383383
user = client.get_user("test")
384384

385385

386+
def test_patch(docker_url):
387+
# create client
388+
client = Client(docker_url, user_agent=test_user_agent)
389+
client.connect(user="admin", team="admin")
390+
client.create_database("patch")
391+
schema = [{"@id" : "Person",
392+
"@type" : "Class",
393+
"name" : "xsd:string"}]
394+
instance = [{"@type" : "Person",
395+
"@id" : "Person/Jane",
396+
"name" : "Jane"}]
397+
client.insert_document(schema, graph_type="schema")
398+
client.insert_document(instance)
399+
400+
patch = Patch(
401+
json='{"@id": "Person/Jane", "name" : { "@op" : "SwapValue", "@before" : "Jane", "@after": "Janine" }}'
402+
)
403+
404+
client.patch_resource(patch)
405+
doc = client.get_document('Person/Jane')
406+
assert doc == {"@type" : "Person",
407+
"@id" : "Person/Jane",
408+
"name" : "Janine"}
409+
client.delete_database("patch", "admin")
410+
411+
386412
def test_diff_ops(docker_url, test_schema):
387413
# create client and db
388414
client = Client(docker_url, user_agent=test_user_agent)
389-
client.connect()
415+
client.connect(user="admin", team="admin")
390416
client.create_database("test_diff_ops")
391417
public_diff = Client(
392418
"https://cloud.terminusdb.com/jsondiff", user_agent=test_user_agent

0 commit comments

Comments
 (0)