Skip to content

Commit 987b906

Browse files
authored
Merge pull request #14 from halloju/patch
Add patch_file()
2 parents 77d5a25 + 3765c49 commit 987b906

2 files changed

Lines changed: 46 additions & 2 deletions

File tree

src/solid/solid_api.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,22 @@ def put_file(self, url, content: RequestContent, content_type, options: WriteOpt
189189
return self.put(url, request_options)
190190

191191
def patch_file(self, url, patch_content, patch_content_type) -> Response:
192-
raise Exception('Not implemented')
192+
# raise Exception('Not implemented')
193+
if url[-1] == '/':
194+
raise Exception("Cannot use patchFile to create a folder : {}".format(url))
195+
# if options.merge == MERGE.KEEP_TARGET and self.item_exists(url):
196+
# raise Exception(f'File already exists: {url}')
197+
198+
request_options = {
199+
'headers': {
200+
'Link': LINK.RESOURCE.value,
201+
'Content-Type': patch_content_type,
202+
},
203+
'content': patch_content
204+
}
205+
206+
return self.patch(url, request_options)
207+
193208

194209
def read_folder(self, url, options: ReadFolderOptions = ReadFolderOptions()) -> FolderData:
195210
if url[-1] != '/':

tests/test_solid_api.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@
66
from httpx import HTTPStatusError
77

88
from solid.auth import Auth
9-
from solid.solid_api import SolidAPI
9+
from solid.solid_api import SolidAPI, WriteOptions
1010
from solid.utils.api_util import append_slashes_at_end
1111

1212

1313
def gen_random_str() -> str:
1414
return uuid.uuid4().hex
1515

16+
def parse_turtle(turtle: str) -> str:
17+
lines = turtle.split('\n')
18+
for line in lines:
19+
if line.startswith('<> dct'):
20+
return line
1621

1722
POD_ENDPOINT = os.getenv('SOLID_ENDPOINT')
1823
IDP = os.getenv('SOLID_IDP')
@@ -145,3 +150,27 @@ def test_file():
145150

146151
# delete
147152
api.delete(url)
153+
154+
# patch - create ttl file
155+
patchedUrl = url + '.ttl'
156+
body = "<> <http://purl.org/dc/terms/title> \"This is a test file\" .\n<> <http://www.w3.org/2000/10/swap/pim/contact#fullName> \"Eric Miller\" ."
157+
f = io.BytesIO(body.encode('UTF-8'))
158+
api.create_file(patchedUrl, f, 'text/turtle', WriteOptions())
159+
160+
# retrieve ttl file
161+
resp = api.get(patchedUrl)
162+
assert resp.text == body
163+
164+
# patch - update ttl file
165+
body = "DELETE DATA { <> <http://www.w3.org/2000/10/swap/pim/contact#fullName> \"Eric Miller\" };\nINSERT DATA { <> <http://www.w3.org/2000/10/swap/pim/contact#personalTitle> \"Dr.\" }"
166+
f = io.BytesIO(body.encode('UTF-8'))
167+
api.patch_file(patchedUrl, f, 'application/sparql-update')
168+
169+
# retrieve updated ttl file
170+
resp = api.get(patchedUrl)
171+
patchedBody = '<> dct:title "This is a test file"; contact:personalTitle "Dr.".'
172+
assert parse_turtle(resp.text) == patchedBody
173+
174+
175+
176+

0 commit comments

Comments
 (0)