Skip to content

Commit 72e354d

Browse files
committed
feat: Use HEAD HTTP method to check if a document exists
1 parent 77cbc68 commit 72e354d

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

arango/collection.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from arango.api import ApiGroup
77
from arango.connection import Connection
88
from arango.cursor import Cursor
9+
from arango.errno import CONFLICT
910
from arango.exceptions import (
1011
ArangoServerError,
1112
CollectionChecksumError,
@@ -508,7 +509,7 @@ def has(
508509
handle, body, headers = self._prep_from_doc(document, rev, check_rev)
509510

510511
request = Request(
511-
method="get",
512+
method="head",
512513
endpoint=f"/_api/document/{handle}",
513514
headers=headers,
514515
read=self.name,
@@ -519,9 +520,11 @@ def response_handler(resp: Response) -> bool:
519520
return False
520521
if resp.status_code == 412:
521522
raise DocumentRevisionError(resp, request)
523+
if resp.status_code == 404:
524+
return False
522525
if not resp.is_success:
523526
raise DocumentInError(resp, request)
524-
return bool(resp.body)
527+
return True
525528

526529
return self._execute(request, response_handler)
527530

tests/test_document.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,7 +1390,7 @@ def test_document_has(col, bad_col, docs):
13901390

13911391
with assert_raises(DocumentRevisionError) as err:
13921392
col.has(doc_input, rev=bad_rev, check_rev=True)
1393-
assert err.value.error_code == 1200
1393+
assert err.value.error_code == 412
13941394

13951395
# Test existing documents with bad revision
13961396
for doc_input in [
@@ -1400,15 +1400,15 @@ def test_document_has(col, bad_col, docs):
14001400
]:
14011401
with assert_raises(DocumentRevisionError) as err:
14021402
col.has(doc_input)
1403-
assert err.value.error_code == 1200
1403+
assert err.value.error_code == 412
14041404

14051405
with assert_raises(DocumentRevisionError) as err:
14061406
col.has(doc_input, rev=bad_rev)
1407-
assert err.value.error_code == 1200
1407+
assert err.value.error_code == 412
14081408

14091409
with assert_raises(DocumentRevisionError) as err:
14101410
col.has(doc_input, rev=bad_rev, check_rev=True)
1411-
assert err.value.error_code == 1200
1411+
assert err.value.error_code == 412
14121412

14131413
assert doc_input in col
14141414
assert col.has(doc_input, rev=rev, check_rev=True) is True
@@ -1487,12 +1487,12 @@ def test_document_has(col, bad_col, docs):
14871487
# Test get with bad database
14881488
with assert_raises(DocumentInError) as err:
14891489
bad_col.has(doc_key)
1490-
assert err.value.error_code in {11, 1228}
1490+
assert err.value.error_code == 401
14911491

14921492
# Test contains with bad database
14931493
with assert_raises(DocumentInError) as err:
14941494
assert doc_key in bad_col
1495-
assert err.value.error_code in {11, 1228}
1495+
assert err.value.error_code == 401
14961496

14971497

14981498
def test_document_get(col, bad_col, docs):

0 commit comments

Comments
 (0)