Skip to content

Commit 95b791c

Browse files
authored
feat: Use HEAD HTTP method to check if a document exists (#192)
* feat: Use HEAD HTTP method to check if a document exists * feat: Applies PR review suggestion
1 parent e44a3e8 commit 95b791c

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

arango/collection.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -630,20 +630,20 @@ def has(
630630
headers["x-arango-allow-dirty-read"] = "true"
631631

632632
request = Request(
633-
method="get",
633+
method="head",
634634
endpoint=f"/_api/document/{handle}",
635635
headers=headers,
636636
read=self.name,
637637
)
638638

639639
def response_handler(resp: Response) -> bool:
640-
if resp.error_code == 1202:
641-
return False
642640
if resp.status_code == 412:
643641
raise DocumentRevisionError(resp, request)
642+
if resp.status_code == 404:
643+
return False
644644
if not resp.is_success:
645645
raise DocumentInError(resp, request)
646-
return bool(resp.body)
646+
return True
647647

648648
return self._execute(request, response_handler)
649649

tests/test_document.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -1554,7 +1554,7 @@ def test_document_has(col, bad_col, docs):
15541554

15551555
with assert_raises(DocumentRevisionError) as err:
15561556
col.has(doc_input, rev=bad_rev, check_rev=True)
1557-
assert err.value.error_code == 1200
1557+
assert err.value.error_code == 412
15581558

15591559
# Test existing documents with bad revision
15601560
for doc_input in [
@@ -1564,15 +1564,15 @@ def test_document_has(col, bad_col, docs):
15641564
]:
15651565
with assert_raises(DocumentRevisionError) as err:
15661566
col.has(doc_input)
1567-
assert err.value.error_code == 1200
1567+
assert err.value.error_code == 412
15681568

15691569
with assert_raises(DocumentRevisionError) as err:
15701570
col.has(doc_input, rev=bad_rev)
1571-
assert err.value.error_code == 1200
1571+
assert err.value.error_code == 412
15721572

15731573
with assert_raises(DocumentRevisionError) as err:
15741574
col.has(doc_input, rev=bad_rev, check_rev=True)
1575-
assert err.value.error_code == 1200
1575+
assert err.value.error_code == 412
15761576

15771577
assert doc_input in col
15781578
assert col.has(doc_input, rev=rev, check_rev=True) is True
@@ -1651,12 +1651,12 @@ def test_document_has(col, bad_col, docs):
16511651
# Test get with bad database
16521652
with assert_raises(DocumentInError) as err:
16531653
bad_col.has(doc_key)
1654-
assert err.value.error_code in {11, 1228}
1654+
assert err.value.error_code == 401
16551655

16561656
# Test contains with bad database
16571657
with assert_raises(DocumentInError) as err:
16581658
assert doc_key in bad_col
1659-
assert err.value.error_code in {11, 1228}
1659+
assert err.value.error_code == 401
16601660

16611661

16621662
def test_document_get(col, bad_col, docs):

0 commit comments

Comments
 (0)