Skip to content

feat: Use HEAD HTTP method to check if a document exists #192

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
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
8 changes: 4 additions & 4 deletions arango/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,20 +630,20 @@ def has(
headers["x-arango-allow-dirty-read"] = "true"

request = Request(
method="get",
method="head",
endpoint=f"/_api/document/{handle}",
headers=headers,
read=self.name,
)

def response_handler(resp: Response) -> bool:
if resp.error_code == 1202:
return False
if resp.status_code == 412:
raise DocumentRevisionError(resp, request)
if resp.status_code == 404:
return False
if not resp.is_success:
raise DocumentInError(resp, request)
return bool(resp.body)
return True

return self._execute(request, response_handler)

Expand Down
12 changes: 6 additions & 6 deletions tests/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -1554,7 +1554,7 @@ def test_document_has(col, bad_col, docs):

with assert_raises(DocumentRevisionError) as err:
col.has(doc_input, rev=bad_rev, check_rev=True)
assert err.value.error_code == 1200
assert err.value.error_code == 412

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

with assert_raises(DocumentRevisionError) as err:
col.has(doc_input, rev=bad_rev)
assert err.value.error_code == 1200
assert err.value.error_code == 412

with assert_raises(DocumentRevisionError) as err:
col.has(doc_input, rev=bad_rev, check_rev=True)
assert err.value.error_code == 1200
assert err.value.error_code == 412

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

# Test contains with bad database
with assert_raises(DocumentInError) as err:
assert doc_key in bad_col
assert err.value.error_code in {11, 1228}
assert err.value.error_code == 401


def test_document_get(col, bad_col, docs):
Expand Down
Loading